Email Sender for Asp.NET 5.0 Identity using SendGrid
How to send activation and password reset emails for Asp.NET 5.0 Identity using SendGrid.
IEmailSender
In multiple Identity scenarios, I need to send my users an email; when they create their account and need activation email, when they change the password, when there is suspicious activity on their account, etc., and hence we need a central email function to take care of this.
With Microsoft Asp.NET Identity, comes an interface called IEmailSender, with only one method to implement — SendEmailAsync. However this service is primarily intended for their default UI infrastructure, and should not be used from within our code.
For that reason, I create my own email service, and use SendGrid helper classes to call their API and send the emails.
Libraries
SendGrid provides the below helper library to integrate with their services:
By the way, this topic belongs to the series to set up Authentication and Authorisation for Asp.NET ecosystem.
- Asp.Net Core Web Api Setup
- React Single Page App Setup
- Asp.NET Identity DB Setup
- Email Sender
- Sign Up & Activation
- Check User Authentication Status
- Change Password
- Sign In
- Policy Based Access Management
- Token Refresh
- Google Authentication with React and Asp.NET API
- Microsoft Authentication with React and Asp.NET API
- Twitter Authentication with React and Asp.NET API
End Result
At the end any function that would need to send an email, would work with an interface like this:
in which the EmailRequest class looks like below. I have chosen Tuple as a simple structure to send the name and email of sender and recipients.
Implementation
The implementation is pretty simple, using MailHelper class of SendGrid. I am using one the exposed methods, CreateSingleEmailToMultipleRecipients, but the library comes with multiple methods you could leverage to send your emails.
The point that is worth highlighting is the use of AppSecrets class. It is the class that I use to organise and structure my secrets. If you’d like to read more about that, refer to Secret Management in ASP.Net 5.0 Web APIs.
Note
Configuration, plumbing and troubleshooting your software foundation take a considerable amount of time in your product development. Consider using Pellerex which is a complete foundation for your enterprise software products, providing source-included Identity and Payment functions across UI (React), API (.NET), Pipeline (Azure DevOps) and Infrastructure (Kubernetes).
Pellerex: Identity Foundation for Your Next Enterprise Software
How are you building your current software today? Build everything from scratch or use a foundation to save on development time, budget and resources? For an enterprise software MVP, which might take 8–12 months with a small team, you might indeed spend 6 months on your foundation. Things like Identity, Payment, Infrastructure, DevOps, etc. they all take time, while contributing not much to your actual product. These features are needed, but they are not your differentiators.
Pellerex does just that. It provides a foundation that save you a lot development time and effort at a fraction of the cost. It gives you source-included Identity, Payment, Infrastructure, and DevOps to build Web, Api and Mobile apps all-integrated and ready-to-go on day 1.
Check out Pellerex and talk to our team today to start building your next enterprise software fast.