Sending A SendGrid Template Email

SendGrid is a pretty good ESP and has a nice API, it also has a free tier available through the Azure Portal. The C# library for it available through NuGet makes it easy to call the API but the documentation on GitHub is a bit lacking.

Below is some basic code for sending an email to a preconfigured template with placeholders.

	
SendGridClient sendGridClient = new SendGridClient(sendGridConfig.ApiKey);

Dictionary replacements = new Dictionary();
replacements.Add("fileName", fileName);
replacements.Add("response", await response.Content.ReadAsStringAsync());

SendGridMessage sendGridMessage = MailHelper.CreateSingleTemplateEmailToMultipleRecipients(new EmailAddress(sendGridConfig.From), sendGridConfig.To.Select(x => new EmailAddress { Email = x }).ToList(), sendGridConfig.TemplateId, replacements);
sendGridMessage.Subject = "File Transfered";
Response sendGridResponse = await sendGridClient.SendEmailAsync(sendGridMessage);
	

Leave a Reply

Your email address will not be published. Required fields are marked *