In this article I will explain with an example, how to send email with HTML Templates using MailKit in ASP.Net.
Installing MailKit package
You will need to install the MailKit package, for details on installation please refer Install MailKit from Nuget in Visual Studio.
Mail Server Settings in Web.Config file
The following Mail Server settings need to be saved in the Web.Config file.
Note: It is necessary to use the sender’s email address credentials while defining the Gmail SMTP Server Credentials as Gmail the sender’s email address must be same as the Gmail Username specified in credentials.
    <smtp deliveryMethod="Network" from="">
          password="GMAILor2STEP-PASSWORD" />
Adding Email Template
The very first step is to Right Click the Project in the Solution Explorer and click Add and then New Item and then select HTML Page and name it as EmailTemplate.htm.
Send Email with HTML Templates using MailKit in ASP.Net
Location of the EmailTemplate
The EmailTemplate is placed inside the Template Folder (Directory) in Project Folder.
Send Email with HTML Templates using MailKit in ASP.Net
Building HTML Template for Email Body
The HTML Template of the Email will be built by generating an HTML containing some placeholders which will be replaced with the actual content.
Advantage of creating templates instead of building HTML using String Builder class or String concatenation in code is that, one can easily change the HTML of the template without changing the code.
The following HTML Email Template consists of four placeholders:
{UserName} – Name of the recipient.
{Url} – Url of the article.
{Title} – Title of the article.
{Description} – Description of the Article.
These placeholders will be replaced with the actual (real) values, when the email is being sent.
<html xmlns="">
    <img style="background:black;src=""/><br /><br />
    <div style="border-top:3px solid #61028D">&nbsp;</div>
    <span style="font-family:Arialfont-size:10pt">
        Hello <b>{UserName}</b>,<br /><br />
        A new article has been published on ASPSnippets.<br /><br/>
        <style="color:#61028D" href="{Url}">{Title}</a><br />
        <br /><br />
        Thanks<br />
HTML Markup
The HTML Markup consists of:
Button – For sending email.
The Button has been assigned with an OnClick event handler.
<asp:Button ID="btnSend" runat="server" Text="Send" OnClick="SendEmail" />
You will need to import the following namespaces.
using MimeKit;
using MailKit.Net.Smtp;
using System.IO;
using System.Configuration;
using System.Net.Configuration;
Imports MimeKit
Imports MailKit.Net.Smtp
Imports System.IO
Imports System.Configuration
Imports System.Net.Configuration
Sending Email with HTML Templates using MailKit
MimeMessage Class Properties
Following are the required properties of the MimeMessage class.
From – Sender’s email address.
To – Recipient(s) Email Address.
CC – Carbon Copies. (If any)
BCC – Blind Carbon Copies. (If any)
Subject – Subject of the Email.
Body – Body of the Email.
Attachments – Attachments. (If any)
ReplyTo – ReplyTo Email address.
SmtpClient Class Methods
Following are the methods of the SMTP class.
Connect – The connection to the SMTP Server is established using the domain and the port number.
Authenticate – The username and password of the SMTP Server is authenticated.
Send – The MimeMessage object is passed to it and the email is sent
Disconnect – Disconnects the connection with SMTP Server.
When the SendEmail Button is clicked, the PopulateBody method is called and contents of the HTML Email Template file are read using the StreamReader class.
The placeholders are replaced with their respective values and finally the contents of the HTML Email Template are returned.
Next, the formatted HTML body, email address (recepientEmail) and Subject are set into the respective properties of the object of MimeMessage class.
Setting Body of Email
For Body, an object of Builder class is created. The Body of the email is HTML hence it is set into the HtmlBody property of the Builder class object.
Sending Email
Then, an object of the SmtpClient class is created and the values of Host, Port, UserName, Password and Sender email address (from) fetched from the SMTP section of the Web.Config file are passed as parameter to respective methods of the SmtpClient class.
And, the methods of the Mail Server such as ConnectAuthenticateSend and Disconnect are executed and a success message is displayed in JavaScript Alert Message Box using RegisterStartupScript method.
protected void SendEmail(object sender, EventArgs e)
    string body = this.PopulateBody("John",
        "Fetch multiple values as Key Value pair in ASP.Net AJAX AutoCompleteExtender",
        "Here Mudassar Khan has explained with an example, how to fetch multiple column values i.e." +
        " ID and Text values in the ASP.Net AJAX Control Toolkit AutocompleteExtender" +
        " and also how to fetch the select text and value server side on postback.");
    //Read SMTP section from Web.Config.
    SmtpSection smtpSection = (SmtpSection)ConfigurationManager.GetSection("");
    using (MimeMessage mm = new MimeMessage())
        mm.From.Add(new MailboxAddress("Sender", smtpSection.From));
        mm.To.Add(new MailboxAddress("Recepient", ""));
        mm.Subject = "New article published!";
        BodyBuilder builder = new BodyBuilder();
        builder.HtmlBody = body;
        mm.Body = builder.ToMessageBody();
        using (SmtpClient smtp = new SmtpClient())
            smtp.Connect(smtpSection.Network.Host, smtpSection.Network.Port);
            smtp.Authenticate(smtpSection.Network.UserName, smtpSection.Network.Password);
    ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Email sent.')", true);
private string PopulateBody(string userName, string title, string url, string description)
    string body = string.Empty;
    using (StreamReader reader = new StreamReader(Server.MapPath("~/Template/EmailTemplate.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;
Protected Sub SendEmail(ByVal sender As Object, ByVal e As EventArgs)
    Dim body As String = Me.PopulateBody("John", "Fetch multiple values as Key Value pair in ASP.Net AJAX AutoCompleteExtender",
                                            "Here Mudassar Khan has explained with an example, how to fetch multiple column values i.e." _
                                            & " ID and Text values in the ASP.Net AJAX Control Toolkit AutocompleteExtender" _
                                            & " and also how to fetch the select text and value server side on postback.")      
    'Read SMTP section from Web.Config.
    Dim smtpSection As SmtpSection = CType(ConfigurationManager.GetSection(""), SmtpSection)
    Using mm As MimeMessage = New MimeMessage()
        mm.From.Add(New MailboxAddress("Sender", smtpSection.From))
        mm.[To].Add(New MailboxAddress("Recepient", ""))
        mm.Subject = "New article published!"
        Dim builder As BodyBuilder = New BodyBuilder()
        builder.HtmlBody = body
        mm.Body = builder.ToMessageBody()
        Using smtp As SmtpClient = New SmtpClient()
            smtp.Connect(smtpSection.Network.Host, smtpSection.Network.Port)
            smtp.Authenticate(smtpSection.Network.UserName, smtpSection.Network.Password)
        End Using
    End Using
    ClientScript.RegisterStartupScript(Me.GetType(), "alert", "alert('Email sent.')", True)
End Sub
Private Function PopulateBody(ByVal userName As String, ByVal title As String, ByVal url As String, ByVal description As String) As String
    Dim body As String = String.Empty
    Using reader As StreamReader = New StreamReader(Server.MapPath("~/Template/EmailTemplate.html"))
        body = reader.ReadToEnd()
    End Using
    body = body.Replace("{UserName}", userName)
    body = body.Replace("{Title}", title)
    body = body.Replace("{Url}", url)
    body = body.Replace("{Description}", description)
    Return body
End Function
Possible Errors
The possible errors (exceptions) occurring while sending email with MailKit in .Net are covered in the following article.
Send Email with HTML Templates using MailKit in ASP.Net