diff --git a/USE_CASES.md b/USE_CASES.md index 1f9426e47..8e6396292 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 } ``` + +# 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() { + new Personalization() { + Tos = new List() { + new EmailAddress("test1@example.com") + } + }, + new Personalization() { + Tos = new List() { + 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(); + } + } +} +``` + # 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() + { + 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() { + new Personalization() { + Tos = new List() + { + 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() + { + 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() { + new Personalization() { + Tos = new List() + { + 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() + { + 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() {