In this article I will explain how to send HTML Formatted emails. Now a day’s many websites sent newsletters, promotional emails. These emails containing Rich Text content and images
So let’s start with it.
Email Body HTML Template
Firstly you will need to build an HTML Template of the body which will have some placeholders which will be replaced with the actual content. Advantage of creating templates instead of building HTML using String Builder in code is that you can easily change the HTML of the template without changing the code.
HTML Formatted Emails for Newsletters in ASP.Net

<html xmlns="http://www.w3.org/1999/xhtml">
<head><title></title></head>
<body>
<img src = "http://www.aspsnippets.com/images/Blue/Logo.png" /><br /><br />
<div style = "border-top:3px solid #22BCE5">&nbsp;</div>
<span style = "font-family:Arial;font-size:10pt">
Hello <b>{UserName}</b>,<br /><br />
A new article has been published on ASPSnippets.<br /><br />
<a style = "color:#22BCE5" href = "{Url}">{Title}</a><br />
{Description}
<br /><br />
Thanks<br />
ASPSnippets
</span>
</body>
</html>

Above you will notice in YELLOW I have created the following four placeholders in the HTML Email template.
{UserName} – Name of the recipient
{Url} – Url of the article
{Title} – Title of the article
{Description} – Description of the Article
We are now almost done. Now the only thing we need to do is replace the placeholders with their actual values and send the email.

Populating the HTML Formatted Body
On the click event handler of a button I will first read the HTML Template file and then replace the placeholders with their values.
C#
private string PopulateBody(string userName, string title, string url, string description)
{
    string body = string.Empty;
    using (StreamReader reader = new StreamReader(Server.MapPath("~/EmailTemplate.htm")))
    {
        body = reader.ReadToEnd();
    }
    body = body.Replace("{UserName}", userName);
    body = body.Replace("{Title}", title);
    body = body.Replace("{Url}", url);
    body = body.Replace("{Description}", description);
    return body;
}

VB.Net
Private Function PopulateBody(ByVal userName As String, ByVal title As String, ByVal url As String, ByVal description As String) As String
    Dim body As String = String.Empty
    Dim reader As StreamReader = New StreamReader(Server.MapPath("~/EmailTemplate.htm"))
    body = reader.ReadToEnd
    body = body.Replace("{UserName}", userName)
    body = body.Replace("{Title}", title)
    body = body.Replace("{Url}", url)
    body = body.Replace("{Description}", description)
    Return body
End Function
 
Email Sending Method
The below method simply accepts the email of the recipient, subject and body and sends an email. It picks up the SMTP settings from the web.config file
C#
private void SendHtmlFormattedEmail(string recepientEmail, string subject, string body)
{
    using (MailMessage mailMessage = new MailMessage())
    {
        mailMessage.From = new MailAddress(ConfigurationManager.AppSettings["UserName"]);
        mailMessage.Subject = subject;
        mailMessage.Body = body;
        mailMessage.IsBodyHtml = true;
        mailMessage.To.Add(new MailAddress(recepientEmail));
        SmtpClient smtp = new SmtpClient();
        smtp.Host = ConfigurationManager.AppSettings["Host"];
        smtp.EnableSsl = Convert.ToBoolean(ConfigurationManager.AppSettings["EnableSsl"]);
        System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();
        NetworkCred.UserName = ConfigurationManager.AppSettings["UserName"];
        NetworkCred.Password = ConfigurationManager.AppSettings["Password"];
        smtp.UseDefaultCredentials = true;
        smtp.Credentials = NetworkCred;
        smtp.Port = int.Parse(ConfigurationManager.AppSettings["Port"]);
        smtp.Send(mailMessage);
    }
}

VB.Net
Private Sub SendHtmlFormattedEmail(ByVal recepientEmail As String, ByVal subject As String, ByVal body As String)
    Dim mailMessage As MailMessage = New MailMessage
    mailMessage.From = New MailAddress(ConfigurationManager.AppSettings("UserName"))
    mailMessage.Subject = subject
    mailMessage.Body = body
    mailMessage.IsBodyHtml = True
    mailMessage.To.Add(New MailAddress(recepientEmail))
    Dim smtp As SmtpClient = New SmtpClient
    smtp.Host = ConfigurationManager.AppSettings("Host")
    smtp.EnableSsl = Convert.ToBoolean(ConfigurationManager.AppSettings("EnableSsl"))
    Dim NetworkCred As System.Net.NetworkCredential = New System.Net.NetworkCredential
    NetworkCred.UserName = ConfigurationManager.AppSettings("UserName")
    NetworkCred.Password = ConfigurationManager.AppSettings("Password")
    smtp.UseDefaultCredentials = True
    smtp.Credentials = NetworkCred
    smtp.Port = Integer.Parse(ConfigurationManager.AppSettings("Port"))
    smtp.Send(mailMessage)
End Sub

And below are the web.config settings. I am making use of GMAIL SMTP server to send emails
<appSettings>
      <addkey="Host"value="smtp.gmail.com"/>
      <addkey="EnableSsl"value="true"/>
      <addkey="UserName"value="sender@gmail.com"/>
      <addkey="Password"value="xxxxx"/>
      <addkey="Port"value="587"/>
</appSettings>
 
Sending the Formatted HTML email
On the click of the send button I am sending the formatted HTML emails.
C#
protected void SendEmail(object sender, EventArgs e)
{
    string body = this.PopulateBody("John",
        "Fetch multiple values as Key Value pair in ASP.Net AJAX AutoCompleteExtender",
        "http://www.aspsnippets.com/Articles/Fetch-multiple-values-as-Key-Value-pair-" +
        "in-ASP.Net-AJAX-AutoCompleteExtender.aspx",
        "Here Mudassar Ahmed Khan has explained how to fetch multiple column values i.e." +
        " ID and Text values in the ASP.Net AJAX Control Toolkit AutocompleteExtender"
        + "and also how to fetch the select text and value server side on postback");
    this.SendHtmlFormattedEmail("recipient@gmail.com", "New article published!" , body);
}

VB.Net
Protected Sub SendEmail(ByVal sender As Object, ByVal e As EventArgs)
    Dim body As String = Me.PopulateBody("John", _
       "Fetch multiple values as Key Value pair in ASP.Net AJAX AutoCompleteExtender", _
       "http://www.aspsnippets.com/Articles/Fetch-multiple-values-as-Key-Value" & _
       "-pair-in-ASP.Net-AJAX-AutoCompleteExtender.aspx", _
       ("Here Mudassar Ahmed Khan has explained how to fetch multiple column values i.e. ID and Text values in" & _
       " the ASP.Net AJAX Control Toolkit AutocompleteExtender" & _
       "and also how to fetch the select text and value server side on postback"))
    Me.SendHtmlFormattedEmail("recipient@gmail.com", "New article published!", body)
End Sub

Above you will notice that first I am populating the HTML body of the email from the template and then send the email to the recipient.
HTML Formatted Emails for Newsletters using C# and VB.Net in ASP.Net


Downloads
You can download the complete source code in C# and VB.Net using the download link provided below.
HTMLFormattedEmails.zip