Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Send mail using send grid in c#
using System;
using SendGrid;
using SendGrid.Helpers.Mail; // Install Send Grid using nuget package manager
namespace ECommerce_Shop.Helpers
{
// Call this common function where you want to send a mail
public static class SendEmail
{
//Send Mail based on subject, HTML body content and TO(Email) passed | Godaddy
public static bool Send(string subject, string body, string to)
{
bool response;
try
{
var client = new SendGridClient("YOUR KEY");
var from = new EmailAddress("FROM MAIL", "FROM NAME");
var toAddress = new EmailAddress("TO EMAIL", "TO NAME");
var plainTextContent = string.Empty;
var htmlContent = body; //BODY content or email template
var msg = MailHelper.CreateSingleEmail(from, toAddress, subject, plainTextContent, htmlContent);
var sendEmailAsync = client.SendEmailAsync(msg);
response = true;
}
catch (Exception e)
{
response = false;
}
return response;
}
}
}
using YourProject.Helpers;
namespace YourProject.Controllers
{
public class HomeController : Controller
{
public void SendTestMail1620()
{
SendEmail.Send("EMAIL SUBJECT", "EMAIL TEMPLATE or CONTENT", "TO EMAIL");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment