Send Mails with C# in Azure

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#

SendGrid is configured and up and running – so it’s time to send a few mails! There is a NuGet package available that we can use to send our mails:

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, "");


Create and configure MailJet service

MailJet is also available as an Azure service, so it’s really simple to create the MailJet service. Just go to the Azure portal – add – and search for the MailJet email service:

In the following screen, we can see the pricing options and we can also see, that the free version offers 25k Mail (800 per day) per month. The service is only creatable in the region “West US”:

After the creation, we have to activate the account. Navigate to the mailjet resource in Azure and copy the activation link which you can find in the properties:

Open this link and after that, you can configure the mailjet service. Go to the API keys (“Master API key & Sub API key management”) and copy the master API key and Secret key:

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("", 587);
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.EnableSsl = true;
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential("[APIKEY]", "[SECRETKEY]");

Additional information

How to Send Email Using SendGrid with Azure:
Using SendGrid’s C# Library:
MailJet lands on Microsoft Azure:
Mailjet and C Sharp Code sample:


No responses yet

    Leave a Reply

    Your email address will not be published. Required fields are marked *