Sending emails using Azure Sendgrid service

For the latest development of our mobile apps, we needed to replace the current service we use for sending out emails. The current email service is scheduled to go into retirement, leaving us with the task of replacing the email functionality on the apps. As we currently make extensive use of Azure for many of our other development services (service bus, webjobs, functions, blob and SQL storage) I thought I’d investigate to see if Azure provided an email service we could use. And sure enough it does.

The email service provided by Azure is called SendGrid. As with every other service provided by Azure, it has excellent integration with the .NET ecosystem. You configure your SendGrid service initially in your Azure portal. As part of this configuration you need to create an API key. It is this API key that you then provide your application when making SendGrid email requests in your code.

To integrate your application with Azure’s SendGrid service, you also need to download and install the Azure SendGrid nuget package. Once installed, you can start sending emails from your application.

Example code for using the Azure SendGrid email service

The following code has been taken from a very simple console application I created as a proof of concept.

Hide Copy Code

var msg = new SendGridMessage();
msg.SetFrom(new EmailAddress(joe.bloggs@company.co.uk, "Development Manager"));

var recipients = new List<EmailAddress>
{
new EmailAddress(fred.smith@company.co.uk, "Software Developer")
};
msg.AddTos(recipients);
msg.SetSubject("Please ignore - Testing the SendGrid C# Library");
msg.AddContent(MimeType.Html, "<p>Hello World!</p>");
var client = new SendGridClient("place_your_api_key_here");
var response = await client.SendEmailAsync(msg);

Console.WriteLine($"Response from SendGrid demo email: {response.StatusCode}");
Console.WriteLine("Press any key to finish.......");
Console.ReadKey();

It really is as easy as that!

With just a few lines of code you have your own email service from which to send your application’s emails. At the time of writing you get 35k emails per month for free before you incur any costs. So unless you are heavily into marketing email campaigns, this should be sufficient for most needs.

I’ve said it before, and I’ll keep saying it. Azure is one of the greatest development platforms I have had the pleasure of using. Setting up and configuring SendGrid was very easy and there is plenty of online documentation and examples.

In a future article I’ll describe how I used Razor to provide templating functionality for the emails that we send using Azure SendGrid. Until then, happy coding.

A father, cyclist, vegetarian, atheist, geek and multiple award winning technical author. Loves real ale, fine wine and good music. All round decent chap.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store