Sending Emails From An Application Using SendGrid Email Delivery Service

This article explains the process of sending an email using SendGrid Email Service with Microsoft Azure. SendGrid is a Cloud based Email Service, which provides reliable transactional Email delivery, scalability and real-time analytics along with flexible APIs, which makes custom integration easy.

This article will cover

  1. Steps to create a SendGrid account.
  2. Steps to generate API key for SendGrid account.
  3. Using SendGrid in an Application to send the Emails.
Creating SendGrid Account

Log in to Azure portal, create a SendGrid resource.

Click New icon.

Azure

Search for Sendgrid Email Delivery.

Azure

Select SendGrid Email delivery.

Azure

Click Create button.

Azure

Fields for entering SendGrid account details should appear.

Azure

Give a name for your SendGrid account, provide a password for your SendGrid account.

Select the appropriate price tier for your SendGrid account, which is based on your Application usage. For now, let’s create an account with free plan, which can send upto 25000 mail per month.

Azure

Provide contact information, which will be used by Azure to communicate SendGrid account related stuff.

Azure

Now, read legal terms & click Purchase. Finally, click Create. It will create a SendGrid account for you.

Azure

Now, your SendGrid account creates. We need to generate API keys to use it or sending Emails.

Generation of API key

Go to All resources.

Azure

Search for “Sendgrid”.

Azure

The newly created SendGrid account should appear. Click it.

Azure

After selecting the resource group which you have created, click Manage button. This will redirect you to https://app.sendgrid.com

Azure

In  the left menu, expand settings section, click API Keys under settings.

Azure

Click Create API key.

Azure

It will ask for API key permission. Choose the appropriate permission.

Azure

On Clicking Create & View, a new API key will be generated with the specified permissions.

Azure

Using SendGrid to send Emails 
  1. using System;  
  2. using System.IO;  
  3. using System.Net.Mail;  
  4.       
  5. namespace Services  
  6. {  
  7.     public class EmailService  
  8.     {  
  9.         public bool SendEmail(EmailType emailType, object emailDetail)  
  10.         {  
  11.             var isEmailSent = false;  
  12.             try  
  13.             {  
  14.                 var email = new EmailDTO  
  15.                 {  
  16.                     Body = "This is email body",  
  17.                     Subject = "Email Subject",  
  18.                     IsBodyHtml = true,  
  19.                     From = "EmailFrom@gmail.com",  
  20.                     To = "EmailTo@gmail.com"  
  21.                 }  
  22.                 isEmailSent = SendEmail(email);  
  23.             }  
  24.             catch (Exception ex)  
  25.             {  
  26.                 //logger.LogError(ex);  
  27.             }  
  28.   
  29.             return isEmailSent;  
  30.         }  
  31.   
  32.         private bool SendEmail(EmailDTO emailDetails)  
  33.         {  
  34.             try  
  35.             {  
  36.                 //SendGrid_ApiKey we got while creating send grid account, ideally we should keep it in web.config file  
  37.                 var apiKey = "dsafags-gsdgsg-5756887547-cnhfgjg";  
  38.   
  39.   
  40.                 // Create an Web transport for sending email.  
  41.                 var transportWeb = new SendGrid.Web(apiKey);  
  42.                 var myMessage = new SendGrid.SendGridMessage();  
  43.                 myMessage.AddTo(emailDetails.To);  
  44.                 myMessage.From = new MailAddress(emailDetails.From, "HR Driver Referral");  
  45.                 myMessage.Subject = emailDetails.Subject;  
  46.                 myMessage.Html = emailDetails.Body;  
  47.                 transportWeb.DeliverAsync(myMessage);  
  48.                 return true;  
  49.             }  
  50.             catch (Exception ex)  
  51.             {  
  52.                 //logger.LogError(ex);  
  53.                 return false;  
  54.             }  
  55.         }  
  56.   
  57.     }