In this article I will explain with an example, how to embed image using MailKit library in ASP.Net WebForms using C# and VB.Net.
Installing MailKit package from Nuget
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: For some mail servers such as Gmail, the From Email address and the UserName email address is required to be same.
    <smtp deliveryMethod="Network" from="">
          password="GMAILor2STEP-PASSWORD" />
You will need to import the following namespaces.
using MimeKit;
using MailKit.Net.Smtp;
using MimeKit.Utils;
using System.Configuration;
using System.Net.Configuration;
Imports MimeKit
Imports MailKit.Net.Smtp
Imports MimeKit.Utils
Imports System.Configuration
Imports System.Net.Configuration
HTML Markup
The following HTML Markup consists of:
Button – For sending email.
The Button has been assigned with an OnClick event handler.
<asp:Button Text="Send" OnClick="SendEmail" runat="server" />
Embedding Image using MailKit
When the SendEmail Button is clicked, the Recipient email address (to), the Sender email address (from), Subject and Body are fetched from their respective fields and SMTP section of the Web.Config file and are set into the respective properties of the object of the MimeMessage class.
Setting Body of Email
For Body and LinkedResources, an object of Builder class is created.
Embedding Image
The image file is added to the local variable of MimeEntity class using LinkedResources property of the Builder class.
And, the ContentId of the image is generated using GenerateMessageId method of MimeUtils class.
The Body of the email is HTML (Non Text) hence it is set into the HtmlBody property of the Builder class object.
The HtmlBody of the email is created inside which the ContentId is set to the src property of the Image control and set into an object of MimeMessage class.
Sending Email
Then, an object of SmtpClient is created and the values of Host and Port 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 Connect, Authenticate, Send 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)
    //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 = "Like ASPSnippets";
        BodyBuilder builder = new BodyBuilder();
        MimeEntitymimeEntity = builder.LinkedResources.Add(@"D:\Images\Logo_Img.png");
        mimeEntity.ContentId = MimeUtils.GenerateMessageId();
        builder.HtmlBody = string.Format(@"<img src=""cid:{0}"" /><br /><div style=""border-top:3px solid #61028d"">&nbsp;</div><p>I like ASPSnippets as it provides details code explanation, working samples and great demos.</p>", mimeEntity.ContentId);
        mm.Body = builder.ToMessageBody();
        using (SmtpClient smtp = new SmtpClient())
            smtp.Connect(smtpSection.Network.Host, smtpSection.Network.Port);
            smtp.Authenticate(smtpSection.Network.From, smtpSection.Network.Password);
    ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Email sent.');", true);
Protected Sub SendEmail(ByVal sender As Object, ByVal e As EventArgs)
    '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 = "Like ASPSnippets"
        Dim builder As BodyBuilder = New BodyBuilder()
        Dim mimeEntity As MimeEntity = builder.LinkedResources.Add("D:\Images\Logo_Img.png")
        mimeEntity.ContentId = MimeUtils.GenerateMessageId()
        builder.HtmlBody = String.Format("<img src=""cid:{0}"" /><br /><div style=""border-top:3px solid #61028d"">&nbsp;</div><p>I like ASPSnippets as it provides details code explanation, working samples and great demos.</p>", mimeEntity.ContentId)
        mm.Body = builder.ToMessageBody()
        Using smtp As SmtpClient = New SmtpClient()
            smtp.Connect(smtpSection.Network.Host, smtpSection.Network.Port)
            smtp.Authenticate(smtpSection.Network.From, smtpSection.Network.Password)
        End Using
    End Using
    ClientScript.RegisterStartupScript(Me.GetType(), "alert", "alert('Email sent.');", True)
End Sub
Possible Errors
The possible errors (exceptions) occurring while sending email with MailKit in .Net are covered in the following article.
Embed images using MailKit in ASP.Net