Share via


Configuring SendGrid with ASP.Net Core

Overview

BrowserPower is a simple game leveraging the power of Azure Cognitive Services - Vision API.  The premise is simple, given clues, find the best image from the web that matches it.  The clues might be a herd of sheep or a particular celebrity eating a cheeseburger.  The game itself is not as important as the technology used to create it.
This wiki highlights SendGrid which was used as a mail server for sending account related emails.

Background

The game grew from the wiki article: Getting started with Cognitive Services - Vision.  Playing the game is simple: An internet search game using Azure Cognitive Services.  The project has been uploaded to MSDN as it is an interesting illustration of building an ASP.Net Core Azure Web App from the ASP.NET Core Web Application template and implements several of steps in the ASP.NET Core Tutorials.

The source can be located here.

SendGrid - Email Delivery System

SendGrid offers a powerful and full-featured solution to email delivery.  The service is highly scalable and extremely easy to provision in Azure. SendGrid offers several pricing plans including one ideal for a for fun solution like BrowserPower: FREE.  With the free plan, the service will provision up to 25,000 emails/month.  For scenarios of providing email for registration and forgotten password retrieval, BrowserPower should be well under that limit!

Provisioning SendGrid in Azure

A SendGrid account can be provisioned within Azure.  When adding a new account, the options of supplying a name, subscription and resource group are familiar:

 

And the pricing plans are clearly explained:

SendGrid API Keys

After the SendGrid account has been created, there is one additional step required.  SendGrid has an external website for maintaining and monitoring the SendGrid account that can be launched from the Azure Portal as shown below:

Under settings, it is necessary to create an API Key that will be used by the application to send messages to the SendGrid API.

Adding SendGrid to ASP.Net Core solution

The first step is to reference the SendGrid package in the project.json file.  For the BrowserPower project, version 9.8 was used:

Once the package has been restored, an implementation of the IEmailSender interface can be made.  

public class  EmailSender : IEmailSender
{
    public EmailSender(IOptions<AuthMessageSenderOptions> optionsAccessor)
    {
        Options = optionsAccessor.Value;
    }
 
    public AuthMessageSenderOptions Options { get; } //set only via Secret Manager
 
    public Task SendEmailAsync(string email, string subject, string message)
    {
        return Execute(Options.SendGridKey, subject, message, email);
    }
 
    public Task Execute(string apiKey, string subject, string message, string email)
    {
        var client = new  SendGridClient(apiKey);
        var msg = new  SendGridMessage()
        {
            // should be a domain other than yahoo.com, outlook.com, hotmail.com, gmail.com
            From = new  EmailAddress("donotreply@somewhere.com", "IndieGamesLab"),
            Subject = subject,
            PlainTextContent = message,
            HtmlContent = message
        };
        msg.AddTo(new EmailAddress(email));
        return client.SendEmailAsync(msg);
    }
}

The new EmailSender needs to also be added to services in Startup:

Summary

BrowserPower is a nonsense application really.  Hopefully some will find it amusing.

Return to Top