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.
Prerequisites
- Visual Studio 2017 Community Edition
- Sendgrid free Account
- 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.
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.
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.
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.
- 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
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.
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".
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
- 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.