Skip to content

Commit 2e4a361

Browse files
Multiple Emails to Multiple Recipients example
1 parent 1eae548 commit 2e4a361

File tree

1 file changed

+74
-20
lines changed

1 file changed

+74
-20
lines changed

USE_CASES.md

Lines changed: 74 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ This documentation provides examples for specific use cases. Please [open an iss
66
* [Email - Kitchen Sink - an example with all settings used](#kitchensink)
77
* [Email - Send a Single Email to Multiple Recipients](#singleemailmultiplerecipients)
88
* [Email - Send a Single Email to a Single Recipient](#singleemailsinglerecipient)
9+
* [Email - Send Multiple Emails to Multiple Recipients](#multipleemailsmultiplerecipients)
910
* [Email - Transactional Templates](#transactional_templates)
1011

1112
<a name="attachments"></a>
@@ -229,10 +230,10 @@ namespace Example
229230
msg.SetTemplateId("13b8f94f-bcae-4ec6-b752-70d6cb59f932");
230231

231232
msg.AddGlobalHeader("X-Day", "Monday");
232-
var globalHeaders = new Dictionary<string, string>
233-
{
233+
var globalHeaders = new Dictionary<string, string>
234+
{
234235
{ "X-Month", "January" },
235-
{ "X-Year", "2017" }
236+
{ "X-Year", "2017" }
236237
};
237238
msg.AddGlobalHeaders(globalHeaders);
238239

@@ -245,18 +246,18 @@ namespace Example
245246
msg.AddSections(sections);
246247

247248
msg.AddCategory("customer");
248-
var categories = new List<string>
249-
{
249+
var categories = new List<string>
250+
{
250251
"vip",
251252
"new_account"
252253
};
253254
msg.AddCategories(categories);
254255

255256
msg.AddGlobalCustomArg("campaign", "welcome");
256257
var globalCustomArgs = new Dictionary<string, string>
257-
{
258+
{
258259
{ "sequence2", "2" },
259-
{ "sequence3", "3" }
260+
{ "sequence3", "3" }
260261
};
261262
msg.AddGlobalCustomArgs(globalCustomArgs);
262263

@@ -265,36 +266,36 @@ namespace Example
265266
msg.SetGlobalSendAt(1461775051);
266267

267268
msg.SetIpPoolName("23");
268-
269+
269270
// This must be a valid [batch ID](https://sendgrid.com/docs/API_Reference/SMTP_API/scheduling_parameters.html)
270271
//msg.SetBatchId("some_batch_id");
271272
272273
msg.SetBccSetting(true, "[email protected]");
273274

274275
msg.SetBypassListManagement(true);
275-
276+
276277
msg.SetFooterSetting(true, "Some Footer HTML", "Some Footer Text");
277-
278+
278279
msg.SetSandBoxMode(true);
279-
280+
280281
msg.SetSpamCheck(true, 1, "https://gotchya.example.com");
281-
282+
282283
msg.SetClickTracking(true, false);
283-
284+
284285
msg.SetOpenTracking(true, "Optional tag to replace with the open image in the body of the message");
285-
286+
286287
msg.SetSubscriptionTracking(true,
287288
"HTML to insert into the text / html portion of the message",
288289
"text to insert into the text/plain portion of the message",
289290
"substitution tag");
290-
291+
291292
msg.SetGoogleAnalytics(true,
292293
"some campaign",
293294
"some content",
294295
"some medium",
295296
"some source",
296297
"some term");
297-
298+
298299
msg.SetReplyTo(new EmailAddress("[email protected]", "Reply To Me"));
299300

300301
var response = await client.SendEmailAsync(msg);
@@ -335,9 +336,9 @@ namespace Example
335336
var from = new EmailAddress("[email protected]", "Example User");
336337
var tos = new List<EmailAddress>
337338
{
338-
new EmailAddress("[email protected]"),
339-
new EmailAddress("[email protected]"),
340-
new EmailAddress("[email protected]")
339+
new EmailAddress("[email protected]", "Example User1"),
340+
new EmailAddress("[email protected]", "Example User2"),
341+
new EmailAddress("[email protected]", "Example User3")
341342
};
342343
var subject = "Sending with SendGrid is Fun";
343344
var plainTextContent = "and easy to do anywhere, even with C#";
@@ -388,6 +389,59 @@ namespace Example
388389
}
389390
```
390391

392+
<a name="multipleemailsmultiplerecipients"></a>
393+
# Send Multiple Emails to Multiple Recipients
394+
395+
```csharp
396+
using SendGrid;
397+
using SendGrid.Helpers.Mail;
398+
using System;
399+
using System.Threading.Tasks;
400+
using System.Collections.Generic;
401+
402+
namespace Example
403+
{
404+
internal class Example
405+
{
406+
private static void Main()
407+
{
408+
Execute().Wait();
409+
}
410+
411+
static async Task Execute()
412+
{
413+
var apiKey = Environment.GetEnvironmentVariable("NAME_OF_THE_ENVIRONMENT_VARIABLE_FOR_YOUR_SENDGRID_KEY");
414+
var client = new SendGridClient(apiKey);
415+
416+
var from = new EmailAddress("[email protected]", "Example User");
417+
var tos = new List<EmailAddress>
418+
{
419+
new EmailAddress("[email protected]", "Example User1"),
420+
new EmailAddress("[email protected]", "Example User2"),
421+
new EmailAddress("[email protected]", "Example User3")
422+
};
423+
var subjects = new List<string> { "Test Subject1", "Test Subject2", "Test Subject3" };
424+
var plainTextContent = "Hello -name-";
425+
var htmlContent = "Goodbye -name-";
426+
var substitutions = new List<Dictionary<string, string>>
427+
{
428+
new Dictionary<string, string>() {{"-name-", "Name1"}},
429+
new Dictionary<string, string>() {{"-name-", "Name2"}},
430+
new Dictionary<string, string>() {{"-name-", "Name3"}}
431+
};
432+
433+
var msg = MailHelper.CreateMultipleEmailsToMultipleRecipients(from,
434+
tos,
435+
subjects,
436+
plainTextContent,
437+
htmlContent,
438+
substitutions
439+
);
440+
}
441+
}
442+
}
443+
```
444+
391445
<a name="transactional_templates"></a>
392446
# Transactional Templates
393447

@@ -467,7 +521,7 @@ namespace Example
467521
## Without Mail Helper Class
468522

469523
```csharp
470-
using Newtonsoft.Json;
524+
using Newtonsoft.Json;
471525
using System.Threading.Tasks;
472526
using System;
473527
using SendGrid;

0 commit comments

Comments
 (0)