In this article I will explain with an example, how to send email using MailKit library in ASP.Net WebForms using C# and VB.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 and AppSettings need to be saved in the Web.Config file.
    <smtp deliveryMethod="Network">
          port="587" />
CC and BCC in AppSettings section of Web.Config file
The following Mail Server settings and AppSettings need to be saved in the Web.Config file.
 <add key="Cc" value="" />
 <add key="Bcc" value="" />
HTML Markup
The HTML Markup consists of:
TextBox – For capturing the values of Recipient Email address, Subject, Body, Gmail account email address, Gmail account password.
Button – For sending email.
The Button has been assigned with an OnClick event handler.
<table border="0" cellpadding="0" cellspacing="0">
        <td style="width: 80px">To:</td>
        <td><asp:TextBox ID="txtTo" runat="server"></asp:TextBox></td>
        <td><asp:TextBox ID="txtSubject" runat="server"></asp:TextBox></td>
        <td valign="top">Body:</td>
        <td><asp:TextBox ID="txtBody" runat="server" TextMode="MultiLine" Height="150" Width="200"></asp:TextBox></td>
        <td>Gmail Email:</td>
        <td><asp:TextBox ID="txtEmail" runat="server"></asp:TextBox></td>
        <td>Gmail Password:</td>
        <td><asp:TextBox ID="txtPassword" runat="server" TextMode="Password"></asp:TextBox></td>
        <td><asp:Button ID="btnSend" Text="Send" OnClick="SendEmail" runat="server" /></td>
You will need to import the following namespaces.
using MimeKit;
using MailKit.Net.Smtp;
using System.Configuration;
using System.Net.Configuration;
Imports MimeKit
Imports MailKit.Net.Smtp
Imports System.Configuration
Imports System.Net.Configuration
Sending email 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 Recipient email address (to), the Sender email address (from), Subject and Body values are fetched from their respective fields and are passed as parameter to SendEmail function.
Inside the SendEmail function all these parameters are set into an object of the MimeMessage class.
The Cc and Bcc addresses are fetched from the AppSettings section of the Web.Config file and are set in the MimeMessage object.
Then, an object of the SmtpClient class is created and the values of Host and Port are fetched from the SMTP section of the Web.Config file and are passed as parameter to the Connect method.
Finally, 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 to = txtTo.Text;
    string from = txtEmail.Text;
    string password = txtPassword.Text;
    string subject = txtSubject.Text;
    string body = txtBody.Text;
    this.SendEmail(to, from, password, subject, body);
    ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Email sent.');", true);
private void SendEmail(string to, string from, string password, string subject, string body)
    //Read SMTP section from Web.Config.
    SmtpSection smtpSection = (SmtpSection)ConfigurationManager.GetSection("");
    string host = smtpSection.Network.Host;
    int port = smtpSection.Network.Port;
    //Read AppSettings section from Web.Config.
    string cc = ConfigurationManager.AppSettings["Cc"];
    string bcc = ConfigurationManager.AppSettings["Bcc"];
    using (MimeMessage mm = new MimeMessage())
        mm.From.Add(new MailboxAddress("Sender", from));
        mm.To.Add(new MailboxAddress("Recepient", to));
        mm.Subject = subject;
        mm.Cc.Add(new MailboxAddress("Cc", cc));
        mm.Bcc.Add(new MailboxAddress("Bcc",bcc ));
        BodyBuilder builder = new BodyBuilder();
        builder.TextBody = body;
        mm.Body = builder.ToMessageBody();
        using (SmtpClient smtp = new SmtpClient())
            smtp.Connect(host, port);
            smtp.Authenticate(from, password);
Protected Sub SendEmail(ByVal sender As Object, ByVal e As EventArgs) Handles btnSend.Click
    Dim [to] As String = txtTo.Text
    Dim from As String = txtEmail.Text
    Dim password As String = txtPassword.Text
    Dim subject As String = txtSubject.Text
    Dim body As String = txtBody.Text
    Me.SendEmail([to], from, password, subject, body)
    ClientScript.RegisterStartupScript(Me.GetType(), "alert", "alert('Email sent.');", True)
End Sub
Private Sub SendEmail(ByVal [to] As String, ByVal from As String, ByVal password As String, ByVal subject As String, ByVal body As String)
    'Read SMTP section from Web.Config.
    Dim smtpSection As SmtpSection = CType(ConfigurationManager.GetSection(""), SmtpSection)
    Dim host As String = smtpSection.Network.Host
    Dim port As Integer = smtpSection.Network.Port
    'Read AppSettings section from Web.Config.
    Dim cc As String = ConfigurationManager.AppSettings("Cc")
    Dim bcc As String = ConfigurationManager.AppSettings("Bcc")
    Using mm As MimeMessage = New MimeMessage()
        mm.From.Add(New MailboxAddress("Sender", from))
        mm.[To].Add(New MailboxAddress("Recepient", [to]))
        mm.Subject = subject
        mm.Cc.Add(New MailboxAddress("Cc", cc))
        mm.Bcc.Add(New MailboxAddress("Bcc", bcc))
        Dim builder As BodyBuilder = New BodyBuilder()
        builder.TextBody = body
        mm.Body = builder.ToMessageBody()
        Using smtp As SmtpClient = New SmtpClient()
            smtp.Connect(host, port)
            smtp.Authenticate(from, password)
        End Using
    End Using
End Sub
Possible Errors
The possible errors (exceptions) occurring while sending email with MailKit in .Net are covered in the following article.
Email Form
Send Email using MailKit in ASP.Net WebForms
Received Email
Send Email using MailKit in ASP.Net WebForms