Send Email Using SendGrid API in Asp.Net MVC application

Send Email Using SendGrid API in Asp.Net MVC & C#, how to register ad generate API key.

 Asp.Net   
 

Posted: May 27, 2017. | By: mustafa

SendGrid is a cloud-based email service provider for businesses, and it is easy to host using Microsoft Azure cloud as well. According to SendGrid, it is a transactional & marketing email tool for business use. It is scalable and provides real-time analytics along with flexible API.
SendGrid has different subscription plans availale for SendGrid API and Email Marketing Campaign. You can go with 30 day free trial sending 40,000 emails per month.

Advantages -

  • It is cloud based solution.
  • Administering distribution lists for sending newsletter.
  • Offering real-time metrics like blocked e-mail, and customer responsiveness.
  • Create reports based on different categories.

Here we have defined some simple steps to follow to send email using Sendgrid API from Asp.Net MVC application.

Steps to follow -

  1. Create and login to SendGrid Account.
  2. How to get SendGrid API key used to send email.
  3. Define settings in web.config.
  4. How to use the method to call API and send emails.

 

Create and login to your SendGrid account
Open url: https://sendgrid.com click on SignUp to create your account you can choose the plans as per your requirement Or you can go with the Free plan. Create your account and login using your username & password.

How to get SendGrid API key used to send email
After login into sendgrid dashboard. Click on Settings menu to expand in the left pane, then select API keys as shown below.

 SendGridDashboard.png, Asp.Net, MVC, SendGrid API,

 

Here on the API key page you will find create API key button on the right top click on it & select General API key it will open a side window to create the API key.

 GeneralAPIKey.png, Asp.Net, MVC, SendGrid API,

 

Here define any relevant name for the key and select the access options for all services needed. And finally click on save button to generate the key as shown in the picture below and copy the key to be used later in your application.

Define settings in web config
To allow the application to access the SendGrid API add key value we have received above in our web.config.

<add key="SendGridAPIKey" value="API Key Value" />

How to use the method to call API and send emails.
Now let us write a method to call API and start sending email but first we have to install SendGrid Nuget package to our application to able to send emails using SendGrid API.
Go to your Solution and Right click on Add References and select Manage Nuget Package, now search for SendGrid and start installing the package.

 NugetSendgrid.png, Asp.Net, MVC, SendGrid API,


Add reference to followng libraries in your code.
using SendGrid;
using System.Net.Mail;
using System.Net;


Now create a SendMail() method as follows.

public ActionResult SendEmail()
{
 SendGridMessage myMessage = new SendGridMessage();
 myMessage.AddTo("account@gmail.com");
 myMessage.From = new MailAddress("abc@youremail.com", "abc 123");
 myMessage.Subject = "Test Email.";
 myMessage.Text = "This is a test mail from SendGrid";
 
 var transportWeb = new Web(ConfigurationManager.AppSettings["SendGridAPIKey"]);
 transportWeb.DeliverAsync(myMessage);

 return true;
}

First we create a SendGridMessage object and then add From, To, Subject, Text or Html properties. At last we declare the SendGrid.Web() object instantiated with sendgrid API key and finally deliver the email message.

Now we can use our SendMail() method to start sending the email using SendGrid API.

Also we can send mail using NetworkCredential with the username and password, in case we have not generate any API key.

var credentials = new NetworkCredential(Username, Password);
var transportWeb = new Web(credentials);
transportWeb.DeliverAsync(myMessage);

We can also send email using .Net's built in SMTP Library, we need to specify SMTP mail settings.

<system.net>
 <mailSettings>
 <smtp from="abc@domain.com">
 <network host="smtp.sendgrid.net" password="password" userName="username" port="587" />
 </smtp>
 </mailSettings>
</system.net>

Now lets define method to build email message using System.Net.Mail & send it using SendGrid.Net library.

public void SendMail(string subject, string msgBody, string sendEmail)
 {
 var myMessage = new SendGrid.SendGridMessage();
 myMessage.AddTo(sendEmail);
 //myMessage.From = new MailAddress("you@youremail.com", "First Last");
 myMessage.Subject = subject;
 myMessage.Html = msgBody;

MailAddress[] addresses = new MailAddress[1];
 myMessage.From = new MailAddress("abc@mail-domian.com", "Admin");
 addresses[0] = new MailAddress("info@mail-domian.com");
 myMessage.ReplyTo = addresses;

var transportWeb = new SendGrid.Web(ConfigurationManager.AppSettings["SendGridAPIKey"]);
 transportWeb.DeliverAsync(myMessage).Wait();
 }

related posts

Back to top