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);
- }
- }
- }




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.