Table of Contents
A few days ago we’ve published a guide explaining how to send email messages via SMTP with MailKit implementing a custom MailKitEmailSender class based upon the ASP.NET Core built-in IEmailSender interface; few days later, we’ve added another guide explaining how to use that class to send transactional e-mail messages using SendInBlue.com, an affordable all-in-one SAAS-based platform that offers transactional e-mail services.
However, MailKit is not the only mail-client library that we can use to implement the IEMailSender interface: in this post, which will be the third of that series, we’ll see how we can implement it using the Twilio SendGrid .NET Core package, which can be used to send transactional e-mail using the Twilio SendGrid services and their API.
For those who don’t know what we’re talking about, Twilio SendGrid is a all-in-one SAAS-based platform that offers transactional services such as Email, SMS, Chat (and more) through a versatile set of APIs that can be invoked and consumed from external websites or web services; its feature set makes it rather similar to SendInBlue, Mailgun, Amazon SES and the likes, at least when it comes to e-mail sending capabilities.
1. Create an Account
The first thing we need to do is to create an account to Twilio SendGrid: doing it is extremely easy, we just have to fill up the required fields, as shown in the following screenshot:
Once done, we need to select one of the various pricing plan available: the best thing we can do, at least for the time being, is to select the FREE plan, which allows to send up to 100 e-mail messages per day; that’s more than enough for testing out our implementation.
In order to complete the registration phase we’ll need to confirm our given e-mail address: as soon as we do that, we can move to the next steps.
2. Choose between Web API and SMTP Relay
Right after completing the registration phase and performing the login we’ll be brought to the SendGrid‘s main dashboard panel, which allows us to monitor our recent activities as well as managing our campaigns, templates, settings, and more.
Once there, select the Email API option from the left-side menu to access the Integration guide option, which is what we’re going to use SendGrid for.
From the Integration guide section, we’ll be prompted to select between two available choices:
- Web API, for direct app integration using the SendGrid official packages for the various supported languages (PHP, Ruby, Java, C# and more).
- SMTP Relay, which can be used to retrieve the SendGrid SMTP data and implement it using a third-party e-mail sender interface such as MailKit.
If you want to take the “easy” route, just select the SMTP Relay option, fetch the SMTP data and then read our How to send e-mail message with .NET Core and any SMTP server using MailKit guide instead of going further with this post: however, this is not the approach that we would recommended, at least for SendGrid. If you want to have more control on your e-mail sending process, then the Web API is definitely the way to go; also, if we look at the green RECOMMENDED flag above such option, we can see how even the SendGrid website agree with us.
That said, if you agree with us and SendGrid, select the Web API option and keep reading.
3. Get the Web API keys
After selecting the Web API option we’ll access to the SendGrid’s Web API setup guide, which is among the best guides we’ve ever seen (no kidding): you can retrieve the Web API Key, read all the required info and even get a working code sample to get the stuff done.
Here’s a list of what we need to do here:
- Create a new API key and copy its value to a safe place (we’re going to use it in a few while)
- Set the API key in a dedicated Environment Variable (following SendGrid’s instruction) If you don’t want to follow the Environment Variable approach, you can either:
- Use the Web API key in plain-text within your app’s code (not recommended)
- Use the Visual Studio’s User Secret approach (as explained in our How to securely store password in Visual Studio using User Secrets guide)
- Install the SendGrid NuGet package on the ASP.NET Core project where we want to send the emails from (here’s the official link)
- Copy the “send your first e-mail” sample code that can be found by the end of the page and paste it within the ASP.NET Core project’s app code.
- Modify the code accordingly (with the choosen Envronment Variable, User Secret or plain text.
Here’s how the SendGrid’s guide sample code looks like: the line of code where we need to put the API Key value has been marked.
// using SendGrid's C# Library
internal class Example
private static void Main()
static async Task Execute()
var client = new SendGridClient(apiKey);
var from = new EmailAddress("firstname.lastname@example.org", "Example User");
var subject = "Sending with SendGrid is Fun";
var to = new EmailAddress("email@example.com", "Example User");
var plainTextContent = "and easy to do anywhere, even with C#";
var htmlContent = "<strong>and easy to do anywhere, even with C#</strong>";
var msg = MailHelper.CreateSingleEmail(from, to, subject, plainTextContent, htmlContent);
var response = await client.SendEmailAsync(msg);
That’s it, at least for now: we hope that this simple guide will help other ASP.NET Core developers in configuring their custom EmailSender using the SendGrid service.