using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
using Newtonsoft.Json;
using SendGrid;
using SendGrid.Helpers.Mail;
namespace Example
{
internal class Program
{
// HttpClient is intended to be instantiated once per application, rather than per-use.
// See https://docs.microsoft.com/dotnet/api/system.net.http.httpclient#remarks
private static readonly HttpClient HttpClient = new HttpClient();
private static async Task Main()
{
var env = Environment.GetEnvironmentVariable("DOTNET_ENVIRONMENT") ?? "Production";
var configuration = new ConfigurationBuilder()
.AddJsonFile("appsettings.json", optional: true)
.AddJsonFile($"appsettings.{env}.json", optional: true)
.Build();
// Retrieve the API key.
var apiKey = Environment.GetEnvironmentVariable("SENDGRID_API_KEY") ?? configuration["SendGrid:ApiKey"];
var client = new SendGridClient(HttpClient, new SendGridClientOptions { ApiKey = apiKey, HttpErrorAsException = true });
// Send a Single Email using the Mail Helper
var from = new EmailAddress(configuration.GetValue("SendGrid:From", "test@example.com"), "Example User");
var subject = "Hello World from the Twilio SendGrid CSharp Library Helper!";
var to = new EmailAddress(configuration.GetValue("SendGrid:To", "test@example.com"), "Example User");
var plainTextContent = "Hello, Email from the helper [SendSingleEmailAsync]!";
var htmlContent = "Hello, Email from the helper! [SendSingleEmailAsync]";
var msg = MailHelper.CreateSingleEmail(from, to, subject, plainTextContent, htmlContent);
var response = await client.SendEmailAsync(msg);
Console.WriteLine(msg.Serialize());
Console.WriteLine(response.StatusCode);
Console.WriteLine(response.Headers);
Console.WriteLine("\n\nPress to continue.");
Console.ReadLine();
// Send a Single Email using the Mail Helper with convenience methods and initialized SendGridMessage object
msg = new SendGridMessage
{
From = from,
Subject = subject,
PlainTextContent = plainTextContent,
HtmlContent = htmlContent
};
msg.AddTo(to);
response = await client.SendEmailAsync(msg);
Console.WriteLine(msg.Serialize());
Console.WriteLine(response.StatusCode);
Console.WriteLine(response.Headers);
Console.WriteLine("\n\nPress to continue.");
Console.ReadLine();
// Send a Single Email using the Mail Helper, entirely with convenience methods
msg = new SendGridMessage();
msg.SetFrom(from);
msg.SetSubject(subject);
msg.AddContent(MimeType.Text, plainTextContent);
msg.AddContent(MimeType.Html, htmlContent);
msg.AddTo(to);
response = await client.SendEmailAsync(msg);
Console.WriteLine(msg.Serialize());
Console.WriteLine(response.StatusCode);
Console.WriteLine(response.Headers);
Console.WriteLine("\n\nPress to continue.");
Console.ReadLine();
// Send a Single Email Without the Mail Helper
var data = @"{
'personalizations': [
{
'to': [
{
'email': 'test@example.com'
}
],
'subject': 'Hello World from the Twilio SendGrid C# Library!'
}
],
'from': {
'email': 'test@example.com'
},
'content': [
{
'type': 'text/plain',
'value': 'Hello, Email!'
}
]
}";
var json = JsonConvert.DeserializeObject