.NET Core App Sending An Email Using SendGrid

In this article, we are going to learn how to send an email by using SendGrid API in a .NET Core application.

Introduction

SendGrid is a cloud-based email service that provides you reliable transactional email delivery, scalability, and real-time analytics along with flexible APIs that make custom integration easy. Following are the features of SendGrid.

  1. Industry-leading Deliverability
  2. Scalable Infrastructure
  3. Real-time, Customizable Analytics
  4. Global Support.

.NET Core App Sending An Email Using Sendgrid 

Prerequisites
  1. Visual Studio 2017 Community Edition
  2. Sendgrid free Account
  3. Any valid email Account

Create SendGrid API

Step 1

Log into the SendGrid site https://sendgrid.com/ and create a free trial account that is more than enough to explore things in SendGrid API. Once you are logged in the SendGrid portal, click on the guide link button in the right side corner.

Step 2

Following that, click on the Start button under “Integrate using our Web API or SMTP relay link” tab.
 
.NET Core App Sending An Email Using Sendgrid 

Step 3

Next, in the Overview blade, we can choose two options; i.e. Web API or SMTP Relay. For demo purposes, I am using WebAPI option. Then, click on the Choose button.
 
.NET Core App Sending An Email Using Sendgrid 

Step 4

Choose the language you want to use. SendGrid supports many famous languages, such as - Node.js, Python, PHP, Java, Ruby, Go, C#, cURL. For this demo, we are selecting C# as the language and then clicking on the "Choose" button.
 
.NET Core App Sending An Email Using Sendgrid 

Step 5

Make sure our library requires .NET version 4.5.2. and create a new API key for integrating SendGrid to our application. Do not share the API key with anyone. Enter the key name and click on the "Create" button. Copy the below C# code and integrate to your app for sending an email.
  1. using SendGrid;  
  2. using SendGrid.Helpers.Mail;  
  3. using System;  
  4. using System.Threading.Tasks;  
  5.   
  6. namespace Example  
  7. {  
  8.     internal class Example  
  9.     {  
  10.         private static void Main()  
  11.         {  
  12.             Execute().Wait();  
  13.         }  
  14.   
  15.         static async Task Execute()  
  16.         {  
  17.             var apiKey = Environment.GetEnvironmentVariable("NAME_OF_THE_ENVIRONMENT_VARIABLE_FOR_YOUR_SENDGRID_KEY");  
  18.             var client = new SendGridClient(apiKey);  
  19.             var from = new EmailAddress("test@example.com""Example User");  
  20.             var subject = "Sending with SendGrid is Fun";  
  21.             var to = new EmailAddress("test@example.com""Example User");  
  22.             var plainTextContent = "and easy to do anywhere, even with C#";  
  23.             var htmlContent = "<strong>and easy to do anywhere, even with C#</strong>";  
  24.             var msg = MailHelper.CreateSingleEmail(from, to, subject, plainTextContent, htmlContent);  
  25.             var response = await client.SendEmailAsync(msg);  
  26.         }  
  27.     }  
  28. }  
.NET Core App Sending An Email Using Sendgrid
 

Integrate API in the .NET Core app

Step 6

Click on the File menu and then select web, then select ASP.NET Core web application project. Click the OK button. Next, we will be selecting the project template. In this demo, we will select web application with MVC template.
 
.NET Core App Sending An Email Using Sendgrid 

Step 7

In View >> Home folder, right click and create a new View with a name as “SendgridEmail” and click on "Add" button. Right-click on the project, i.e., “WebApplication1” and click on "Manage NuGet Packages". In the Browse section, look for “Sendgrid” packages and click "Install".
 
.NET Core App Sending An Email Using Sendgrid
 
.NET Core App Sending An Email Using Sendgrid 

Step 8

Create a model class “Emailmodel” and design the View page for sending an email. Hit Control+ F5 to run the application.

Emailmodel.cs

  1. public class Emailmodel  
  2.     {  
  3.         public string From { getset; }  
  4.         public string To { getset; }  
  5.         public string Subject { getset; }  
  6.         public string Body { getset; }  
  7.     }  

SendgridEmail.cshtml

  1. @model Emailmodel  
  2. @{  
  3.     ViewData["Title"] = "SendgridEmail";  
  4. }  
  5.   
  6. <h2>Sendgrid Email Api Inetegration</h2>  
  7. <form asp-action="SendgridEmailSubmit" asp-controller="Home" method="post">  
  8.     <table style="padding:20px;" >  
  9.         <tr >  
  10.             <td >From</td>  
  11.             <td>  <input asp-for="From" /></td>  
  12.             <td><br /></td>  
  13.         </tr>  
  14.         <tr>  
  15.             <td>To</td>  
  16.             <td>  <input asp-for="To" /></td>  
  17.         </tr>  
  18.         <tr>  
  19.             <td>Subject</td>  
  20.             <td>  <input asp-for="Subject"  /></td>  
  21.         </tr>  
  22.         <tr>  
  23.             <td>  
  24.                 Body  
  25.             </td>  
  26.             <td>  <input asp-for="Body" /></td>  
  27.         </tr>  
  28.         <tr>  
  29.             <td></td>  
  30.             <td><input type="submit" value="Send Email"/></td>  
  31.         </tr>  
  32.     </table>  
  33. </form>  

HomeController.cs

  1. [HttpPost]  
  2.         public async Task<IActionResult> SendgridEmailSubmit(Emailmodel emailmodel)  
  3.         {  
  4.             ViewData["Message"] = "Email Sent!!!...";  
  5.             Example emailexample = new Example();  
  6.             await emailexample.Execute(emailmodel.From, emailmodel.To, emailmodel.Subject, emailmodel.Body  
  7.                 , emailmodel.Body);  
  8.   
  9.             return View("SendgridEmail");  
  10.         }  

.NET Core App Sending An Email Using Sendgrid

 

Conclusion

In this demo, we learned how to integrate SendGrid API in the .NET Core application. Please post your queries in the comments section if you are facing any issues. Thanks for your time.