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.
- Industry-leading Deliverability
- Scalable Infrastructure
- Real-time, Customizable Analytics
- Global Support.
- Visual Studio 2017 Community Edition
- Sendgrid free Account
- Any valid email Account
Create SendGrid API
Step 1
Step 2
Step 3
Step 4
Step 5
- using SendGrid;
- using SendGrid.Helpers.Mail;
- using System;
- using System.Threading.Tasks;
- namespace Example
- {
- internal class Example
- {
- private static void Main()
- {
- Execute().Wait();
- }
- static async Task Execute()
- {
- var apiKey = Environment.GetEnvironmentVariable("NAME_OF_THE_ENVIRONMENT_VARIABLE_FOR_YOUR_SENDGRID_KEY");
- var client = new SendGridClient(apiKey);
- var from = new EmailAddress("[email protected]", "Example User");
- var subject = "Sending with SendGrid is Fun";
- var to = new EmailAddress("[email protected]", "Example User");
- var plainTextContent = "and easy to do anywhere, even with C#";
- var htmlContent = "<strong>and easy to do anywhere, even with C#</strong>";
- var msg = MailHelper.CreateSingleEmail(from, to, subject, plainTextContent, htmlContent);
- var response = await client.SendEmailAsync(msg);
- }
- }
- }

Integrate API in the .NET Core app
Step 6
Step 7

Step 8
Emailmodel.cs
- public class Emailmodel
- {
- public string From { get; set; }
- public string To { get; set; }
- public string Subject { get; set; }
- public string Body { get; set; }
- }
SendgridEmail.cshtml
- @model Emailmodel
- @{
- ViewData["Title"] = "SendgridEmail";
- }
- <h2>Sendgrid Email Api Inetegration</h2>
- <form asp-action="SendgridEmailSubmit" asp-controller="Home" method="post">
- <table style="padding:20px;" >
- <tr >
- <td >From</td>
- <td> <input asp-for="From" /></td>
- <td><br /></td>
- </tr>
- <tr>
- <td>To</td>
- <td> <input asp-for="To" /></td>
- </tr>
- <tr>
- <td>Subject</td>
- <td> <input asp-for="Subject" /></td>
- </tr>
- <tr>
- <td>
- Body
- </td>
- <td> <input asp-for="Body" /></td>
- </tr>
- <tr>
- <td></td>
- <td><input type="submit" value="Send Email"/></td>
- </tr>
- </table>
- </form>
HomeController.cs
- [HttpPost]
- public async Task<IActionResult> SendgridEmailSubmit(Emailmodel emailmodel)
- {
- ViewData["Message"] = "Email Sent!!!...";
- Example emailexample = new Example();
- await emailexample.Execute(emailmodel.From, emailmodel.To, emailmodel.Subject, emailmodel.Body
- , emailmodel.Body);
- return View("SendgridEmail");
- }

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.

Kaustav basuPosted Aug 21, 2021, 3:40 PM
The code is not working at my end
Abhijeet kaduPosted Jul 10, 2020, 5:22 AM
Hi, I am trying this but show forbidden status code
Shubham KumarPosted May 26, 2020, 9:28 AM
Hi, I tried to use this code , but it throw forbidden status code at the time of send email.
JosephsPosted Jul 18, 2019, 7:35 AM
How to make service same code
Dhanik SahniPosted May 1, 2019, 10:44 AM
Excellent and simple explanation.