Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add tests & use case for From personalization #1137

Merged
merged 6 commits into from
Nov 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions USE_CASES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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

Expand Down
85 changes: 85 additions & 0 deletions tests/SendGrid.Tests/Integration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down