How To Send Email Using MailKit In MVC


In this article, We will learn how to send email using MailKit in MVC.

Simple Mail Transfer Protocol (SMTP) is a TCP/IP protocol for used in sending and receiving e-mail. Most e-mail systems that send mail over the Internet use SMTP to send messages from one server to another.

below list of SMTP Server and Port Numbers:

No Server Host Port
1 Gmail 587
2 Yahoo Mail 465
3 Hotmail 465
4 587
5 Outlook 587
6 Zoho Mail 465
7 Yahoo Mail Plus 465

Install Package


MailKit is a library for used to sending email.

Send Email

Let’s Begin

Creating a new MVC Project and install the MailKit library from Manage NuGet packages.

After the add new HomeController in your project and below code in it.

        public async Task<JsonResult> SendEmailUsingMimeKit(string toEmail)
                string emailBarcodeA = string.Empty;
                string emailBarcodeB = string.Empty;
                EmailMessage message = new EmailMessage
                    Sender = new MailboxAddress("Email Send - MailKit", "your email address"),
                    Reciever = new MailboxAddress("Email Send - MailKit", ""),
                    Subject = "Test Message",
                    Content = "Hello",
                var mimeMessage = CreateMimeMessageFromEmailMessage(message, toEmail, emailBarcodeA, emailBarcodeB);
                using (SmtpClient smtpClient = new SmtpClient())
                    smtpClient.Connect("", 465, true);
                    await smtpClient.AuthenticateAsync("your email address, "your password");
                    await smtpClient.SendAsync(mimeMessage);
                    await smtpClient.DisconnectAsync(true);
                return Json(new { IsSuccess = true, Message = "Email successfully send." }, JsonRequestBehavior.AllowGet);
            catch (Exception ex)
                return Json(new { IsSuccess = false, Message = ex.Message }, JsonRequestBehavior.AllowGet);

        private MimeMessage CreateMimeMessageFromEmailMessage(EmailMessage message, string emailBarcode, string emailBarcodeA, string emailBarcodeB)
                var emailBarcodeMail = new MailboxAddress("Email Barcode", emailBarcode);
                var emailBarcodeMailA = new MailboxAddress("CC", "");
                var emailBarcodeMailB = new MailboxAddress("CC", "");
                var mimeMessage = new MimeMessage();
                if (!string.IsNullOrEmpty(emailBarcodeA))
                    emailBarcodeMailA = new MailboxAddress("CC", emailBarcodeA);

                if (!string.IsNullOrEmpty(emailBarcodeB))
                    emailBarcodeMailB = new MailboxAddress("CC", emailBarcodeB);
                var builder = new BodyBuilder();
                builder.HtmlBody = message.Content;
                if (!string.IsNullOrEmpty(message.AttachmentPath))
                    string fileName = message.AttachmentPath;
                    builder.Attachments.Add(fileName, message.Attachment);
                mimeMessage.Subject = message.Subject;
                //mimeMessage.Body = new TextPart(MimeKit.Text.TextFormat.Html);
                mimeMessage.Body = builder.ToMessageBody();
                // { Text = message.Content };
                return mimeMessage;
            catch (Exception ex)


and After Create view Index.cshtml and below code in it.

<div class="">
    <div class="col-md-12">
        <input type="text" class="form-control" id="txtEmail" placeholder="Enter Email Address" /><br/>
        <button type="button" class="btn btn-success mt-2 ml-2" onclick="SendEmail()">Send Email</button>

    function SendEmail() {
            url: "/Home/SendEmailUsingMimeKit",
            data: { "toEmail": $('#txtEmail').val() },
            success: function (result) {
            error: function (err) {

Let’s run the application and click to Send Email button.

Output :

Thank you for reading. If you have any doubt please let me know.

Read my previous blogs here.

Submit a Comment

Your email address will not be published. Required fields are marked *


Select Categories