From 6c85a7d926989a9d418599d27fa6d68c893d426a Mon Sep 17 00:00:00 2001 From: Bilal Boussayoud <bboussayoud@twilio.com> Date: Fri, 5 Nov 2021 13:47:37 -0600 Subject: [PATCH 1/6] feat: add tests & use case for from personalization --- USE_CASES.md | 58 ++++++++++++++++++++ tests/SendGrid.Tests/Integration.cs | 85 +++++++++++++++++++++++++++++ 2 files changed, 143 insertions(+) diff --git a/USE_CASES.md b/USE_CASES.md index 1f9426e47..bc03ed7a9 100644 --- a/USE_CASES.md +++ b/USE_CASES.md @@ -8,6 +8,7 @@ This document provides examples for specific use cases. Please [open an issue](h - [Send a Single Email to Multiple Recipients](#send-a-single-email-to-multiple-recipients) - [Send a Single Email to a Single Recipient](#send-a-single-email-to-a-single-recipient) - [Send Multiple Emails to Multiple Recipients](#send-multiple-emails-to-multiple-recipients) +- [Send Multiple Emails with Personalizations](#send-multiple-emails-with-personalizations) - [Transactional Templates](#transactional-templates) - [With Mail Helper Class](#with-mail-helper-class) - [Without Mail Helper Class](#without-mail-helper-class) @@ -492,6 +493,63 @@ namespace Example } ``` +<a name="multipleemailspersonalization"></a> +# Send Multiple Emails with Personalizations + +```csharp +using SendGrid; +using SendGrid.Helpers.Mail; +using System; +using System.Threading.Tasks; +using System.Collections.Generic; + +namespace Example +{ + internal class Example + { + private static void Main() + { + Execute().Wait(); + } + + static async Task Execute() + { + var apiKey = Environment.GetEnvironmentVariable("NAME_OF_THE_ENVIRONMENT_VARIABLE_FOR_YOUR_SENDGRID_KEY"); + var client = new SendGridClient(apiKey); + var from = new EmailAddress("test@example.com"); + var subject = "Hello from Twilio SendGrid!"; + + //Note that the domain for all from addresses must match + var msg = new SendGridMessage(); + msg.Subject = subject; + msg.AddContent(MimeType.Text, "Easy to use, even with C#!"); + msg.SetFrom(from); + + msg.Personalizations = new List<Personalization>() { + new Personalization() { + Tos = new List<EmailAddress>() { + new EmailAddress("test1@example.com") + } + }, + new Personalization() { + Tos = new List<EmailAddress>() { + new EmailAddress("test2@example.com") + }, + From = new EmailAddress("test3@example.com") + }, + }; + var response = await client.SendEmailAsync(msg); + + Console.WriteLine(msg.Serialize()); + Console.WriteLine(response.StatusCode); + Console.WriteLine(response.Headers.ToString()); + Console.WriteLine("\n\nPress any key to exit."); + Console.ReadLine(); + } + } +} +``` + <a name="transactional-templates"></a> # Transactional Templates diff --git a/tests/SendGrid.Tests/Integration.cs b/tests/SendGrid.Tests/Integration.cs index a8d0f43eb..e7b213f78 100644 --- a/tests/SendGrid.Tests/Integration.cs +++ b/tests/SendGrid.Tests/Integration.cs @@ -592,6 +592,91 @@ public void TestCreateMultipleEmailsToMultipleRecipients() Assert.Equal("{\"from\":{\"name\":\"Example User\",\"email\":\"test@example.com\"},\"personalizations\":[{\"to\":[{\"email\":\"test1@example.com\"}],\"subject\":\"Test Subject1\",\"substitutions\":{\"-name-\":\"Name1\"}},{\"to\":[{\"email\":\"test2@example.com\"}],\"subject\":\"Test Subject2\",\"substitutions\":{\"-name-\":\"Name1\"}},{\"to\":[{\"email\":\"test3@example.com\"}],\"subject\":\"Test Subject3\",\"substitutions\":{\"-name-\":\"Name1\"}}],\"content\":[{\"type\":\"text/plain\",\"value\":\"Hello -name-\"}]}", msg5.Serialize()); } + [Fact] + public void TestAddFrom() + { + // Personalization not passed in, Personalization does not exist + var msg = new SendGridMessage(); + msg.AddTo(new EmailAddress("test001@example.com", "Example User")); + msg.SetFrom(new EmailAddress("test002@example.com", "Example User")); + Assert.Equal("{\"from\":{\"name\":\"Example User\",\"email\":\"test002@example.com\"},\"personalizations\":[{\"to\":[{\"name\":\"Example User\",\"email\":\"test001@example.com\"}]}]}", msg.Serialize()); + + // Personalization passed in, no Personalizations + msg = new SendGridMessage(); + var toEmail = new EmailAddress("test002@example.com", "Example User"); + var fromEmail = new EmailAddress("test001@example.com", "Example User"); + var personalization = new Personalization() + { + Tos = new List<EmailAddress>() + { + toEmail + }, + From = fromEmail + }; + msg.AddTo(new EmailAddress("test003@example.com", "Example User"), 0, personalization); + Assert.Equal("{\"personalizations\":[{\"to\":[{\"name\":\"Example User\",\"email\":\"test002@example.com\"}," + + "{\"name\":\"Example User\",\"email\":\"test003@example.com\"}]," + + "\"from\":{\"name\":\"Example User\",\"email\":\"test001@example.com\"}}]}", msg.Serialize()); + + // Personalization passed in, Personalization exists + msg = new SendGridMessage(); + toEmail = new EmailAddress("test004@example.com", "Example User"); + fromEmail = new EmailAddress("test005@example.com", "Example User"); + msg.Personalizations = new List<Personalization>() { + new Personalization() { + Tos = new List<EmailAddress>() + { + toEmail + }, + From = fromEmail + } + }; + toEmail = new EmailAddress("test006@example.com", "Example User"); + fromEmail = new EmailAddress("test007@example.com", "Example User"); + personalization = new Personalization() + { + Tos = new List<EmailAddress>() + { + toEmail + }, + From = fromEmail + }; + msg.AddTo(toEmail, 1, personalization); + Assert.Equal("{\"personalizations\":[{\"to\":[{\"name\":\"Example User\",\"email\":\"test004@example.com\"}]," + + "\"from\":{\"name\":\"Example User\",\"email\":\"test005@example.com\"}}," + + "{\"to\":[{\"name\":\"Example User\",\"email\":\"test006@example.com\"}]," + + "\"from\":{\"name\":\"Example User\",\"email\":\"test007@example.com\"}}]}", msg.Serialize()); + + + // Personalization not passed in Personalizations exists + msg = new SendGridMessage(); + toEmail = new EmailAddress("test009@example.com", "Example User"); + fromEmail = new EmailAddress("test010@example.com", "Example User"); + msg.Personalizations = new List<Personalization>() { + new Personalization() { + Tos = new List<EmailAddress>() + { + toEmail + }, + From = fromEmail + } + }; + toEmail = new EmailAddress("test011@example.com", "Example User"); + fromEmail = new EmailAddress("test012@example.com", "Example User"); + personalization = new Personalization() + { + Tos = new List<EmailAddress>() + { + toEmail + }, + From = fromEmail + }; + msg.Personalizations.Add(personalization); + msg.AddTo(new EmailAddress("test013@example.com", "Example User")); + Assert.Equal("{\"personalizations\":[{\"to\":[{\"name\":\"Example User\",\"email\":\"test009@example.com\"},{\"name\":\"Example User\",\"email\":\"test013@example.com\"}]," + + "\"from\":{\"name\":\"Example User\",\"email\":\"test010@example.com\"}},{\"to\":[{\"name\":\"Example User\",\"email\":\"test011@example.com\"}]," + + "\"from\":{\"name\":\"Example User\",\"email\":\"test012@example.com\"}}]}", msg.Serialize()); + } [Fact] public void TestAddTo() { From 981616521234a567efb4603c582ab93a68e1e608 Mon Sep 17 00:00:00 2001 From: Bilal Boussayoud <bboussayoud@twilio.com> Date: Mon, 8 Nov 2021 09:18:45 -0700 Subject: [PATCH 2/6] add comment space --- ExampleCoreProject/Program.cs | 75 ++++++++++++++++------------------- USE_CASES.md | 2 +- 2 files changed, 35 insertions(+), 42 deletions(-) diff --git a/ExampleCoreProject/Program.cs b/ExampleCoreProject/Program.cs index 1ddfa7306..a08acacae 100644 --- a/ExampleCoreProject/Program.cs +++ b/ExampleCoreProject/Program.cs @@ -1,57 +1,50 @@ using System; +using System.Collections.Generic; using System.Threading.Tasks; -using Microsoft.Extensions.Configuration; -using Microsoft.Extensions.DependencyInjection; using SendGrid; -using SendGrid.Extensions.DependencyInjection; using SendGrid.Helpers.Mail; namespace Example { - internal class Program + internal class ExampleEmail { - private static IConfiguration Configuration { get; set; } - - private static async Task Main() + private static void Main() { - var env = Environment.GetEnvironmentVariable("DOTNET_ENVIRONMENT") ?? "Production"; - Configuration = new ConfigurationBuilder() - .AddJsonFile("appsettings.json", optional: true) - .AddJsonFile($"appsettings.{env}.json", optional: true) - .Build(); - var services = ConfigureServices(new ServiceCollection()).BuildServiceProvider(); - var client = services.GetRequiredService<ISendGridClient>(); - var from = new EmailAddress(Configuration.GetValue("SendGrid:From", "test@example.com"), "Example User"); - var to = new EmailAddress(Configuration.GetValue("SendGrid:To", "test@example.com"), "Example User"); - var msg = new SendGridMessage - { - From = from, - Subject = "Sending with Twilio SendGrid is Fun" - }; - msg.AddContent(MimeType.Text, "and easy to do anywhere, even with C#"); - msg.AddTo(to); - if (Configuration.GetValue("SendGrid:SandboxMode", false)) - { - msg.MailSettings = new MailSettings - { - SandboxMode = new SandboxMode - { - Enable = true - } - }; - } - Console.WriteLine($"Sending email with payload: \n{msg.Serialize()}"); - var response = await client.SendEmailAsync(msg).ConfigureAwait(false); - - Console.WriteLine($"Response: {response.StatusCode}"); - Console.WriteLine(response.Headers); + Execute().Wait(); } - private static IServiceCollection ConfigureServices(IServiceCollection services) + static async Task Execute() { - services.AddSendGrid(options => { options.ApiKey = Environment.GetEnvironmentVariable("SENDGRID_API_KEY") ?? Configuration["SendGrid:ApiKey"]; }); + var apiKey = "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"; + var client = new SendGridClient(apiKey); + var from = new EmailAddress("team_intrapersonalfaces@devinterfaces.com"); + var subject = "Hello from Twilio SendGrid"; + + var msg = new SendGridMessage(); + msg.Subject = subject; + msg.AddContent(MimeType.Text, "Hello from fakegrid, please work"); + msg.SetFrom(from); + + msg.Personalizations = new List<Personalization>() { + new Personalization() { + Tos = new List<EmailAddress>() { + new EmailAddress("bboussayoud@colgate.edu") + }, + }, + new Personalization() { + Tos = new List<EmailAddress>() { + new EmailAddress("bilal.boussayoud@gmail.com") + }, + From = new EmailAddress("team_nofaces@devinterfaces.com") + }, + }; + var response = await client.SendEmailAsync(msg); - return services; + Console.WriteLine(msg.Serialize()); + Console.WriteLine(response.StatusCode); + Console.WriteLine(response.Headers.ToString()); + Console.WriteLine("\n\nPress any key to exit."); + Console.ReadLine(); } } } \ No newline at end of file diff --git a/USE_CASES.md b/USE_CASES.md index bc03ed7a9..8e6396292 100644 --- a/USE_CASES.md +++ b/USE_CASES.md @@ -519,7 +519,7 @@ namespace Example var from = new EmailAddress("test@example.com"); var subject = "Hello from Twilio SendGrid!"; - //Note that the domain for all from addresses must match + // Note that the domain for all from addresses must match var msg = new SendGridMessage(); msg.Subject = subject; msg.AddContent(MimeType.Text, "Easy to use, even with C#!"); From ed8f615843edd652a951accb85a9b0049e94c900 Mon Sep 17 00:00:00 2001 From: Bilal Boussayoud <bboussayoud@twilio.com> Date: Mon, 8 Nov 2021 09:22:31 -0700 Subject: [PATCH 3/6] Delete Program.cs --- ExampleCoreProject/Program.cs | 50 ----------------------------------- 1 file changed, 50 deletions(-) delete mode 100644 ExampleCoreProject/Program.cs diff --git a/ExampleCoreProject/Program.cs b/ExampleCoreProject/Program.cs deleted file mode 100644 index a08acacae..000000000 --- a/ExampleCoreProject/Program.cs +++ /dev/null @@ -1,50 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Threading.Tasks; -using SendGrid; -using SendGrid.Helpers.Mail; - -namespace Example -{ - internal class ExampleEmail - { - private static void Main() - { - Execute().Wait(); - } - - static async Task Execute() - { - var apiKey = "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"; - var client = new SendGridClient(apiKey); - var from = new EmailAddress("team_intrapersonalfaces@devinterfaces.com"); - var subject = "Hello from Twilio SendGrid"; - - var msg = new SendGridMessage(); - msg.Subject = subject; - msg.AddContent(MimeType.Text, "Hello from fakegrid, please work"); - msg.SetFrom(from); - - msg.Personalizations = new List<Personalization>() { - new Personalization() { - Tos = new List<EmailAddress>() { - new EmailAddress("bboussayoud@colgate.edu") - }, - }, - new Personalization() { - Tos = new List<EmailAddress>() { - new EmailAddress("bilal.boussayoud@gmail.com") - }, - From = new EmailAddress("team_nofaces@devinterfaces.com") - }, - }; - var response = await client.SendEmailAsync(msg); - - Console.WriteLine(msg.Serialize()); - Console.WriteLine(response.StatusCode); - Console.WriteLine(response.Headers.ToString()); - Console.WriteLine("\n\nPress any key to exit."); - Console.ReadLine(); - } - } -} \ No newline at end of file From 212c8d8c1c805935e42e53ef0f69b8c408b72523 Mon Sep 17 00:00:00 2001 From: Bilal Boussayoud <bboussayoud@twilio.com> Date: Mon, 8 Nov 2021 09:25:02 -0700 Subject: [PATCH 4/6] Revert "Delete Program.cs" This reverts commit ed8f615843edd652a951accb85a9b0049e94c900. --- ExampleCoreProject/Program.cs | 50 +++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 ExampleCoreProject/Program.cs diff --git a/ExampleCoreProject/Program.cs b/ExampleCoreProject/Program.cs new file mode 100644 index 000000000..a08acacae --- /dev/null +++ b/ExampleCoreProject/Program.cs @@ -0,0 +1,50 @@ +using System; +using System.Collections.Generic; +using System.Threading.Tasks; +using SendGrid; +using SendGrid.Helpers.Mail; + +namespace Example +{ + internal class ExampleEmail + { + private static void Main() + { + Execute().Wait(); + } + + static async Task Execute() + { + var apiKey = "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"; + var client = new SendGridClient(apiKey); + var from = new EmailAddress("team_intrapersonalfaces@devinterfaces.com"); + var subject = "Hello from Twilio SendGrid"; + + var msg = new SendGridMessage(); + msg.Subject = subject; + msg.AddContent(MimeType.Text, "Hello from fakegrid, please work"); + msg.SetFrom(from); + + msg.Personalizations = new List<Personalization>() { + new Personalization() { + Tos = new List<EmailAddress>() { + new EmailAddress("bboussayoud@colgate.edu") + }, + }, + new Personalization() { + Tos = new List<EmailAddress>() { + new EmailAddress("bilal.boussayoud@gmail.com") + }, + From = new EmailAddress("team_nofaces@devinterfaces.com") + }, + }; + var response = await client.SendEmailAsync(msg); + + Console.WriteLine(msg.Serialize()); + Console.WriteLine(response.StatusCode); + Console.WriteLine(response.Headers.ToString()); + Console.WriteLine("\n\nPress any key to exit."); + Console.ReadLine(); + } + } +} \ No newline at end of file From dfb273f935bffb76c88be30742fb8535fb7b6876 Mon Sep 17 00:00:00 2001 From: Bilal Boussayoud <bboussayoud@twilio.com> Date: Mon, 8 Nov 2021 09:25:18 -0700 Subject: [PATCH 5/6] Revert "add comment space" This reverts commit 981616521234a567efb4603c582ab93a68e1e608. --- ExampleCoreProject/Program.cs | 75 +++++++++++++++++++---------------- USE_CASES.md | 2 +- 2 files changed, 42 insertions(+), 35 deletions(-) diff --git a/ExampleCoreProject/Program.cs b/ExampleCoreProject/Program.cs index a08acacae..1ddfa7306 100644 --- a/ExampleCoreProject/Program.cs +++ b/ExampleCoreProject/Program.cs @@ -1,50 +1,57 @@ using System; -using System.Collections.Generic; using System.Threading.Tasks; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; using SendGrid; +using SendGrid.Extensions.DependencyInjection; using SendGrid.Helpers.Mail; namespace Example { - internal class ExampleEmail + internal class Program { - private static void Main() - { - Execute().Wait(); - } + private static IConfiguration Configuration { get; set; } - static async Task Execute() + private static async Task Main() { - var apiKey = "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"; - var client = new SendGridClient(apiKey); - var from = new EmailAddress("team_intrapersonalfaces@devinterfaces.com"); - var subject = "Hello from Twilio SendGrid"; + var env = Environment.GetEnvironmentVariable("DOTNET_ENVIRONMENT") ?? "Production"; + Configuration = new ConfigurationBuilder() + .AddJsonFile("appsettings.json", optional: true) + .AddJsonFile($"appsettings.{env}.json", optional: true) + .Build(); + var services = ConfigureServices(new ServiceCollection()).BuildServiceProvider(); + var client = services.GetRequiredService<ISendGridClient>(); + var from = new EmailAddress(Configuration.GetValue("SendGrid:From", "test@example.com"), "Example User"); + var to = new EmailAddress(Configuration.GetValue("SendGrid:To", "test@example.com"), "Example User"); + var msg = new SendGridMessage + { + From = from, + Subject = "Sending with Twilio SendGrid is Fun" + }; + msg.AddContent(MimeType.Text, "and easy to do anywhere, even with C#"); + msg.AddTo(to); + if (Configuration.GetValue("SendGrid:SandboxMode", false)) + { + msg.MailSettings = new MailSettings + { + SandboxMode = new SandboxMode + { + Enable = true + } + }; + } + Console.WriteLine($"Sending email with payload: \n{msg.Serialize()}"); + var response = await client.SendEmailAsync(msg).ConfigureAwait(false); - var msg = new SendGridMessage(); - msg.Subject = subject; - msg.AddContent(MimeType.Text, "Hello from fakegrid, please work"); - msg.SetFrom(from); + Console.WriteLine($"Response: {response.StatusCode}"); + Console.WriteLine(response.Headers); + } - msg.Personalizations = new List<Personalization>() { - new Personalization() { - Tos = new List<EmailAddress>() { - new EmailAddress("bboussayoud@colgate.edu") - }, - }, - new Personalization() { - Tos = new List<EmailAddress>() { - new EmailAddress("bilal.boussayoud@gmail.com") - }, - From = new EmailAddress("team_nofaces@devinterfaces.com") - }, - }; - var response = await client.SendEmailAsync(msg); + private static IServiceCollection ConfigureServices(IServiceCollection services) + { + services.AddSendGrid(options => { options.ApiKey = Environment.GetEnvironmentVariable("SENDGRID_API_KEY") ?? Configuration["SendGrid:ApiKey"]; }); - Console.WriteLine(msg.Serialize()); - Console.WriteLine(response.StatusCode); - Console.WriteLine(response.Headers.ToString()); - Console.WriteLine("\n\nPress any key to exit."); - Console.ReadLine(); + return services; } } } \ No newline at end of file diff --git a/USE_CASES.md b/USE_CASES.md index 8e6396292..bc03ed7a9 100644 --- a/USE_CASES.md +++ b/USE_CASES.md @@ -519,7 +519,7 @@ namespace Example var from = new EmailAddress("test@example.com"); var subject = "Hello from Twilio SendGrid!"; - // Note that the domain for all from addresses must match + //Note that the domain for all from addresses must match var msg = new SendGridMessage(); msg.Subject = subject; msg.AddContent(MimeType.Text, "Easy to use, even with C#!"); From c0f92844713d00eb0f59988936a30f80f611403f Mon Sep 17 00:00:00 2001 From: Bilal Boussayoud <bboussayoud@twilio.com> Date: Mon, 8 Nov 2021 09:26:05 -0700 Subject: [PATCH 6/6] Address spacing nit --- USE_CASES.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/USE_CASES.md b/USE_CASES.md index bc03ed7a9..8e6396292 100644 --- a/USE_CASES.md +++ b/USE_CASES.md @@ -519,7 +519,7 @@ namespace Example var from = new EmailAddress("test@example.com"); var subject = "Hello from Twilio SendGrid!"; - //Note that the domain for all from addresses must match + // Note that the domain for all from addresses must match var msg = new SendGridMessage(); msg.Subject = subject; msg.AddContent(MimeType.Text, "Easy to use, even with C#!");