Created
September 2, 2019 05:56
-
-
Save ankitkanojia/dcb24e6eeea2d10945704efc7d523c47 to your computer and use it in GitHub Desktop.
Send mail using send grid in c#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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