Created
April 15, 2019 08:00
-
-
Save vfabing/3f547635d6b4ac2d138f67f92b7f59c7 to your computer and use it in GitHub Desktop.
Send Sendgrid Dynamic Transactional Template email from dotnetcore
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 Newtonsoft.Json; | |
using SendGrid; | |
using SendGrid.Helpers.Mail; | |
namespace dotnet_sendgrid | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var sendGridClient = new SendGridClient("MY_API_KEY"); | |
var sendGridMessage = new SendGridMessage(); | |
sendGridMessage.SetFrom("vfabing@live.com", "Vivien FABING"); | |
sendGridMessage.AddTo("usertest2019@yopmail.com", "user test"); | |
sendGridMessage.SetTemplateId("MY_TEMPLATE_ID"); | |
sendGridMessage.SetTemplateData(new HelloEmail | |
{ | |
Name = "Vivien", | |
Url = "https://www.vivienfabing.com" | |
}); | |
var response = sendGridClient.SendEmailAsync(sendGridMessage).Result; | |
if (response.StatusCode == System.Net.HttpStatusCode.Accepted) | |
{ | |
Console.WriteLine("Email sent"); | |
} | |
} | |
private class HelloEmail | |
{ | |
[JsonProperty("name")] | |
public string Name { get; set; } | |
[JsonProperty("url")] | |
public string Url { get; set; } | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment