The ability to send e-mail notifications is common in a variety of systems. My blog can also send emails to administrators when there are new comments, new replies, or when articles are referenced by other sites. So in. NET Core, how can we send an e-mail?

My case will use Microsoft Outlook.com's personal account to send e-mail, using the SMTP protocol. But you can also do it with other SMTP-supported mailbox systems. Anyway, first of all, you have to have a mailbox account.

About MailKit

A lot of experienced. NET programmers might say that it's not hard at all to send an email, we were able to email with the SmtpClient that came with .NET Framework more than 10 years ago, and it is also available in .NET Core. Why are you writing this article?

Well, everything that has a beginning has an end. Recently I suddenly discovered that SmtpClient has been marked as obsolete by Microsoft:

And Microsoft has chosen a successor: MailKit, which is what this article is going to introduce.

This is a cross-platform .NET e-mail library based on MimeKit that supports IMAP, POP3, and SMTP protocols. Comparing to. NET's own SmtpClient, it supports a wider range of protocols and more modern email standards. So Microsoft officials suggest that SmtpClient is only used to be compatible with old applications and that if new apps are developed, use MailKit directly.

Also, it is open source under the MIT license, which means it is free to use, and it can be built by the community together.

Send Email by SMTP

After I get this wonderful library, the first step is to migrate the old code that uses SmtpClient to MailKit. Therefore, only the SMTP protocol is used in my case to send mail.

Install from NuGet:

In Visual Studio:

Install-Package MailKit

In .NET Core CLI:

dotnet add package MailKit

Construct MimeMessage

MimeMessage is the type in MailKit that representing an email, and it is similar to .NET's own MailMessage. For example, add subject and senders:

var messageToSend = new MimeMessage
{
    Sender = new MailboxAddress("Sender Name", "Sender Email Address"),
    Subject = "Your Subject",
};

Adding sender information is different from what it used to be, and MailKit actually supports multiple senders, so From is a collection type that you can add senders via Add method:

messageToSend.From.Add(new MailboxAddress("Sender Name", "Sender Email Address"));

Body properties support multiple formats, most commonly plain text and HTML. The TextPart class is used to specify body format in the constructor, such as HTML:

messageToSend.Body = new TextPart(TextFormat.Html) { Text = bodyText };

or plain text

messageToSend.Body = new TextPart(TextFormat.Plain) { Text = bodyText };

To add recipient information:

messageToSend.To.Add(new MailboxAddress("Recipient Email Address"));

Add CC Information:

messageToSend.Cc.Add(new MailboxAddress("CC Email Address"));

Send Email via Outlook.com

Outlook in Microsoft Office 365 supports the SMTP protocol.

Server: smtp-mail.outlook.com

Port: 587

SSL: Support

The following code demonstrates several steps:

  1. Register Event after email is sent successfully
  2. Connecting servers
  3. Verify your Account
  4. Send a message
  5. Disconnect
using (var smtp = new MailKit.Net.Smtp.SmtpClient())
{
    smtp.MessageSent += (sender, args) => { // args.Response };
    smtp.ServerCertificateValidationCallback = (s, c, h, e) => true;

    await smtp.ConnectAsync("smtp-mail.outlook.com", 587, SecureSocketOptions.StartTls);
    await smtp.AuthenticateAsync("Username", "Password");
    await smtp.SendAsync(messageToSend);
    await smtp.DisconnectAsync(true);
}

In the MessageSent event, the server's response information can be obtained through the args parameter in case you might want to log the response.

The connection to outlook.com needs to be set to SecureSocketOptions.StartTls or the connection will be rejected. For other servers, you can try SecureSocketOptions.Auto

Here's how MailKit sent messages in my email library (Edi.TemplateEmail) used by my blog system: