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


[code language=”html”]
<html xmlns=””>
<img src = “” /><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 />
<br /><br />
Thanks<br />
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.
[code language=”csharp”]
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;
[/code] [code language=”vb”]
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
[code language=”csharp”]
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”]);
[code language=”vb”]
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”))
End Sub
And below are the web.config settings. I am making use of GMAIL SMTP server to send emails
      <addkey=”UserName”value=”[email protected]”/>
Sending the Formatted HTML email
On the click of the send button I am sending the formatted HTML emails.
protected void SendEmail(object sender, EventArgs e)
    string body = this.PopulateBody(“John”,
        “Fetch multiple values as Key Value pair in ASP.Net AJAX AutoCompleteExtender”,
        “” +
        “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(“[email protected]”, “New article published!” , body);
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”, _
       “” & _
       “-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(“[email protected]”, “New article published!”, body)
End Sub


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

Download Sample


One thought on “Send HTML Formatted Emails in ASP.Net using C#

Leave a Reply

Your email address will not be published. Required fields are marked *