In this article I’m going
to explain how to send email using HTML template or HTML formatted email in ASP.NET.
While working with send email from ASP.NET application I
have found the solution to send email with HTML template. Please follow the steps
I have given below.
First we
have to create HTML page. Add NewItem ->HTML pageDesign your HTML page like this

Example: SendMail.html
<!DOCTYPE html PUBLIC
"-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Email</title>
</head>
<body>
<div>
<table style="font-family:Calibri;background:#E6E6E6" >
<tr>
<td><p>Dear [MyName]</p>
<p>Thanks for
your subscription</p>
</td>
</tr>
</table>
</div>
</body>
</html>
In above code you’ll be noticed that Yellow color, here I have
created placeholder for replace the string.
Here we have to configure email
setting in we.config.
<appSettings>
<add key="Email" value="example@gmail.com"/>
<add key="Password" valueyour password"/>
<add key="MailServer" value="smtp.gmail.com"/>
<add key="MailPort" value="587"/>
<add key="IsSSLEnabled" value="true"/>
</appSettings>
Then design your aspx page like this
<%@ Page Language="C#"
AutoEventWireup="true"
CodeFile="Default.aspx.cs"
Inherits="_Default"
%>
<!DOCTYPE html PUBLIC
"-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>HTML mail</title>
</head>
<body>
<form id="form1" runat="server">
<div>
Name: <asp:TextBox ID="txtName" runat="server"></asp:TextBox><br />
Email-ID: <asp:TextBox ID="txtEmail" runat="server"></asp:TextBox><br />
<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click"/>
</div>
</form>
</body>
</html>
C#
code:
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.IO;
using System.Configuration;
public partial class _Default :
System.Web.UI.Page
{
protected void Page_Load(object
sender, EventArgs e)
{ }
protected void btnSubmit_Click(object
sender, EventArgs e)
{
try {
MailMessage
Msg = new MailMessage();
Msg.From = new
MailAddress(ConfigurationManager.AppSettings["Email"]);
Msg.To.Add(txtEmail.Text);
StreamReader
reader = new StreamReader(Server.MapPath("~/SendMail.htm"));
string
readFile = reader.ReadToEnd();
string
StrContent = "";
StrContent = readFile;
//Here
replace the name with [MyName]
StrContent = StrContent.Replace("[MyName]", txtName.Text);
Msg.Subject = "Dotnetfox - Thanks for your subscription";
Msg.Body = StrContent.ToString();
Msg.IsBodyHtml = true;
// your
remote SMTP server IP.
SmtpClient
smtp = new SmtpClient();
smtp.Host = ConfigurationManager.AppSettings["MailServer"];
System.Net.NetworkCredential
NetworkCred = new System.Net.NetworkCredential();
NetworkCred.UserName = ConfigurationManager.AppSettings["Email"];
NetworkCred.Password = ConfigurationManager.AppSettings["Password"];
smtp.UseDefaultCredentials = true;
smtp.Credentials = NetworkCred;
smtp.Port = int.Parse(ConfigurationManager.AppSettings["MailPort"]);
smtp.EnableSsl = Convert.ToBoolean(ConfigurationManager.AppSettings["IsSSLEnabled"].ToString());
smtp.Send(Msg);
}
catch (Exception ex)
{ }
}
}