SendGrid Integration with c#
SendGrid is one of the way to send mail through the application which gives the statics on the emails send through the email server.There are two ways to configure mail client through the send grid.
1.STMP server
2.Through the API
Send grid Event notification will give the notifications on the email activity happened like bounces,clicks,opens etc to get the potential reach of the email send via send grid which is a very cool option if you are working with some marketing related application.Let's see how to do this.
Configure SendGrid
1.Set up an account with send grid.
2.In the start up guide you can configure the SMTP server or the API.
3.Select the WEB API option and generate Api key and keep the API key with you.
4.To set up SMTP,choose the option and create new key for the SMTP server like below.
Note down the username,generated key,Server name,Port also.
For this , we need to set environment variable named as mail engine with value as the API key. You can set this by clicking on the system (our desk top) ->advanced settings like below,
For sending Mail through the SMTP server,
For this we need to set up web config with our api key,server name port.
1.STMP server
2.Through the API
Send grid Event notification will give the notifications on the email activity happened like bounces,clicks,opens etc to get the potential reach of the email send via send grid which is a very cool option if you are working with some marketing related application.Let's see how to do this.
Configure SendGrid
1.Set up an account with send grid.
2.In the start up guide you can configure the SMTP server or the API.
3.Select the WEB API option and generate Api key and keep the API key with you.
4.To set up SMTP,choose the option and create new key for the SMTP server like below.
Note down the username,generated key,Server name,Port also.
Now we are done with send grid set up.
Integrate in C# Code
1.Create a asp.net web api application.
2.Add package PM> Install-Package SendGrid.
Code to send email through the API:
/// SendMail using API for this you need to install sendgrid package. | |
/// </summary> | |
/// <returns></returns> | |
[HttpGet] | |
public async Task<bool> SendMailUsingAPI() | |
{ | |
var apiKey = Environment.GetEnvironmentVariable("MailEngine"); | |
var client = new SendGridClient(apiKey); | |
var from = new EmailAddress("abcd@outlook.com", "MailEngine"); | |
var subject = "Sending with SendGrid is Fun"; | |
var to = new EmailAddress("abcd@socxo.com", "Anu test"); | |
var plainTextContent = "test 12345" + "https://stackoverflow.com/questions/23000224/design-considerations-for-an-rss-client?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa"; | |
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); | |
return true; | |
} |
For this , we need to set environment variable named as mail engine with value as the API key. You can set this by clicking on the system (our desk top) ->advanced settings like below,
For sending Mail through the SMTP server,
/// Send mail using Smtp server | |
/// </summary> | |
/// <returns></returns> | |
public bool SendMailUsingSmtpServer() | |
{ | |
using (SmtpClient smtpClient = new SmtpClient()) | |
{ | |
MailMessage mailMessage = new MailMessage(); | |
mailMessage.From = new MailAddress("abcd@outlook.com"); | |
mailMessage.To.Add(new MailAddress("abcd@suyati.com")); | |
mailMessage.To.Add(new MailAddress("abcd@socxo.com")); | |
mailMessage.Subject = "TestMail"; | |
mailMessage.Body = "test 12345" + "https://stackoverflow.com/questions/23000224/design-considerations-for-an-rss-client?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa"; | |
// AddCopies(mailMessage); | |
smtpClient.Send(mailMessage); | |
} | |
return true; | |
} |
For this we need to set up web config with our api key,server name port.
<system.net> | |
<mailSettings> | |
<smtp from="SOCXO <abcd@outlook.com>"> | |
<network host="smtp.sendgrid.net" password="YOUR_API_KEY" userName="username" port="587" /> | |
</smtp> | |
</mailSettings> | |
<defaultProxy enabled="true" /> | |
</system.net> |
Replace the YOUR_API_KEY with your smtp key and username with your username.
you can get the complete code from the GIT link:
https://github.com/AnuOuseph1991/SendGrid-Integration-with-C-
Pretty simple!!Happy coding :)
Nice & useful Article
ReplyDelete