Send HTML Page as Email Body in ASP.Net

To send HTML page as Email body first create a 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.

Add the below code in the HTML page.

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
</head>
<body>
    <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 EaysCode<br />
        <br />
        <a style="color: #22BCE5" href="{Url}">{Title}</a><br />
        {Description}
        <br />
        <br />
        Thanks<br />
        EasyCode
    </span>
</body>
</html>

In the above code you can see the placeholders

{UserName} – Name of the recipient
{Url} – Url of the article
{Title} – Title of the article
{Description} – Description of the Article
Just replace the place holders while sending emails.

C# Code to Send HTML Page as Email Body

protected void SendEmail(object sender, EventArgs e)
      {
          string body = this.PopulateBody("John",
              "Populate HTML Table with XML data using JavaScript",
              "http://easycode.onewebapp.com/",
            "The article explains how to Populate HTML Table with XML data using JavaScript and jQuery. The XML string will be read into an XML document.");
          this.SendHtmlFormattedEmail("mailtodilak@gmail.com", "New article published!", body);
      }

      private string PopulateBody(string userName, string title, string url, string description)
      {
          string body = string.Empty;
          using (StreamReader reader = new StreamReader(Server.MapPath("~/SampleEmailTemplate.html")))
          {
              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;
      }

      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);
          }
      }

 

Add below code in web.config page

<appSettings>
   <add key="Host" value="smtp.gmail.com"/>
   <add key="EnableSsl" value="true"/>
   <add key="UserName" value="Registered Email"/>
   <add key="Password" value="password"/>
   <add key="Port" value="587"/>
 </appSettings>

 

Output

Output to Send HTML Page as Email Body

 

Demo Code

Download Demo