Send Mail Using SendGrid In .NET Core

Sending mail in .NET Core is not easy as .NET Core does not support SMTPClient class. In .NET Core, there is System.NET.Mail assembly where the SMTPClient is missing, that we use while sending mails in a full framework .NET Application.

So, this article explains how we can send mail in .NET Core, using SendGrid.
 
What is SendGrid?

SendGrid actually does more than sending emails. According to SendGrid, it is a "Marketing & Transactional Email" Service. SendGrid API integrates via email API to ensure the delivery of most important emails.

Site Link - https://sendgrid.com 

Requirements
  1. Visual Studio 2015
  2. .NET Core
  3. Azure subscription to create SendGrid (I am using Azure to integrate SendGrid else you can create a trial account on SendGrid Site)
 
Advantages
  1. Easy to use with integrated API for .NET Core.
  2. Cloud based.
  3. Azure Integration
Before starting to code, we need to send up the SendGrid account.

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



  2. Type "SendGrid Email Delivery" to find SendGrid.



  3. Click "Create".



  4. Fill in the required settings in SendGrid as per your subscription.



  5. Select the "Free Plan" from the SendGrid Pricing Tier or as per your requirement.



  6. Click "Create" and "Purchase" after all the information has been provided.



  7. After the resource is created, a username, password, and SMTP Server details will be provided. Note that down.



  8. We can send emails using username and password but the best way is to create an API key. So, click on "Manage" to go to SendGrid website and create an API key which we will use while sending an email. 



  9. After you save, the API Key will be generated. Please note it down.

  10. Now, let's come to the coding part. Create a .NET Core Console application and add the below code.
    1. using SendGrid;  
    2. using System;  
    3. using System.Collections.Generic;  
    4. using System.Linq;  
    5. using System.Net.Mail;  
    6. using System.Threading.Tasks;  
    7. namespace SendMail {  
    8.     public class Program {  
    9.         public static void Main(string[] args) {  
    10.             var message = new SendGrid.SendGridMessage();  
    11.             message.From = new MailAddress("[email protected]""Julian");  
    12.             message.Subject = "Sending Email using SendGrid";  
    13.             message.AddTo("[email protected]");  
    14.             message.Html = "<p>Hello from Julian</p>";  
    15.             var client = new Web("YOUR API KEY");  
    16.             client.DeliverAsync(message).Wait();  
    17.         }  
    18.     }  
    19. }  
    The project.json
    1. {  
    2.     "version""1.0.0-*",  
    3.     "buildOptions": {  
    4.         "emitEntryPoint"true  
    5.     },  
    6.     "dependencies": {  
    7.         "Microsoft.NETCore.App": {  
    8.             "type""platform",  
    9.             "version""1.0.1"  
    10.         },  
    11.         "SendGrid.NetCore""1.0.0-rc2-00004"  
    12.     },  
    13.     "frameworks": {  
    14.         "netcoreapp1.0": {  
    15.             "imports": ["dnxcore50""portable-net45+win8"]  
    16.         }  
    17.     }  
    18. }  
Now, run the application and check for the sent mail.
 
 
Thanks a lot for giving your valuable time reading this article !!


Similar Articles