Published:

.NET 5.0 - Send an Email via SMTP with MailKit

Tutorial built with .NET 5.0

Other versions available:

This is a quick example of how to send an email in .NET 5.0 using the MailKit email client library.

For more info on MailKit see https://github.com/jstedfast/MailKit.


Install MailKit via NuGet

.NET Core CLI: dotnet add package MailKit

Visual Studio Package Manager Console: Install-Package MailKit


Send an HTML email in .NET

This code sends a simple HTML email using the Ethereal free SMTP testing service, you can create a free test account in one click at https://ethereal.email/ and copy the username and password from below the title SMTP configuration. See instructions below for using different SMTP providers such as Gmail and Hotmail.

// create email message
var email = new MimeMessage();
email.From.Add(MailboxAddress.Parse("[email protected]"));
email.To.Add(MailboxAddress.Parse("[email protected]"));
email.Subject = "Test Email Subject";
email.Body = new TextPart(TextFormat.Html) { Text = "<h1>Example HTML Message Body</h1>" };

// send email
using var smtp = new SmtpClient();
smtp.Connect("smtp.ethereal.email", 587, SecureSocketOptions.StartTls);
smtp.Authenticate("[USERNAME]", "[PASSWORD]");
smtp.Send(email);
smtp.Disconnect(true);


Send a plain text email in .NET

This code sends the same email as above with a plain text body.

// create email message
var email = new MimeMessage();
email.From.Add(MailboxAddress.Parse("[email protected]"));
email.To.Add(MailboxAddress.Parse("[email protected]"));
email.Subject = "Test Email Subject";
email.Body = new TextPart(TextFormat.Plain) { Text = "Example Plain Text Message Body" };

// send email
using var smtp = new SmtpClient();
smtp.Connect("smtp.ethereal.email", 587, SecureSocketOptions.StartTls);
smtp.Authenticate("[USERNAME]", "[PASSWORD]");
smtp.Send(email);
smtp.Disconnect(true);


Change SMTP Provider (e.g. Gmail, Hotmail, Office365, AWS)

To change the above code to use a different email provider simply update the host parameter (the first parameter) passed to the smtp.Connect() method, for example:

// gmail
smtp.Connect("smtp.gmail.com", 587, SecureSocketOptions.StartTls);

// hotmail
smtp.Connect("smtp.live.com", 587, SecureSocketOptions.StartTls);

// office 365
smtp.Connect("smtp.office365.com", 587, SecureSocketOptions.StartTls);

// aws ses (simple email service)
smtp.Connect("email-smtp.[AWS REGION].amazonaws.com", 587, SecureSocketOptions.StartTls)


Wrap it up in an Email Service

To encapsulate the email sending functionality so it's easy to send email from anywhere in your .NET app you can create an EmailService class and IEmailService interface like below.

The email service is taken from a boilerplate .NET tutorial I posted recently, so to download and test the code locally see .NET 5.0 - Boilerplate API with Email Sign Up, Verification, Authentication & Forgot Password.

using MailKit.Net.Smtp;
using MailKit.Security;
using Microsoft.Extensions.Options;
using MimeKit;
using MimeKit.Text;
using WebApi.Helpers;

namespace WebApi.Services
{
    public interface IEmailService
    {
        void Send(string from, string to, string subject, string html);
    }

    public class EmailService : IEmailService
    {
        private readonly AppSettings _appSettings;

        public EmailService(IOptions<AppSettings> appSettings)
        {
            _appSettings = appSettings.Value;
        }

        public void Send(string from, string to, string subject, string html)
        {
            // create message
            var email = new MimeMessage();
            email.From.Add(MailboxAddress.Parse(from));
            email.To.Add(MailboxAddress.Parse(to));
            email.Subject = subject;
            email.Body = new TextPart(TextFormat.Html) { Text = html };

            // send email
            using var smtp = new SmtpClient();
            smtp.Connect(_appSettings.SmtpHost, _appSettings.SmtpPort, SecureSocketOptions.StartTls);
            smtp.Authenticate(_appSettings.SmtpUser, _appSettings.SmtpPass);
            smtp.Send(email);
            smtp.Disconnect(true);
        }
    }
}

 


Subscribe or Follow Me For Updates

Subscribe to my YouTube channel or follow me on Twitter, Facebook or GitHub to be notified when I post new content.

Other than coding...

I'm currently attempting to travel around Australia by motorcycle with my wife Tina on a pair of Royal Enfield Himalayans. You can follow our adventures on YouTube, Instagram and Facebook.


Need Some .NET Help?

Search fiverr to find help quickly from experienced .NET developers.



Supported by