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 latest 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, you must 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 drafting 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 represents an email, and it is like .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 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:
- Register Event after email is sent successfully
- Connecting servers
- Verify your Account
- Send a message
- 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:
Lucky
Nice Example Do you have full eample in your https://github.com/EdiWang
R Harris
Hello Edi, Thanks for your post. I've been trying to do this through my Office365 account but I keep getting an AuthenticationInvalidCredentials error. (You can see my issue on Stack Overflow: https://stackoverflow.com/questions/59735368/can-i-send-smtp-email-through-office365-shared-mailbox) Any thoughts on what I need to do to get this to work?
Roy Bradley
I tried using MailKit with my Core 3.1 Web Api and I get the following error: MimeKit.ParseException: Unexpected token at offset 29 at MimeKit.MailboxAddress.set_Address(String value) at MimeKit.MailboxAddress..ctor(Encoding encoding, String name, String address) at MimeKit.MailboxAddress..ctor(String name, String address) at NETCore.MailKit.Core.EmailService.SendEmail(String mailTo, String mailCc, String mailBcc, String subject, String message, Encoding encoding, Boolean isHtml, SenderInfo sender) at NETCore.MailKit.Core.EmailService.Send(String mailTo, String subject, String message, Boolean isHtml, SenderInfo sender)
Mirnoca
When you look at the repository of Microsoft you don't find any Obsolete Attribute in SmtpClient class. https://github.com/dotnet/corefx/blob/master/src/System.Net.Mail/src/System/Net/Mail/SmtpClient.cs The statement, that it's obsolete is wrong.