ASP.NET Core C# – Send email messages with SendGrid API How to send transactional e-mail messages from a ASP.NET Core C# web application using Twilio SendGrid API

ASP.NET Core C# - Send email messages with SendGrid API

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.

DISCLAIMER: This website is not affiliated with Twilio SendGrid; this article represents the free opinion of the author and has not been commissioned or sponsored in any way.

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:

ASP.NET Core C# - Send email messages with SendGrid API

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.

ASP.NET Core C# - Send email messages with SendGrid API

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.

ASP.NET Core C# - Send email messages with SendGrid API

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.

ASP.NET Core C# - Send email messages with SendGrid API

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.

ASP.NET Core C# - Send email messages with SendGrid API

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:
  • 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 VariableUser 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.

IMPORTANT: the above sample code has been published for reference purposes only. Instead of getting it from here, fetch the latest version from the official SendGrid Web API C# integration guide.

Conclusions

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.

About Ryan

IT Project Manager, Web Interface Architect and Lead Developer for many high-traffic web sites & services hosted in Italy and Europe. Since 2010 it's also a lead designer for many App and games for Android, iOS and Windows Phone mobile devices for a number of italian companies. Microsoft MVP for Development Technologies since 2018.

View all posts by Ryan

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.