Send Email in Azure using SendGrid

In this tutorial, we'll learn to send emails in Azure using SendGrid. SendGrid can also be used to send bulk emails in Azure.

Use of SendGrid

 
SendGrid is used to deliver your transactional and business emails through Cloud system. It also will deliver a thousand of email in a minute. The SendGrid has a attachment option to send any document with email.
 
Step 1
 
Open the Windows azure account with your credential. Go to Search box in top of the page and Search Send Grid in search box.
 
Send Email In Azure Send Grid
 
Step 2
 
It will redirect into a new page called Send Grid Account.
 
There you can Add new SendGrid Account.
 
Send Email In Azure Send Grid
 
Step 3
 
Enter the information on required fields. As you can see the Name, Password, Confirm Password and Other required fields.
 
If you are paid user, then click the “Paid-as-go” option in subscription dropdown box.
 
Resource Group: It means location. We should create a new resource group to create a new SendGrid.
 
Send Email In Azure Send Grid
 
Step 4
 
Select the pricing tire. It will send an email based on setting Pricing Tier.
 
Send Email In Azure Send Grid
 
Step 5
 
We selected “Free” account. We have three options in Pricing Tire,
  1. Silver
  2. Bronze
  3. Free
Each type has different type of usage as you can see in the below screenshot.
 
Send Email In Azure Send Grid
 
Step 6
 
Click on Contact Information and fill out the required fields on the page. This will verify your “To Address”.
 
Main purpose of this information is that where the email is come from and name of the person/organization.
 
Enter the required information on Contact Information.
 
Send Email In Azure Send Grid
 
Step 7
 
Read the legal terms and click and then confirm the check box and click on Create button.
 
Send Email In Azure Send Grid
 
Complete the process then click on Create button.
 
Step 8
 
The deployment process will take some time to finish.
 
Send Email In Azure Send Grid
 
Step 9
 
Refresh the account.
 
Send Email In Azure Send Grid
 
Step 10
 
Click on Manage to see more details about created SendGrid.
 
Send Email In Azure Send Grid
 
Step 11
 
The SendGrid will send you the confirmation mail to respective email address to authorize.
 
Click on Send Confirmation Email and check your personal email account.
 
Send Email In Azure Send Grid
 
Step 12
 
After the confirmation, it will redirect to Key settings page. Click on Settings and go to API Keys on left menu bar.
 
Copy the API key by following the steps.
 
Send Email In Azure Send Grid
 
Click on Create API key button and you will see the key on the window.
 
Send Email In Azure Send Grid
 

.NET Code development

 
Use the following steps for dot net implementation. I have pasted the copied key on my app config file.
 
Copy the whole working code and paste on your project.
  1. "Key": {  
  2.     "FromAddress""test@gmail.com",  
  3.     "SubjectName""Mail from Azure and SendGrid",  
  4.     "AzureSendGridKey""SG.x4W9xmHzRueW7RuEwdeQnw._lyBezACbfJmqLsM1lbQB………<<my key>>"  
  5.   }  
Step 14
 
Write the code inside your API.
  1. public async Task SendEmailAsync(string ToAddress, string subject, string messages)  
  2.         {  
  3.             string fromAddress = Configuration["Key:FromAddress"];  
  4.             string SubjectName = Configuration["Key:SubjectName"];  
  5.             string AzureSendGridKey = Configuration["Key:AzureSendGridKey"];  
  6.             var client = new SendGridClient(AzureSendGridKey);  
  7.             var msg = new SendGridMessage();  
  8.   
  9.             msg.SetFrom(new EmailAddress(fromAddress, SubjectName));  
  10.   
  11.             var recipients = new List<EmailAddress>  
  12.                 {  
  13.                     new EmailAddress(ToAddress)  
  14.                 };  
  15.             msg.AddTos(recipients);  
  16.   
  17.             msg.SetSubject(subject);  
  18.   
  19.             //msg.AddContent(MimeType.Text, messages);  
  20.             msg.AddContent(MimeType.Html, messages);  
  21.             var response = await client.SendEmailAsync(msg);  
  22.         }  
Build the application and run it.
 
Step 15
 
You will get the notification email like the below.
 
Send Email In Azure Send Grid
 
Happy coding…