Now here in this tutorial, I’ll explain how to send HTML page in email body in asp .net c#. Using Gmail SMTP settings in asp.net using c# or vb.net.
In my previous tutorials, I’d explained how to send test mail using gmail smtp settings, how to send mail with asp.net gridview data in email body, difference between dataset datareader dataadapter and dataview and other more cracking tutorials on Repeater, Gridivew, Asp.net here.
The .NET framework has a built-in namespace for handling email settings, which is System.Net.Mail namespace. In the following example, I’ll use two classes from the System.Net.Mail namespace:
- For email settings, we will use the MailMessage class and
- For SMTP settings and sending email, we will use the SmtpClient class
I guess you are aware that mails are sent via SMTP server, if not then please follow my first tutorial about how to send test mail using gmail smtp settings.
HTML Markup To Send HTML Page in Email Body – [.aspx]
Following is the complete HTML markup code for your .aspx page:
<head id=”Head1″ runat=”server”>
<title>Send html web page in mail in asp.net</title>
</head>
<body>
<form id=”form1″ runat=”server”>
<div>
<table>
<tr>
<td><h4>Send html web page in mail in asp.net</h4></td>
</tr>
<tr>
<td>
<asp:TextBox ID=”txtToEmail” runat=”server” Width=”250″ Height=”28″
placeholder=”To email: example@example.com”></asp:TextBox>
<asp:RequiredFieldValidator ID=”rfvToEmail” runat=”server” ForeColor=”Red”
ControlToValidate=”txtToEmail” ErrorMessage=”Required” Display=”Dynamic”>
</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID=”revToEmail” runat=”server” ForeColor=”Red”
ValidationExpression=”\w+([-+.’]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*”
ControlToValidate=”txtToEmail” ErrorMessage=”Invalid Email”>
</asp:RegularExpressionValidator>
</td>
</tr>
<tr><td> </td></tr>
<tr>
<td>
<asp:TextBox ID=”txtRecipient” runat=”server” Width=”250″ Height=”28″
placeholder=”Recipient Name: John, Mayank”></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:TextBox ID=”txtMessage” runat=”server” Width=”250″ Height=”150″
placeholder=”Your body message” TextMode=”MultiLine”></asp:TextBox>
<asp:RequiredFieldValidator ID=”rfvMessage” runat=”server” ForeColor=”Red”
ControlToValidate=”txtMessage” ErrorMessage=”Required”>
</asp:RequiredFieldValidator>
</td>
</tr>
<tr><td> </td></tr>
<tr>
<td>
<asp:Button ID=”btnSend” runat=”server” Text=”Send Mail!”
OnClick=”btnSend_Click” />
</td>
</tr>
<tr><td> </td></tr>
<tr>
<td>
<asp:Label ID=”lblMsg” runat=”server”></asp:Label>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
Now create mailtemplate.htm page or use your existing HTML page. Following is the code that we need to use for sending emails, choose your language from below.
Sample Code To Send HTML Page in Email Body in Asp .Net C#
Add the following namespace that is required for sending emails:
using System.Text;
using System.IO;
Now following is the code that we can use to send emails using MailMessage class:
{
}protected void btnSend_Click(object sender, EventArgs e)
{
try
{
string Subject = “This is test mail with html web page”,
Body = GetWebPageContent(txtRecipient.Text.Trim(), txtMessage.Text.Trim()),
ToEmail = txtToEmail.Text.Trim();
string SMTPUser = “yourname@gmail.com”, SMTPPassword = “yourpassword”;
MailMessage mail = new MailMessage();
mail.From = new MailAddress(SMTPUser, “AspnetO”);
mail.To.Add(ToEmail);
mail.Subject = Subject;
mail.Body = Body;
mail.IsBodyHtml = true;
mail.Priority = MailPriority.Normal;
SmtpClient smtp = new SmtpClient();
//if you are using your smtp server, then change your host like “smtp.yourdomain.com”
smtp.Host = “smtp.gmail.com”;
//chnage your port for your host
smtp.Port = 25; //or you can also use port# 587
smtp.Credentials = new System.Net.NetworkCredential(SMTPUser, SMTPPassword);
//if you are using secure authentication using SSL/TLS then “true” else “false”
smtp.EnableSsl = true;
smtp.Send(mail);
lblMsg.Text = “Success: Mail sent successfully!”;
lblMsg.ForeColor = System.Drawing.Color.Green;
}
catch (SmtpException ex)
{
//catched smtp exception
lblMsg.Text = “SMTP Exception: “ + ex.Message.ToString();
lblMsg.ForeColor = System.Drawing.Color.Red;
}
catch (Exception ex)
{
lblMsg.Text = “Error: “ + ex.Message.ToString();
lblMsg.ForeColor = System.Drawing.Color.Red;
}
}
private string GetWebPageContent(string recipient, string customMsg)
{
StreamReader objStreamReader = new StreamReader(Server.MapPath(“~/mailtemplate.htm”));
//read html template file
string bodyMsg = objStreamReader.ReadToEnd();
//replace the dynamic string at run-time
bodyMsg = bodyMsg.Replace(“##recipient##”, recipient);
bodyMsg = bodyMsg.Replace(“##somecustommessage##”, customMsg);
return bodyMsg;
}
Sample Code To Send HTML page in Email Body – [Vb.net]
Add the following namespace that is required for sending emails:
Imports System.Text
Imports System.IO
Now following is the code that we can use to send emails using MailMessage class:
End SubProtected Sub btnSend_Click(ByVal sender As Object, ByVal e As EventArgs)
Try
Dim Subject As String = “This is test mail with html web page”,
Body As String = GetWebPageContent(txtRecipient.Text, txtMessage.Text),
ToEmail As String = txtToEmail.Text.Trim()
Dim SMTPUser As String = “yourname@gmail.com”,
SMTPPassword As String = “yourpassword”
Dim mail As New MailMessage()
mail.From = New MailAddress(SMTPUser, “AspnetO”)
mail.To.Add(ToEmail)
mail.Subject = Subject
mail.Body = Body
mail.IsBodyHtml = True
mail.Priority = MailPriority.Normal
Dim smtp As New SmtpClient()
‘if you are using your smtp server, then change your host like “smtp.yourdomain.com”
smtp.Host = “smtp.gmail.com”
‘chnage your port for your host
smtp.Port = 25 ‘or you can also use port# 587
smtp.Credentials = New System.Net.NetworkCredential(SMTPUser, SMTPPassword)
‘if you are using secure authentication using SSL/TLS then “true” else “false”
smtp.EnableSsl = True
smtp.Send(mail)
lblMsg.Text = “Success: Mail sent successfully!”
lblMsg.ForeColor = System.Drawing.Color.Green
Catch ex As SmtpException
‘catched smtp exception
lblMsg.Text = “SMTP Exception: “ & ex.Message.ToString()
lblMsg.ForeColor = System.Drawing.Color.Red
Catch ex As Exception
lblMsg.Text = “Error: “ & ex.Message.ToString()
lblMsg.ForeColor = System.Drawing.Color.Red
End Try
End Sub
Private Function GetWebPageContent(ByVal recipient As String, ByVal customMsg As String)
As String
Dim objStreamReader As New StreamReader(Server.MapPath(“~/mailtemplate.htm”))
‘read html template file
Dim bodyMsg As String = objStreamReader.ReadToEnd()
‘replace the dynamic string at run-time
bodyMsg = bodyMsg.Replace(“##recipient##”, recipient)
bodyMsg = bodyMsg.Replace(“##somecustommessage##”, customMsg)
Return bodyMsg
End Function
Have you tested it ?
Yes, absolutely!