Skip to content

Instantly share code, notes, and snippets.

@ankitkanojia
Created September 2, 2019 05:56
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ankitkanojia/dcb24e6eeea2d10945704efc7d523c47 to your computer and use it in GitHub Desktop.
Save ankitkanojia/dcb24e6eeea2d10945704efc7d523c47 to your computer and use it in GitHub Desktop.
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