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.
I have designed my htmlTemplate.html as follows.
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <title></title>
- </head>
- <body>
- <table>
- <tr>
- <td>
- <img src="http://www.c-sharpcorner.com/App_themes/csharp/images/sitelogo.png" width="150px" height="60px" />
- <br />
- <br />
- <div style="border-top:3px solid #22BCE5"> </div>
- <span style="font-family:Arial;font-size:10pt">
- Hello <b>{UserName}</b>,<br /><br />
- <a style = "color:#22BCE5" href = "{Url}">{Title}</a><br />
- {message}
- <br /><br />
- Thanks<br />
- Debendra dash
- </span>
- </td>
- </tr>
- </table>
- </body>
- </html>
The Yellow mark text should be replaced at run time.
Here I have attached the screenshot.
Here I have attached the screenshot.

Now in the "default.aspx", I have the following design view to send an email.
- <body>
- <form id="form1" runat="server">
- <div>
- <table>
- <tr>
- <td>
- Name
- </td>
- <td>
- <asp:TextBox ID="txt_name" Width="200px" runat="server"></asp:TextBox>
- </td>
- </tr>
- <tr>
- <td>
- Email Id
- </td>
- <td>
- <asp:TextBox ID="txt_email" Width="200px" runat="server"></asp:TextBox>
- </td>
- </tr>
- <tr>
- <td>
- Message
- </td>
- <td>
- <asp:TextBox ID="txt_message" Width="200px" runat="server" TextMode="MultiLine" Height="60px"></asp:TextBox>
- </td>
- </tr>
- </table>
- <asp:Button ID="btnSend" runat="server" Text="Send Email" OnClick="SendEmail" />
- </div>
- </form>
- </body>

Here is my code in default.aspx.cs.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Net.Mail;
- using System.Configuration;
- using System.IO;
- public partial class _Default: System.Web.UI.Page
- {
- protected void SendEmail(object sender, EventArgs e)
- {
- //calling for creating the email body with html template
- string body = this.createEmailBody(txt_name.Text, "Please check your account Information", txt_message.Text);
- this.SendHtmlFormattedEmail("New article published!", body);
- }
- private string createEmailBody(string userName, string title, string message)
- {
- string body = string.Empty;
- //using streamreader for reading my htmltemplate
- using(StreamReader reader = new StreamReader(Server.MapPath("~/HtmlTemplate.html")))
- {
- body = reader.ReadToEnd();
- }
- body = body.Replace("{UserName}", userName); //replacing the required things
- body = body.Replace("{Title}", title);
- body = body.Replace("{message}", message);
- return body;
- }
- private void SendHtmlFormattedEmail(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(txt_email.Text));
- 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"]; //reading from web.config
- NetworkCred.Password = ConfigurationManager.AppSettings["Password"]; //reading from web.config
- smtp.UseDefaultCredentials = true;
- smtp.Credentials = NetworkCred;
- smtp.Port = int.Parse(ConfigurationManager.AppSettings["Port"]); //reading from web.config
- smtp.Send(mailMessage);
- }
- }
- }
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.


asif rahimanPosted Jan 10, 2023, 1:01 PM EST
How to populate order details to email template
Yogesh MhadgutPosted Aug 20, 2019, 12:55 AM EDT
Template image not show in outlook
Adarsh SinghPosted Jun 28, 2019, 4:03 AM EDT
This article's is helpful for me thanks for share..............
ritavrat DwivediPosted Mar 6, 2019, 1:29 AM EST
Hi, When I send mail through SMTP of Gmail as same as you did above instead of the body HTML page is rendered in the mail.Please help why this is happening
vishal fondekarPosted Sep 21, 2018, 10:26 PM EDT
Add redirect page along with google captcha
Bala PrasathPosted Apr 23, 2018, 8:34 PM EDT
Nice codding.. Good work...................
Anthony GriggsPosted Feb 16, 2018, 6:19 AM EST
Very nice thank you! The only thing I would have added was an attachment example... but it wasn't hard to find this as you had already supplied the MailMessage class
munish pandiPosted Dec 4, 2017, 8:54 PM EST
Thank u so much but i want send javascript too in email body.. Can u help me
Sreejith kcPosted Jul 9, 2017, 9:30 PM EDT
Good work....................
Harvey BirdmanPosted Jun 3, 2017, 6:33 PM EDT
Great Work!! thanks!
Jindam SAIKIRANPosted Feb 1, 2016, 2:18 AM EST
thanx so much buddy/Sir......nice tutorial................
DanielPosted Oct 19, 2015, 11:09 AM EDT
Great, good job!!
Santhakumar MunuswamyPosted Oct 17, 2015, 11:28 PM EDT
Good one
Gowtham KTOP 100Posted Oct 13, 2015, 7:50 AM EDT
Good One
Gopi ChandPosted Oct 13, 2015, 7:11 AM EDT
Nice read
RakeshPosted Oct 13, 2015, 6:32 AM EDT
Good share
Vinodh NarayananPosted Oct 13, 2015, 2:00 AM EDT
nice share
Nilesh JadavPosted Oct 12, 2015, 11:07 PM EDT
Nice one sir
Ravi PatelPosted Oct 12, 2015, 11:07 PM EDT
nice thanks for sharing
Mukesh KumarPosted Oct 12, 2015, 10:50 PM EDT
Great, good job
Raja TPosted Oct 12, 2015, 10:19 PM EDT
Nice, Thanks for sharing
Harshad PansuriyaPosted Oct 12, 2015, 10:08 PM EDT
Nice one
Rajeesh MenothPosted Oct 12, 2015, 10:04 PM EDT
Good One