How to Send Mails in ASP .NET 5 on Azure

Unfortunately neither SendGrid nor System.Net.Mail is supported in the actual version of ASP .NET 5 (RC).

So how to send mails then?
One solution is to use MailKit

To use MailKit just add “MailKit”: “1.3.0-beta5” to your project.json

What you need is an SMTP server. On Azure you can create a SendGrid account and use the host name and the credentials provided in the Configurations section. SendGrid on Azure is free for up to 25’000 mails per month!

The code to send a mail is straight forward.

private void SendEmail()
 var message = new MimeMessage();
 message.From.Add(new MailboxAddress("Peter Muster", ""));
 message.To.Add(new MailboxAddress("Petra Muster", ""));
 message.Subject = "Some Subject";

message.Body = new TextPart("plain") {Text = string.Format(@"Hello, Just a little message for you") };

using (var client = new SmtpClient())
 client.Connect("", 587, false);
 client.Authenticate("", "password");