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, apiKey, httpErrorAsException: true); // Send a Single Email using the Mail Helper var from = new EmailAddress(configuration.GetValue("SendGrid:From", "*****@*****.**"), "Example User"); var subject = "Hello World from the Twilio SendGrid CSharp Library Helper!"; var to = new EmailAddress(configuration.GetValue("SendGrid:To", "*****@*****.**"), "Example User"); var plainTextContent = "Hello, Email from the helper [SendSingleEmailAsync]!"; var htmlContent = "<strong>Hello, Email from the helper! [SendSingleEmailAsync]</strong>"; 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 <Enter> 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 <Enter> 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 <Enter> to continue."); Console.ReadLine(); // Send a Single Email Without the Mail Helper var data = @"{ 'personalizations': [ { 'to': [ { 'email': '*****@*****.**' } ], 'subject': 'Hello World from the Twilio SendGrid C# Library!' } ], 'from': { 'email': '*****@*****.**' }, 'content': [ { 'type': 'text/plain', 'value': 'Hello, Email!' } ] }"; var json = JsonConvert.DeserializeObject <object>(data); response = await client.RequestAsync(BaseClient.Method.POST, json.ToString(), urlPath : "mail/send"); Console.WriteLine(response.StatusCode); Console.WriteLine(response.Headers); Console.WriteLine("\n\nPress <Enter> to continue."); Console.ReadLine(); // GET Collection var queryParams = @"{ 'limit': 100 }"; response = await client.RequestAsync(method : BaseClient.Method.GET, urlPath : "asm/groups", queryParams : queryParams); Console.WriteLine(response.StatusCode); Console.WriteLine(response.Body.ReadAsStringAsync().Result); Console.WriteLine(response.Headers); Console.WriteLine("\n\nPress <Enter> to continue to POST."); Console.ReadLine(); // POST var requestBody = @"{ 'description': 'Suggestions for products our users might like.', 'is_default': false, 'name': 'Magic Products' }"; json = JsonConvert.DeserializeObject <object>(requestBody); response = await client.RequestAsync(method : BaseClient.Method.POST, urlPath : "asm/groups", requestBody : json.ToString()); var dsResponse = JsonConvert.DeserializeObject <Dictionary <string, dynamic> >(response.Body.ReadAsStringAsync().Result); Console.WriteLine(response.StatusCode); Console.WriteLine(response.Body.ReadAsStringAsync().Result); Console.WriteLine(response.Headers); Console.WriteLine("\n\nPress <Enter> to continue to GET single."); Console.ReadLine(); if (dsResponse != null && dsResponse.ContainsKey("id")) { var groupId = dsResponse["id"].ToString(); // GET Single response = await client.RequestAsync(method : BaseClient.Method.GET, urlPath : $"asm/groups/{groupId}"); Console.WriteLine(response.StatusCode); Console.WriteLine(response.Body.ReadAsStringAsync().Result); Console.WriteLine(response.Headers); Console.WriteLine("\n\nPress <Enter> to continue to PATCH."); Console.ReadLine(); // PATCH requestBody = @"{ 'name': 'Cool Magic Products' }"; json = JsonConvert.DeserializeObject <object>(requestBody); response = await client.RequestAsync(method : BaseClient.Method.PATCH, urlPath : $"asm/groups/{groupId}", requestBody : json.ToString()); Console.WriteLine(response.StatusCode); Console.WriteLine(response.Body.ReadAsStringAsync().Result); Console.WriteLine(response.Headers.ToString()); Console.WriteLine("\n\nPress <Enter> to continue to PUT."); Console.ReadLine(); // DELETE response = await client.RequestAsync(method : BaseClient.Method.DELETE, urlPath : $"asm/groups/{groupId}"); Console.WriteLine(response.StatusCode); Console.WriteLine(response.Headers.ToString()); Console.WriteLine("\n\nPress <Enter> to DELETE and exit."); Console.ReadLine(); } }
static async Task Execute() { // Retrieve the API key from the environment variables. See the project README for more info about setting this up. var apiKey = Environment.GetEnvironmentVariable("NAME_OF_THE_ENVIRONMENT_VARIABLE_FOR_YOUR_SENDGRID_KEY"); var client = new SendGridClient(apiKey); // Send a Single Email using the Mail Helper var from = new EmailAddress("*****@*****.**", "Example User"); var subject = "Hello World from the Twilio SendGrid CSharp Library Helper!"; var to = new EmailAddress("*****@*****.**", "Example User"); var plainTextContent = "Hello, Email from the helper [SendSingleEmailAsync]!"; var htmlContent = "<strong>Hello, Email from the helper! [SendSingleEmailAsync]</strong>"; 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 <Enter> to continue."); Console.ReadLine(); // Send a Single Email using the Mail Helper with convenience methods and initialized SendGridMessage object msg = new SendGridMessage() { From = new EmailAddress("*****@*****.**", "Example User"), Subject = "Hello World from the Twilio SendGrid CSharp Library Helper!", PlainTextContent = "Hello, Email from the helper [SendSingleEmailAsync]!", HtmlContent = "<strong>Hello, Email from the helper! [SendSingleEmailAsync]</strong>" }; msg.AddTo(new EmailAddress("*****@*****.**", "Example User")); response = await client.SendEmailAsync(msg); Console.WriteLine(msg.Serialize()); Console.WriteLine(response.StatusCode); Console.WriteLine(response.Headers); Console.WriteLine("\n\nPress <Enter> to continue."); Console.ReadLine(); // Send a Single Email using the Mail Helper, entirely with convenience methods msg = new SendGridMessage(); msg.SetFrom(new EmailAddress("*****@*****.**", "Example User")); msg.SetSubject("Hello World from the Twilio SendGrid CSharp Library Helper!"); msg.AddContent(MimeType.Text, "Hello, Email from the helper [SendSingleEmailAsync]!"); msg.AddContent(MimeType.Html, "<strong>Hello, Email from the helper! [SendSingleEmailAsync]</strong>"); msg.AddTo(new EmailAddress("*****@*****.**", "Example User")); response = await client.SendEmailAsync(msg); Console.WriteLine(msg.Serialize()); Console.WriteLine(response.StatusCode); Console.WriteLine(response.Headers); Console.WriteLine("\n\nPress <Enter> to continue."); Console.ReadLine(); // Send a Single Email Without the Mail Helper string data = @"{ 'personalizations': [ { 'to': [ { 'email': '*****@*****.**' } ], 'subject': 'Hello World from the TWilio SendGrid C# Library!' } ], 'from': { 'email': '*****@*****.**' }, 'content': [ { 'type': 'text/plain', 'value': 'Hello, Email!' } ] }"; var json = JsonConvert.DeserializeObject <object>(data); response = await client.RequestAsync(SendGridClient.Method.POST, json.ToString(), urlPath : "mail/send"); Console.WriteLine(response.StatusCode); Console.WriteLine(response.Headers); Console.WriteLine("\n\nPress <Enter> to continue."); Console.ReadLine(); // GET Collection string queryParams = @"{ 'limit': 100 }"; response = await client.RequestAsync(method : SendGridClient.Method.GET, urlPath : "asm/groups", queryParams : queryParams); Console.WriteLine(response.StatusCode); Console.WriteLine(response.Body.ReadAsStringAsync().Result); Console.WriteLine(response.Headers); Console.WriteLine("\n\nPress <Enter> to continue to POST."); Console.ReadLine(); // POST string requestBody = @"{ 'description': 'Suggestions for products our users might like.', 'is_default': false, 'name': 'Magic Products' }"; json = JsonConvert.DeserializeObject <object>(requestBody); response = await client.RequestAsync(method : SendGridClient.Method.POST, urlPath : "asm/groups", requestBody : json.ToString()); var ds_response = JsonConvert.DeserializeObject <Dictionary <string, dynamic> >(response.Body.ReadAsStringAsync().Result); Console.WriteLine(response.StatusCode); Console.WriteLine(response.Body.ReadAsStringAsync().Result); Console.WriteLine(response.Headers); Console.WriteLine("\n\nPress <Enter> to continue to GET single."); Console.ReadLine(); if (ds_response != null && ds_response.ContainsKey("id")) { string group_id = ds_response["id"].ToString(); // GET Single response = await client.RequestAsync(method : SendGridClient.Method.GET, urlPath : string.Format("asm/groups/{0}", group_id)); Console.WriteLine(response.StatusCode); Console.WriteLine(response.Body.ReadAsStringAsync().Result); Console.WriteLine(response.Headers); Console.WriteLine("\n\nPress <Enter> to continue to PATCH."); Console.ReadLine(); // PATCH requestBody = @"{ 'name': 'Cool Magic Products' }"; json = JsonConvert.DeserializeObject <object>(requestBody); response = await client.RequestAsync(method : SendGridClient.Method.PATCH, urlPath : string.Format("asm/groups/{0}", group_id), requestBody : json.ToString()); Console.WriteLine(response.StatusCode); Console.WriteLine(response.Body.ReadAsStringAsync().Result); Console.WriteLine(response.Headers.ToString()); Console.WriteLine("\n\nPress <Enter> to continue to PUT."); Console.ReadLine(); // DELETE response = await client.RequestAsync(method : SendGridClient.Method.DELETE, urlPath : string.Format("asm/groups/{0}", group_id)); Console.WriteLine(response.StatusCode); Console.WriteLine(response.Headers.ToString()); Console.WriteLine("\n\nPress <Enter> to DELETE and exit."); Console.ReadLine(); } }
static async Task Execute() { // Retrieve the API key from the environment variables. See the project README for more info about setting this up. var apiKey = Environment.GetEnvironmentVariable("SENDGRID_API_KEY"); var fromEmail = "*****@*****.**"; var fromEmailAliasName = "Phong Ha"; var toEmail = "*****@*****.**"; var toEmailAlias = "Phong89"; if (apiKey == null) { Console.WriteLine("Error: SendGrid API Key environment variable: SENDGRID_API_KEY not found. Please configure System Variables."); Console.WriteLine("\n\nPress <Enter> to continue."); Console.ReadLine(); Environment.Exit(0); } var client = new SendGridClient(apiKey); // Send a Single Email using the Mail Helper var from = new EmailAddress(fromEmail, fromEmailAliasName); var subject = "Hello World from the SendGrid CSharp Library Helper!"; var to = new EmailAddress(toEmail, toEmailAlias); var plainTextContent = "Hello, Email from the helper [SendSingleEmailAsync]!"; var htmlContent = "<strong>Hello, Email from the helper! [SendSingleEmailAsync]</strong>"; 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 <Enter> to continue."); Console.ReadLine(); // Send a Single Email using the Mail Helper with convenience methods and initialized SendGridMessage object msg = new SendGridMessage() { From = new EmailAddress(fromEmail, fromEmailAliasName), Subject = "Hello World from the SendGrid CSharp Library Helper!", PlainTextContent = "Hello, Email from the helper [SendSingleEmailAsync]!", HtmlContent = "<strong>Hello, Email from the helper! [SendSingleEmailAsync]</strong>" }; msg.AddTo(new EmailAddress(toEmail, toEmailAlias)); response = await client.SendEmailAsync(msg); Console.WriteLine(msg.Serialize()); Console.WriteLine(response.StatusCode); Console.WriteLine(response.Headers); Console.WriteLine("\n\nPress <Enter> to continue."); Console.ReadLine(); // Send a Single Email using the Mail Helper, entirely with convenience methods msg = new SendGridMessage(); msg.SetFrom(new EmailAddress(fromEmailAliasName, "Phong Ha")); msg.SetSubject("Hello World from the SendGrid CSharp Library Helper!"); msg.AddContent(MimeType.Text, "Hello, Email from the helper [SendSingleEmailAsync]!"); msg.AddContent(MimeType.Html, "<strong>Hello, Email from the helper! [SendSingleEmailAsync]</strong>"); msg.AddTo(new EmailAddress(toEmail, toEmailAlias)); response = await client.SendEmailAsync(msg); Console.WriteLine(msg.Serialize()); Console.WriteLine(response.StatusCode); Console.WriteLine(response.Headers); Console.WriteLine("\n\nPress <Enter> to continue."); Console.ReadLine(); }