Send Mail Using SendGrid

What is SendGrid? 
 
Site Link - https://sendgrid.com

SendGrid API helps in delivering your important emails over SMPT or HTTP.  It does have its own server which is on the cloud and helps you integrate their durable service with less effort.

Requirements
  1. Visual Studio 2015.
    1. <packages>  
    2.     <package id="Newtonsoft.Json" version="9.0.1" targetFramework="net45" />  
    3.     <package id="Sendgrid" version="9.9.0" targetFramework="net452" />  
    4.     <package id="System.Net.Http" version="4.3.3" targetFramework="net45" />   
    5. </packages>  
  2. Azure subscription to create SendGrid.
SendGrid Benefits

It provides 24/7 customer support. It also has click tracking mechanism, Spam Reports, and if your mail gets bounced back, SenGrid tracks that as well. You can set daily/monthly mailing functionality over SendGrid API just to track how many emails went well or bounced back. If ever SendGrid API is down, it puts those emails in queues and sends it again once it is up.

So, it works on customer satisfaction with huge support.
 
Working with SendGrid

We need to create a SendGrid account.

Log in to your Azure account and add a new SendGrid Email Delivery Resource.
  1. Click "New" to add a new resource.

    Azure

  2. Perform the configuration like in the below image.

    Azure

  3. Fill contact information.

    Azure

  4. Accept the licence under the "Legal Terms" tab.

    Azure

  5. Once you have successfully added SendGrid Resources, you will see a screen like the below one. Click on the "Manage" tab. It will redirect to the SendGrid portal.

    Azure

  6. SendGrid Portal

    Azure

  7. Here, you can configure/create an API key. 

    Azure

  8. Please save your key or take a screenshot of the same, otherwise, you will unable to find the key on a later stage.

  9. Let's create our template for the mail.

    Azure

  10. Edit your template like below using different modules. These templates are compatible with your mobile devices as well.

    Azure

  11. Save this Template ID.

    Azure
And at last, you can add the below logic in your .NET application.
  1. static async Task sendEmailUsingSendGrid(string apiKey) {  
  2.     var client = new SendGridClient(apiKey);  
  3.     ////send an email message using the SendGrid Web API with a console application.  
  4.     var msgs = new SendGridMessage() {  
  5.         From = new EmailAddress("[email protected]""Pankaj Sapkal"),  
  6.             Subject = "Subject line",  
  7.             TemplateId = "fb09a5fb-8bc3-4183-b648-dc6d48axxxxx",  
  8.             ////If you have html ready and dont want to use Template's   
  9.             //PlainTextContent = "Hello, Email!",  
  10.             //HtmlContent = "<strong>Hello, Email!</strong>",  
  11.     };  
  12.     //if you have multiple reciepents to send mail  
  13.     msgs.AddTo(new EmailAddress("[email protected]""Pankaj Sapkal"));  
  14.     //If you have attachment  
  15.     var attach = new Attachment();  
  16.     //attach.Content = Convert.ToBase64String("rawValues");  
  17.     attach.Type = "image/png";  
  18.     attach.Filename = "hulk.png";  
  19.     attach.Disposition = "inline";  
  20.     attach.ContentId = "hulk2";  
  21.     //msgs.AddAttachment(attach.Filename, attach.Content, attach.Type, attach.Disposition, attach.ContentId);  
  22.     //Set footer as per your requirement  
  23.     msgs.SetFooterSetting(true"<strong>Regards,</strong><b> Pankaj Sapkal""Pankaj");  
  24.     //Tracking (Appends an invisible image to HTML emails to track emails that have been opened)  
  25.     //msgs.SetClickTracking(true, true);  
  26.     var responses = await client.SendEmailAsync(msgs);  
  27. }  
Now, run the application and check for the sent mail.

Thanks a lot for giving your valuable time reading this article.