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. <head>
  3. <title></title>
  4. </head>
  5. <body>
  6. <table>
  7. <tr>
  8. <td>
  9. <img src="http://www.c-sharpcorner.com/App_themes/csharp/images/sitelogo.png" width="150px" height="60px" />
  10. <br />
  11. <br />
  12. <div style="border-top:3px solid #22BCE5"> </div>
  13. <span style="font-family:Arial;font-size:10pt">
  14. Hello <b>{UserName}</b>,<br /><br />
  15. <a style = "color:#22BCE5" href = "{Url}">{Title}</a><br />
  16. {message}
  17. <br /><br />
  18. Thanks<br />
  19. Debendra dash
  20. </span>
  21. </td>
  22. </tr>
  23. </table>
  24. </body>
  25. </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. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7. using System.Net.Mail;
  8. using System.Configuration;
  9. using System.IO;
  10. public partial class _Default: System.Web.UI.Page
  11. {
  12. protected void SendEmail(object sender, EventArgs e)
  13. {
  14. //calling for creating the email body with html template
  15. string body = this.createEmailBody(txt_name.Text, "Please check your account Information", txt_message.Text);
  16. this.SendHtmlFormattedEmail("New article published!", body);
  17. }
  18. private string createEmailBody(string userName, string title, string message)
  19. {
  20. string body = string.Empty;
  21. //using streamreader for reading my htmltemplate
  22. using(StreamReader reader = new StreamReader(Server.MapPath("~/HtmlTemplate.html")))
  23. {
  24. body = reader.ReadToEnd();
  25. }
  26. body = body.Replace("{UserName}", userName); //replacing the required things
  27. body = body.Replace("{Title}", title);
  28. body = body.Replace("{message}", message);
  29. return body;
  30. }
  31. private void SendHtmlFormattedEmail(string subject, string body)
  32. {
  33. using(MailMessage mailMessage = new MailMessage())
  34. {
  35. mailMessage.From = new MailAddress(ConfigurationManager.AppSettings["UserName"]);
  36. mailMessage.Subject = subject;
  37. mailMessage.Body = body;
  38. mailMessage.IsBodyHtml = true;
  39. mailMessage.To.Add(new MailAddress(txt_email.Text));
  40. SmtpClient smtp = new SmtpClient();
  41. smtp.Host = ConfigurationManager.AppSettings["Host"];
  42. smtp.EnableSsl = Convert.ToBoolean(ConfigurationManager.AppSettings["EnableSsl"]);
  43. System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();
  44. NetworkCred.UserName = ConfigurationManager.AppSettings["UserName"]; //reading from web.config
  45. NetworkCred.Password = ConfigurationManager.AppSettings["Password"]; //reading from web.config
  46. smtp.UseDefaultCredentials = true;
  47. smtp.Credentials = NetworkCred;
  48. smtp.Port = int.Parse(ConfigurationManager.AppSettings["Port"]); //reading from web.config
  49. smtp.Send(mailMessage);
  50. }
  51. }
  52. }
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.