Sending Email With HTML Template

Here in this article I have shown how to send an email using HTML template.

For this I have created one ASP.NET empty website.

Added one webform(default.aspx).

Added one HTML form(HtmlTemplate.html).

Here I have shown my solution explorer.



I have designed my htmlTemplate.html as follows.
  1. <html xmlns="http://www.w3.org/1999/xhtml">  
  2.   
  3. <head>  
  4.     <title></title>  
  5. </head>  
  6.   
  7. <body>  
  8.   
  9.     <table>  
  10.   
  11.         <tr>  
  12.   
  13.             <td>  
  14.   
  15.                 <img src="http://www.c-sharpcorner.com/App_themes/csharp/images/sitelogo.png" width="150px" height="60px" />  
  16.                 <br />  
  17.                 <br />  
  18.   
  19.                 <div style="border-top:3px solid #22BCE5"> </div>  
  20.   
  21.                 <span style="font-family:Arial;font-size:10pt">  
  22.   
  23. Hello <b>{UserName}</b>,<br /><br />  
  24.   
  25. <a style = "color:#22BCE5" href = "{Url}">{Title}</a><br />  
  26.   
  27. {message}  
  28.   
  29. <br /><br />  
  30.   
  31. Thanks<br />  
  32.   
  33. Debendra dash  
  34.   
  35. </span>  
  36.   
  37.             </td>  
  38.   
  39.         </tr>  
  40.   
  41.     </table>  
  42.   
  43. </body>  
  44.   
  45. </html> 
The Yellow mark text should be replaced at run time.

Here I have attached the screenshot.

code
 
 Now in the "default.aspx", I have the following design view to send an email.
  1. <body>  
  2.     <form id="form1" runat="server">  
  3.         <div>  
  4.             <table>  
  5.                 <tr>  
  6.                     <td>  
  7.                         Name  
  8.                     </td>  
  9.                     <td>  
  10.                         <asp:TextBox ID="txt_name" Width="200px" runat="server"></asp:TextBox>  
  11.                     </td>  
  12.                 </tr>  
  13.                 <tr>  
  14.                     <td>  
  15.                         Email Id  
  16.                     </td>  
  17.                     <td>  
  18.                         <asp:TextBox ID="txt_email" Width="200px" runat="server"></asp:TextBox>  
  19.                     </td>  
  20.                 </tr>  
  21.                 <tr>  
  22.                     <td>  
  23.                         Message  
  24.                     </td>  
  25.                     <td>  
  26.                         <asp:TextBox ID="txt_message" Width="200px" runat="server" TextMode="MultiLine" Height="60px"></asp:TextBox>  
  27.                     </td>  
  28.                 </tr>  
  29.             </table>  
  30.             <asp:Button ID="btnSend" runat="server" Text="Send Email" OnClick="SendEmail" />  
  31.         </div>  
  32.     </form>  
  33. </body> 
And here how it will look.
 


Here is my code in default.aspx.cs.
  1. using System;  
  2.   
  3. using System.Collections.Generic;  
  4.   
  5. using System.Linq;  
  6.   
  7. using System.Web;  
  8.   
  9. using System.Web.UI;  
  10.   
  11. using System.Web.UI.WebControls;  
  12.   
  13. using System.Net.Mail;  
  14.   
  15. using System.Configuration;  
  16.   
  17. using System.IO;  
  18.   
  19. public partial class _Default: System.Web.UI.Page  
  20.   
  21. {  
  22.   
  23.     protected void SendEmail(object sender, EventArgs e)  
  24.   
  25.     {  
  26.         //calling for creating the email body with html template   
  27.   
  28.         string body = this.createEmailBody(txt_name.Text, "Please check your account Information", txt_message.Text);  
  29.   
  30.         this.SendHtmlFormattedEmail("New article published!", body);  
  31.   
  32.     }  
  33.   
  34.     private string createEmailBody(string userName, string title, string message)  
  35.   
  36.     {  
  37.   
  38.         string body = string.Empty;  
  39.         //using streamreader for reading my htmltemplate   
  40.   
  41.         using(StreamReader reader = new StreamReader(Server.MapPath("~/HtmlTemplate.html")))  
  42.   
  43.         {  
  44.   
  45.             body = reader.ReadToEnd();  
  46.   
  47.         }  
  48.   
  49.         body = body.Replace("{UserName}", userName); //replacing the required things  
  50.   
  51.         body = body.Replace("{Title}", title);  
  52.   
  53.         body = body.Replace("{message}", message);  
  54.   
  55.         return body;  
  56.   
  57.     }  
  58.   
  59.     private void SendHtmlFormattedEmail(string subject, string body)  
  60.   
  61.     {  
  62.   
  63.         using(MailMessage mailMessage = new MailMessage())  
  64.   
  65.         {  
  66.   
  67.             mailMessage.From = new MailAddress(ConfigurationManager.AppSettings["UserName"]);  
  68.   
  69.             mailMessage.Subject = subject;  
  70.   
  71.             mailMessage.Body = body;  
  72.   
  73.             mailMessage.IsBodyHtml = true;  
  74.   
  75.             mailMessage.To.Add(new MailAddress(txt_email.Text));  
  76.   
  77.             SmtpClient smtp = new SmtpClient();  
  78.   
  79.             smtp.Host = ConfigurationManager.AppSettings["Host"];  
  80.   
  81.             smtp.EnableSsl = Convert.ToBoolean(ConfigurationManager.AppSettings["EnableSsl"]);  
  82.   
  83.             System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();  
  84.   
  85.             NetworkCred.UserName = ConfigurationManager.AppSettings["UserName"]; //reading from web.config  
  86.   
  87.             NetworkCred.Password = ConfigurationManager.AppSettings["Password"]; //reading from web.config  
  88.   
  89.             smtp.UseDefaultCredentials = true;  
  90.   
  91.             smtp.Credentials = NetworkCred;  
  92.   
  93.             smtp.Port = int.Parse(ConfigurationManager.AppSettings["Port"]); //reading from web.config  
  94.   
  95.             smtp.Send(mailMessage);  
  96.   
  97.         }  
  98.   
  99.     }  
  100.   

Here is my web.config.
 
 
 Now save and run the project by filling the form.
 
 
After clicking the Send Email button check your email. You will get the HTML designed mail. Here is the mail I received.
 
 
 
In this way we can send an email with any type of HTML design. 


Similar Articles