Last week I’ve to work on a pet project with a colleague and we were talking about the notification emails that we are going to use in the application, automatically I thought about azure and found the perfect service for this task. SendGrid is a cloud-based email service that provides reliable transactional email delivery, scalability, and much more.
So what implies the use of SendGrid? Does it Have Any cost?
Azure customers can unlock 25,000 free emails each month. These 25,000 free monthly emails will give you access to advanced reporting and analytics and all APIs (Web, SMTP, Event, Parse, and more)
- Sign up for a SendGrid account
For that task, you need to sign in to the Azure portal and create a SendGrid resource.
You can also choose your subscription, resource group, and pricing tier. After filling the form with all your contact information, you will see a deployment success pop-up and your account listed.
There are different pricing tier based on your need, for this sample, we’ll use the free plan:
Once you complete all the forms and deploy your resource, you need to verify your account and provide an API key to SendGrid. For that click on the manage, button to access the SendGrid portal. Into the settings sections of your resource, you’ll find the key to register on your SendGrid account.
At the Sendgrid portal find the setting section to get the API key:
All you have to do is select the API key, create a new one and save it for later you’ll gonna need it.
So How to send email from ASP.NET Core API?
The first thing you need to do is to add the SendGrid API key on your appsetting.json file like in this example:
Second, we need to add some code in the Startup.cs file on the .NET Core API project, to access your SendGrid API key to the appsettings.json. All you need to do is add the IConfiguration dependency to the startup file content.
And It will look like this:
Looking forward to the future needs we are going to create a Service, that we can call wherever we need it. It’s going to make it easier for us to reusability of code. We are not going to deep in details about the interface and the service, that could be a topic for another day :p
Before we create the service, we are going to create an interface where we are going to define the signatures methods of our SendEmailService:
Once we created the interface, we are going to implement this interface in the service:
For now, we are just focusing on the Create account email, as you may notice I’m receiving a model for the creation of the email:
We are going to need it to send all the necessary information for the email, the most relevant things of the create account email are :
- The API key: This is based on the API key that you already configured on your appsettings.cs
- The var message that runs the task to create a single Email, you have many options with SendGrid, you are free to experiment with the others available, for this tutorial purpose I’m just using this one :)
- The last one is the response var, just commit the email that you created.
This is how you test the endpoint:
This is the result:
Thanks for reading, hoping this will be useful for you. ＠＾▽＾＠ (✿ ♥‿♥)(♥ω♥*)