The following blog post is about sending emails in/via Azure with C#. There are currently two services available that you can easily create in Azure – MailJet and SendGrid. You can also use them without Azure or you can use another service or your own Mail server, but in this post, I’ll focus on the services in Azure and how to use them. If you want to send mails from an Azure function, web job, web app or any other “application” in Azure, then the two services are good and easy to use. Both services, SendGrid and MailJet offer a free plan that allows to send up to 25k e-mails per month. Both of them work fine and are easy to configure, that’s why I’ll describe how to use the services.
Create and configure SendGrid service
The SendGrid service is available in the Azure marketplace, so you can simply add it via the portal. Just search for “SendGrid” and you’ll find the service “SendGrid Email Delivery”. If you create it, then you can also see the pricing:
When the service is provisioned, we have to create an API key that we use to send the mails. Open the SendGrid service in the Azure portal and go to manage:
Navigate to the settings – API Keys and generate a new general key. If you do so, then the next step is to configure the permissions. I just want to send mails, so “Full Access” for “Mail Send” is enough:
Copy the the generate API key! We need it later on.
Send mails via SendGrid with C#
The C# code for sending emails is simple and self-explaining:
var client = new SendGridClient("[APIKEY]"); var from = new EmailAddress("[email protected]", "CodeHollow Blog"); var to = new EmailAddress("[email protected]", "CodeHollow Blog"); var body = "This is just a simple test message!"; var msg = MailHelper.CreateSingleEmail(from, to, "Mail from Azure and SendGrid", body, ""); client.SendEmailAsync(msg).Wait();
Create and configure MailJet service
Add sender mail address
It’s required to add a sender mail address in MailJet to proof that it is your mail address and that you are allowed to use this mail address. This can be found in “Senders & Domains” – “Add a Sender Domain or Address”. Enter your mail address and wait for the confirmation mail. Accept the confirmation mail and the address is up and running.
Send mails via MailJet with C#
MailJet is configured – so we can implement the solution in C#. There is a NuGet package available for it, but this time, I’ll use the .net build in SmtpClient:
MailMessage msg = new MailMessage(); msg.From = new System.Net.Mail.MailAddress("[email protected]", "CodeHollow Blog"); msg.To.Add(new System.Net.Mail.MailAddress("[email protected]", "Armin Reiter")); msg.Subject = "Mail from Azure and MailJet!"; msg.Body = "This is just a simple test message!"; SmtpClient client = new SmtpClient("in.mailjet.com", 587); client.DeliveryMethod = SmtpDeliveryMethod.Network; client.EnableSsl = true; client.UseDefaultCredentials = false; client.Credentials = new System.Net.NetworkCredential("[APIKEY]", "[SECRETKEY]"); client.Send(msg);
How to Send Email Using SendGrid with Azure: https://docs.microsoft.com/en-us/azure/app-service-web/sendgrid-dotnet-how-to-send-email
Using SendGrid’s C# Library: https://sendgrid.com/docs/Integrate/Code_Examples/v3_Mail/csharp.html
MailJet lands on Microsoft Azure: https://www.mailjet.com/azure
Mailjet and C Sharp Code sample: https://www.mailjet.com/docs/code/c/c-sharp