Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon

How to send email Notifications using SendGrid

Save for later
  • 6 min read
  • 10 Aug 2018

article-image

SendGrid is one of the popular services that allow the audience to send emails for different purposes. In today’s tutorial we will explore to:

  • Create SendGrid account
  • Generate SendGrid API Key
  • Configure SendGrid API key with Azure function app

Send an email notification to the website administrator


Here, we will learn how to create a SendGrid output binding and send an email notification to the administrator with a static content. In general there would be only administrators so we will be hard coding the email address of the administrator in the To address field of the SendGrid output binding

Getting ready

  1. Create a SendGrid account API Key from the Azure Management Portal.
  2. Generate an API Key from the SendGrid Portal.

Create a SendGrid account

  1. Navigate to Azure Management Portal and create a SendGrid Email Delivery account by searching for the same in the Marketplace shown as follows:


send-email-notifications-using-sendgrid-img-0

  1. In the SendGrid Email Delivery blade, click on Create button to navigate to the Create a new SendGrid Account. Please select Free tier in the Pricing tier and provide all other details and click on the Create button shown as follows:


send-email-notifications-using-sendgrid-img-1

  1. Once the account is created successfully, navigate to the SendGrid account. You can use the search box available in the top which is shown as follows:


send-email-notifications-using-sendgrid-img-2

  1. Navigate to the Settings, choose configurations and grab the username and SmtpServer from the Configurations blade.

Generate SendGrid API key

  1. In order to utilize SendGrid account by the Azure Functions runtime, we need to provide the SendGrid API key as input to the Azure Functions. You can generate an API Key from the SendGrid portal. Let's navigate to the SendGrid portal by clicking on the Manage button in the Essentials blade of the SendGrid account shown as follows:


send-email-notifications-using-sendgrid-img-3

  1. In the SendGrid portal, click on the API Keys under Settings section of the Left hand side menu shown as follows:


send-email-notifications-using-sendgrid-img-4

  1. In the API Keys page, click on Create API Key shown as follows:


send-email-notifications-using-sendgrid-img-5

  1. In the Create API Key popup, provide a name and choose the API Key Permissions and click on Create & View button.

  1. After a moment you will be able to see the API key. Click on the key to copy the same to the clipboard:


send-email-notifications-using-sendgrid-img-6

Configure SendGrid API key with Azure Function app

  1. Create a new app setting in the Azure Function app by navigating to the Application Settings blade under the Platform features section of the function app shown as follows:


send-email-notifications-using-sendgrid-img-7

  1. Click on Save button after adding the app settings in the preceding step.

How to do it...

  1. Navigate to the Integrate tab of the RegisterUser function and click on New Output button to add a new output binding.
  2. Choose the SendGrid output binding and click on Select button to add the binding.
  3. Please provide the following parameters in the SendGrid output binding:
    1. Message parameter name - leave the default value - message. We will be using this parameter in the run method in a moment.
    2. Unlock access to the largest independent learning library in Tech for FREE!
      Get unlimited access to 7500+ expert-authored eBooks and video courses covering every tech area you can think of.
      Renews at $19.99/month. Cancel anytime
    3. SendGrid API key: Please provide the app settings key that you have created in the application settings.
    4. To address: Please provide the email address of the administrator.
    5. From address: Please provide the email address from where you would like to send the email. In general, it would be kind of donotreply@example.com.
    6. Message subject: Please provide the subject that you would like to have in the email subject.
    7. Message Text: Please provide the email body text that you would like to have in the email body.

  4. Below is how the SendGrid output binding should look like after providing all the fields: send-email-notifications-using-sendgrid-img-8
  5. Once you review the values, click on Save to save the changes.
  6. Navigate to Run method and make the following changes:
    1. Add a new reference for SendGrid and also the namespace
    2. Add a new out parameter message of type Mail.
    3. Create an object of type Mail.

  7. Following is the complete code of the Run method:
    #r  "Microsoft.WindowsAzure.Storage"
    
    #r "SendGrid"
    
    
    
    using  System.Net;
    
    using SendGrid.Helpers.Mail;
    
    using  Microsoft.WindowsAzure.Storage.Table;
    
    using  Newtonsoft.Json;
    
    public  static  void  Run(HttpRequestMessage  req, TraceWriter  log,
    
    CloudTable  objUserProfileTable,
    
    out  string  objUserProfileQueueItem,
    
    out Mail message
    
    )
    
    {
    
    var  inputs  =  req.Content.ReadAsStringAsync().Result;
    
    dynamic  inputJson  =  JsonConvert.DeserializeObject<dynamic>(inputs);
    
    string  firstname=  inputJson.firstname;
    
    string  lastname=inputJson.lastname;
    
    string  profilePicUrl  =  inputJson.ProfilePicUrl;
    
    objUserProfileQueueItem  =  profilePicUrl;
    
    UserProfile  objUserProfile  =  new  UserProfile(firstname,  lastname); TableOperation  objTblOperationInsert  =
    
    TableOperation.Insert(objUserProfile);
    
    objUserProfileTable.Execute(objTblOperationInsert);
    
    message = new Mail();
    
    }
    
    public  class  UserProfile  :  TableEntity
    
    {
    
    public  UserProfile(string  lastName,  string  firstname,string profilePicUrl)
    
    {
    
    this.PartitionKey  =  "p1";
    
    this.RowKey  =  Guid.NewGuid().ToString();;
    
    this.FirstName  =  firstName; this.LastName  =  lastName; this.ProfilePicUrl  =  profilePicUrl;
    
    }
    
    public  UserProfile()  {  }
    
    public  string  FirstName  {  get;  set;  } public  string  LastName  {  get;  set;  } public  string  ProfilePicUrl  {get;  set;}
    
    }

  8. Now, let's test the functionality of sending the email by navigating to the RegisterUser function and submit a request with the some test values:
    {
    "firstname":  "Bill", "lastname":  "Gates",
    "ProfilePicUrl":"https://upload.wikimedia.org/wikipedia/commons/thumb/1/19/ Bill_Gates_June_2015.jpg/220px-Bill_Gates_June_2015.jpg"
    }

How it works...


The aim here is to send a notification via email to an administrator updating that a new registration got created successfully.

We have used the one of the Azure Function experimental templates named SendGrid as a SMTP server for sending the emails by hard coding the following properties in the SendGrid output bindings:

  • From email address
  • To email address
  • Subject of the email
  • Body of the email


SendGrid output bindings will use the API key provided in the app settings to invoke the required APIs of the SendGrid library for sending the emails.

To summarize, we learnt about sending an email notification using SendGrid service.

[box type="shadow" align="" class="" width=""]This article is an excerpt from the book, Azure Serverless Computing Cookbook, written by Praveen Kumar Sriram. It contains over 50 recipes to help you build applications hosted on Serverless architecture using Azure Functions.[/box]

5 reasons why your business should adopt cloud computing