forked from sendgrid/sendgrid-csharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Integration.cs
6329 lines (5813 loc) · 273 KB
/
Integration.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
namespace SendGrid.Tests
{
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Reliability;
using SendGrid.Helpers.Errors.Model;
using SendGrid.Helpers.Mail;
using SendGrid.Helpers.Reliability;
using Xunit;
using Xunit.Abstractions;
public class IntegrationFixture
{
public string apiKey = "SG.12345678901234567890123456789012";
}
public class Integration : IClassFixture<IntegrationFixture>
{
IntegrationFixture fixture;
private readonly ITestOutputHelper output;
public Integration(IntegrationFixture fixture, ITestOutputHelper output)
{
this.fixture = fixture;
this.output = output;
}
private SendGridClient GetClient(string mockStatusCode)
{
var headers = new Dictionary<string, string> { { "X-Mock", mockStatusCode } };
return new SendGridClient(fixture.apiKey, null, headers);
}
// Base case for sending a single email
[Fact]
public void TestSendSingleEmailWithHelper()
{
var msg = new SendGridMessage();
msg.SetFrom(new EmailAddress("test@example.com"));
msg.AddTo(new EmailAddress("test@example.com"));
msg.SetSubject("Hello World from the Twilio SendGrid CSharp Library");
msg.AddContent(MimeType.Text, "Textual content");
msg.AddContent(MimeType.Html, "HTML content");
Assert.Equal("{\"from\":{\"email\":\"test@example.com\"},\"personalizations\":[{\"to\":[{\"email\":\"test@example.com\"}],\"subject\":\"Hello World from the Twilio SendGrid CSharp Library\"}],\"content\":[{\"type\":\"text/plain\",\"value\":\"Textual content\"},{\"type\":\"text/html\",\"value\":\"HTML content\"}]}", msg.Serialize());
// Test Hello World Example
var from = new EmailAddress("test@example.com", "Example User");
var subject = "Sending with Twilio SendGrid is Fun";
var to = new EmailAddress("test@example.com", "Example User");
var plainTextContent = "and easy to do anywhere, even with C#";
var htmlContent = "<strong>and easy to do anywhere, even with C#</strong>";
msg = MailHelper.CreateSingleEmail(from, to, subject, plainTextContent, htmlContent);
output.WriteLine(msg.Serialize());
Assert.Equal("{\"from\":{\"name\":\"Example User\",\"email\":\"test@example.com\"},\"personalizations\":[{\"to\":[{\"name\":\"Example User\",\"email\":\"test@example.com\"}],\"subject\":\"Sending with Twilio SendGrid is Fun\"}],\"content\":[{\"type\":\"text/plain\",\"value\":\"and easy to do anywhere, even with C#\"},{\"type\":\"text/html\",\"value\":\"\\u003cstrong\\u003eand easy to do anywhere, even with C#\\u003c/strong\\u003e\"}]}", msg.Serialize());
}
[Fact]
public void TestSendSingleEmailWithHelperWithOutEmailObject()
{
var msg = new SendGridMessage();
msg.SetFrom(new EmailAddress("test@example.com"));
msg.AddTo("test@example.com");
msg.SetSubject("Hello World from the Twilio SendGrid CSharp Library");
msg.AddContent(MimeType.Text, "Textual content");
msg.AddContent(MimeType.Html, "HTML content");
Assert.Equal("{\"from\":{\"email\":\"test@example.com\"},\"personalizations\":[{\"to\":[{\"email\":\"test@example.com\"}],\"subject\":\"Hello World from the Twilio SendGrid CSharp Library\"}],\"content\":[{\"type\":\"text/plain\",\"value\":\"Textual content\"},{\"type\":\"text/html\",\"value\":\"HTML content\"}]}", msg.Serialize());
msg = new SendGridMessage();
msg.SetFrom(new EmailAddress("test@example.com"));
msg.AddTo("test@example.com");
msg.SetSubject("Hello World from the Twilio SendGrid CSharp Library");
msg.AddContent(MimeType.Html, "HTML content");
Console.WriteLine(msg.Serialize());
}
// All paramaters available for sending an email
[Fact]
public void TestKitchenSink()
{
TestKitchenSinkInternal();
}
// All paramaters available for sending an email
private void TestKitchenSinkInternal(bool useDefaultSerialization = true)
{
var msg = new SendGridMessage();
msg.SetFrom(new EmailAddress("test1@example.com", "Example User1"));
msg.SetGlobalSubject("Hello World from the Twilio SendGrid CSharp Library");
msg.AddTo(new EmailAddress("test2@example.com", "Example User2"));
msg.AddTo("test-2@example.com", "Example User-2");
msg.AddTo(new EmailAddress("test3@example.com", "Example User3"));
var emails = new List<EmailAddress>
{
new EmailAddress("test4@example.com", "Example User4"),
new EmailAddress("test5@example.com", "Example User5")
};
msg.AddTos(emails);
msg.AddCc(new EmailAddress("test6@example.com", "Example User6"));
msg.AddCc(new EmailAddress("test7@example.com", "Example User7"));
emails = new List<EmailAddress>
{
new EmailAddress("test8@example.com", "Example User8"),
new EmailAddress("test9@example.com", "Example User9")
};
msg.AddCcs(emails);
msg.AddCc("test-9@example.com", "Example User-9");
msg.AddBcc(new EmailAddress("test10example.com", "Example User10"));
msg.AddBcc(new EmailAddress("test11@example.com", "Example User11"));
emails = new List<EmailAddress>
{
new EmailAddress("test12@example.com", "Example User12"),
new EmailAddress("test13@example.com", "Example User13")
};
msg.AddBccs(emails);
msg.AddBcc("test-13@example.com", "Example User-13");
msg.SetSubject("Thank you for signing up, % name %");
msg.AddHeader("X-Test1", "True1");
msg.AddHeader("X-Test2", "Test2");
var headers = new Dictionary<string, string>()
{
{ "X-Test3", "True3" },
{ "X-Test4", "True4" }
};
msg.AddHeaders(headers);
msg.AddSubstitution("%name1%", "Example User1");
msg.AddSubstitution("%city2%", "Denver1");
var substitutions = new Dictionary<string, string>()
{
{ "%name3%", "Example User2" },
{ "%city4%", "Orange1" }
};
msg.AddSubstitutions(substitutions);
msg.AddCustomArg("marketing1", "false");
msg.AddCustomArg("transactional1", "true");
var customArgs = new Dictionary<string, string>()
{
{ "marketing2", "true" },
{ "transactional2", "false" }
};
msg.AddCustomArgs(customArgs);
msg.SetSendAt(1461775051);
msg.AddTo(new EmailAddress("test14@example.com", "Example User14"), 1);
msg.AddTo(new EmailAddress("test15@example.com", "Example User15"), 1);
emails = new List<EmailAddress>
{
new EmailAddress("test16@example.com", "Example User16"),
new EmailAddress("test17@example.com", "Example User17")
};
msg.AddTos(emails, 1);
msg.AddCc(new EmailAddress("test18@example.com", "Example User18"), 1);
msg.AddCc(new EmailAddress("test19@example.com", "Example User19"), 1);
emails = new List<EmailAddress>
{
new EmailAddress("test20@example.com", "Example User20"),
new EmailAddress("test21@example.com", "Example User21")
};
msg.AddCcs(emails, 1);
msg.AddBcc(new EmailAddress("test22example.com", "Example User22"), 1);
msg.AddBcc(new EmailAddress("test23@example.com", "Example User23"), 1);
emails = new List<EmailAddress>
{
new EmailAddress("test24@example.com", "Example User24"),
new EmailAddress("test25@example.com", "Example User25")
};
msg.AddBccs(emails, 1);
msg.SetSubject("Thank you for signing up, % name % 2", 1);
msg.AddHeader("X-Test5", "True5", 1);
msg.AddHeader("X-Test6", "Test6", 1);
headers = new Dictionary<string, string>()
{
{ "X-Test7", "True7" },
{ "X-Test8", "True8" }
};
msg.AddHeaders(headers, 1);
msg.AddSubstitution("%name5%", "Example User5", 1);
msg.AddSubstitution("%city6%", "Denver6", 1);
substitutions = new Dictionary<string, string>()
{
{ "%name7%", "Example User7" },
{ "%city8%", "Orange8" }
};
msg.AddSubstitutions(substitutions, 1);
msg.AddCustomArg("marketing3", "false", 1);
msg.AddCustomArg("transactional3", "true", 1);
customArgs = new Dictionary<string, string>()
{
{ "marketing4", "true" },
{ "transactional4", "false" }
};
msg.AddCustomArgs(customArgs, 1);
msg.SetSendAt(1461775052, 1);
msg.AddTo(new EmailAddress("test26@example.com", "Example User26"), 2);
msg.AddTo(new EmailAddress("test27@example.com", "Example User27"), 2);
emails = new List<EmailAddress>
{
new EmailAddress("test28@example.com", "Example User28"),
new EmailAddress("test29@example.com", "Example User29")
};
msg.AddTos(emails, 2);
msg.AddCc(new EmailAddress("test30@example.com", "Example User30"), 2);
msg.AddCc(new EmailAddress("test31@example.com", "Example User31"), 2);
emails = new List<EmailAddress>
{
new EmailAddress("test32@example.com", "Example User32"),
new EmailAddress("test33@example.com", "Example User33")
};
msg.AddCcs(emails, 2);
msg.AddBcc(new EmailAddress("test34example.com", "Example User34"), 2);
msg.AddBcc(new EmailAddress("test35@example.com", "Example User35"), 2);
emails = new List<EmailAddress>
{
new EmailAddress("test36@example.com", "Example User36"),
new EmailAddress("test37@example.com", "Example User37")
};
msg.AddBccs(emails, 2);
msg.SetSubject("Thank you for signing up, % name % 3", 2);
msg.AddHeader("X-Test7", "True7", 2);
msg.AddHeader("X-Test8", "Test8", 2);
headers = new Dictionary<string, string>()
{
{ "X-Test9", "True9" },
{ "X-Test10", "True10" }
};
msg.AddHeaders(headers, 2);
msg.AddSubstitution("%name9%", "Example User9", 2);
msg.AddSubstitution("%city10%", "Denver10", 2);
substitutions = new Dictionary<string, string>()
{
{ "%name11%", "Example User11" },
{ "%city12%", "Orange12" }
};
msg.AddSubstitutions(substitutions, 2);
msg.AddCustomArg("marketing5", "false", 2);
msg.AddCustomArg("transactional5", "true", 2);
customArgs = new Dictionary<string, string>()
{
{ "marketing6", "true" },
{ "transactional6", "false" }
};
msg.AddCustomArgs(customArgs, 2);
msg.SetSendAt(1461775053, 2);
var contents = new List<Content>();
var content = new Content()
{
Type = "text/calendar",
Value = "Party Time!!"
};
contents.Add(content);
content = new Content()
{
Type = "text/calendar2",
Value = "Party Time2!!"
};
contents.Add(content);
msg.AddContents(contents);
msg.AddContent(MimeType.Html, "HTML content");
msg.AddContent(MimeType.Text, "Textual content");
msg.AddAttachment("balance_001.pdf",
"TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQsIGNvbnNlY3RldHVyIGFkaXBpc2NpbmcgZWxpdC4gQ3JhcyBwdW12",
"application/pdf",
"attachment",
"Balance Sheet");
var attachments = new List<Attachment>();
var attachment = new Attachment()
{
Content = "BwdW",
Type = "image/png",
Filename = "banner.png",
Disposition = "inline",
ContentId = "Banner"
};
attachments.Add(attachment);
attachment = new Attachment()
{
Content = "BwdW2",
Type = "image/png",
Filename = "banner2.png",
Disposition = "inline",
ContentId = "Banner 2"
};
attachments.Add(attachment);
msg.AddAttachments(attachments);
msg.SetTemplateId("13b8f94f-bcae-4ec6-b752-70d6cb59f932");
msg.AddGlobalHeader("X-Day", "Monday");
var globalHeaders = new Dictionary<string, string> { { "X-Month", "January" }, { "X-Year", "2017" } };
msg.AddGlobalHeaders(globalHeaders);
msg.AddSection("%section1", "Substitution for Section 1 Tag");
var sections = new Dictionary<string, string>
{
{"%section2%", "Substitution for Section 2 Tag"},
{"%section3%", "Substitution for Section 3 Tag"}
};
msg.AddSections(sections);
msg.AddCategory("customer");
var categories = new List<string> { "vip", "new_account" };
msg.AddCategories(categories);
msg.AddGlobalCustomArg("campaign", "welcome");
var globalCustomArgs = new Dictionary<string, string> { { "sequence2", "2" }, { "sequence3", "3" } };
msg.AddGlobalCustomArgs(globalCustomArgs);
msg.SetAsm(3, new List<int>() { 1, 4, 5 });
msg.SetGlobalSendAt(1461775051);
msg.SetIpPoolName("23");
// This must be a valid [batch ID](https://sendgrid.com/docs/API_Reference/SMTP_API/scheduling_parameters.html)
msg.SetBatchId("some_batch_id");
msg.SetBccSetting(true, "test@example.com");
msg.SetBypassListManagement(true);
msg.SetBypassSpamManagement(true);
msg.SetBypassBounceManagement(true);
msg.SetBypassUnsubscribeManagement(true);
msg.SetFooterSetting(true, "Some Footer HTML", "Some Footer Text");
msg.SetSandBoxMode(true);
msg.SetSpamCheck(true, 1, "https://gotchya.example.com");
msg.SetClickTracking(true, false);
msg.SetOpenTracking(true, "Optional tag to replace with the open image in the body of the message");
msg.SetSubscriptionTracking(true,
"HTML to insert into the text / html portion of the message",
"text to insert into the text/plain portion of the message",
"substitution tag");
msg.SetGoogleAnalytics(true,
"some campaign",
"some content",
"some medium",
"some source",
"some term");
msg.SetReplyTo(new EmailAddress("test+reply@example.com", "Reply To Me"));
Assert.Equal("{\"from\":{\"name\":\"Example User1\",\"email\":\"test1@example.com\"},\"subject\":\"Hello World from the Twilio SendGrid CSharp Library\",\"personalizations\":[{\"to\":[{\"name\":\"Example User2\",\"email\":\"test2@example.com\"},{\"name\":\"Example User-2\",\"email\":\"test-2@example.com\"},{\"name\":\"Example User3\",\"email\":\"test3@example.com\"},{\"name\":\"Example User4\",\"email\":\"test4@example.com\"},{\"name\":\"Example User5\",\"email\":\"test5@example.com\"}],\"cc\":[{\"name\":\"Example User6\",\"email\":\"test6@example.com\"},{\"name\":\"Example User7\",\"email\":\"test7@example.com\"},{\"name\":\"Example User8\",\"email\":\"test8@example.com\"},{\"name\":\"Example User9\",\"email\":\"test9@example.com\"},{\"name\":\"Example User-9\",\"email\":\"test-9@example.com\"}],\"bcc\":[{\"name\":\"Example User10\",\"email\":\"test10example.com\"},{\"name\":\"Example User11\",\"email\":\"test11@example.com\"},{\"name\":\"Example User12\",\"email\":\"test12@example.com\"},{\"name\":\"Example User13\",\"email\":\"test13@example.com\"},{\"name\":\"Example User-13\",\"email\":\"test-13@example.com\"}],\"subject\":\"Thank you for signing up, % name %\",\"headers\":{\"X-Test1\":\"True1\",\"X-Test2\":\"Test2\",\"X-Test3\":\"True3\",\"X-Test4\":\"True4\"},\"substitutions\":{\"%name1%\":\"Example User1\",\"%city2%\":\"Denver1\",\"%name3%\":\"Example User2\",\"%city4%\":\"Orange1\"},\"custom_args\":{\"marketing1\":\"false\",\"transactional1\":\"true\",\"marketing2\":\"true\",\"transactional2\":\"false\"},\"send_at\":1461775051},{\"to\":[{\"name\":\"Example User14\",\"email\":\"test14@example.com\"},{\"name\":\"Example User15\",\"email\":\"test15@example.com\"},{\"name\":\"Example User16\",\"email\":\"test16@example.com\"},{\"name\":\"Example User17\",\"email\":\"test17@example.com\"}],\"cc\":[{\"name\":\"Example User18\",\"email\":\"test18@example.com\"},{\"name\":\"Example User19\",\"email\":\"test19@example.com\"},{\"name\":\"Example User20\",\"email\":\"test20@example.com\"},{\"name\":\"Example User21\",\"email\":\"test21@example.com\"}],\"bcc\":[{\"name\":\"Example User22\",\"email\":\"test22example.com\"},{\"name\":\"Example User23\",\"email\":\"test23@example.com\"},{\"name\":\"Example User24\",\"email\":\"test24@example.com\"},{\"name\":\"Example User25\",\"email\":\"test25@example.com\"}],\"subject\":\"Thank you for signing up, % name % 2\",\"headers\":{\"X-Test5\":\"True5\",\"X-Test6\":\"Test6\",\"X-Test7\":\"True7\",\"X-Test8\":\"True8\"},\"substitutions\":{\"%name5%\":\"Example User5\",\"%city6%\":\"Denver6\",\"%name7%\":\"Example User7\",\"%city8%\":\"Orange8\"},\"custom_args\":{\"marketing3\":\"false\",\"transactional3\":\"true\",\"marketing4\":\"true\",\"transactional4\":\"false\"},\"send_at\":1461775052},{\"to\":[{\"name\":\"Example User26\",\"email\":\"test26@example.com\"},{\"name\":\"Example User27\",\"email\":\"test27@example.com\"},{\"name\":\"Example User28\",\"email\":\"test28@example.com\"},{\"name\":\"Example User29\",\"email\":\"test29@example.com\"}],\"cc\":[{\"name\":\"Example User30\",\"email\":\"test30@example.com\"},{\"name\":\"Example User31\",\"email\":\"test31@example.com\"},{\"name\":\"Example User32\",\"email\":\"test32@example.com\"},{\"name\":\"Example User33\",\"email\":\"test33@example.com\"}],\"bcc\":[{\"name\":\"Example User34\",\"email\":\"test34example.com\"},{\"name\":\"Example User35\",\"email\":\"test35@example.com\"},{\"name\":\"Example User36\",\"email\":\"test36@example.com\"},{\"name\":\"Example User37\",\"email\":\"test37@example.com\"}],\"subject\":\"Thank you for signing up, % name % 3\",\"headers\":{\"X-Test7\":\"True7\",\"X-Test8\":\"Test8\",\"X-Test9\":\"True9\",\"X-Test10\":\"True10\"},\"substitutions\":{\"%name9%\":\"Example User9\",\"%city10%\":\"Denver10\",\"%name11%\":\"Example User11\",\"%city12%\":\"Orange12\"},\"custom_args\":{\"marketing5\":\"false\",\"transactional5\":\"true\",\"marketing6\":\"true\",\"transactional6\":\"false\"},\"send_at\":1461775053}],\"content\":[{\"type\":\"text/plain\",\"value\":\"Textual content\"},{\"type\":\"text/html\",\"value\":\"HTML content\"},{\"type\":\"text/calendar\",\"value\":\"Party Time!!\"},{\"type\":\"text/calendar2\",\"value\":\"Party Time2!!\"}],\"attachments\":[{\"content\":\"TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQsIGNvbnNlY3RldHVyIGFkaXBpc2NpbmcgZWxpdC4gQ3JhcyBwdW12\",\"type\":\"application/pdf\",\"filename\":\"balance_001.pdf\",\"disposition\":\"attachment\",\"content_id\":\"Balance Sheet\"},{\"content\":\"BwdW\",\"type\":\"image/png\",\"filename\":\"banner.png\",\"disposition\":\"inline\",\"content_id\":\"Banner\"},{\"content\":\"BwdW2\",\"type\":\"image/png\",\"filename\":\"banner2.png\",\"disposition\":\"inline\",\"content_id\":\"Banner 2\"}],\"template_id\":\"13b8f94f-bcae-4ec6-b752-70d6cb59f932\",\"headers\":{\"X-Day\":\"Monday\",\"X-Month\":\"January\",\"X-Year\":\"2017\"},\"sections\":{\"%section1\":\"Substitution for Section 1 Tag\",\"%section2%\":\"Substitution for Section 2 Tag\",\"%section3%\":\"Substitution for Section 3 Tag\"},\"categories\":[\"customer\",\"vip\",\"new_account\"],\"custom_args\":{\"campaign\":\"welcome\",\"sequence2\":\"2\",\"sequence3\":\"3\"},\"send_at\":1461775051,\"asm\":{\"group_id\":3,\"groups_to_display\":[1,4,5]},\"batch_id\":\"some_batch_id\",\"ip_pool_name\":\"23\",\"mail_settings\":{\"bcc\":{\"enable\":true,\"email\":\"test@example.com\"},\"bypass_list_management\":{\"enable\":true},\"bypass_spam_management\":{\"enable\":true},\"bypass_bounce_management\":{\"enable\":true},\"bypass_unsubscribe_management\":{\"enable\":true},\"footer\":{\"enable\":true,\"text\":\"Some Footer Text\",\"html\":\"Some Footer HTML\"},\"sandbox_mode\":{\"enable\":true},\"spam_check\":{\"enable\":true,\"threshold\":1,\"post_to_url\":\"https://gotchya.example.com\"}},\"tracking_settings\":{\"click_tracking\":{\"enable\":true,\"enable_text\":false},\"open_tracking\":{\"enable\":true,\"substitution_tag\":\"Optional tag to replace with the open image in the body of the message\"},\"subscription_tracking\":{\"enable\":true,\"text\":\"text to insert into the text/plain portion of the message\",\"html\":\"HTML to insert into the text / html portion of the message\",\"substitution_tag\":\"substitution tag\"},\"ganalytics\":{\"enable\":true,\"utm_source\":\"some source\",\"utm_medium\":\"some medium\",\"utm_term\":\"some term\",\"utm_content\":\"some content\",\"utm_campaign\":\"some campaign\"}},\"reply_to\":{\"name\":\"Reply To Me\",\"email\":\"test+reply@example.com\"}}", msg.Serialize(useDefaultSerialization));
// Ensure serializing a SendGridMessage from deserialization generates the same JSON object to send to Twilio SendGrid.
var json = msg.Serialize(useDefaultSerialization);
var msg2 = SendGridMessage.Deserialize(json);
Assert.Equal(json, msg2.Serialize(useDefaultSerialization));
}
[Fact]
public void TestKitchenSinkIsUnaffectedByCustomContractResolver()
{
var originalGetDefaults = JsonConvert.DefaultSettings;
JsonConvert.DefaultSettings = () => new JsonSerializerSettings
{
ContractResolver = new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver()
};
try
{
TestKitchenSinkInternal(false);
// Ensure default behavior is not broken
var testObject = new
{
PropertyName = "PropertyValue",
Dictionary = new Dictionary<string, string> { { "DictionaryKey", "DictionaryValue" } }
};
Assert.Equal("{\"propertyName\":\"PropertyValue\",\"dictionary\":{\"dictionaryKey\":\"DictionaryValue\"}}", JsonConvert.SerializeObject(testObject));
}
finally
{
JsonConvert.DefaultSettings = originalGetDefaults;
}
}
[Fact]
public void TestCreateSingleEmail()
{
var msg = MailHelper.CreateSingleEmail(new EmailAddress("test@example.com", "Example User"),
new EmailAddress("test@example.com"),
"Test Subject",
"Plain Text Content",
"HTML Content");
Assert.Equal("{\"from\":{\"name\":\"Example User\",\"email\":\"test@example.com\"},\"personalizations\":[{\"to\":[{\"email\":\"test@example.com\"}],\"subject\":\"Test Subject\"}],\"content\":[{\"type\":\"text/plain\",\"value\":\"Plain Text Content\"},{\"type\":\"text/html\",\"value\":\"HTML Content\"}]}", msg.Serialize());
var msg2 = MailHelper.CreateSingleEmail(new EmailAddress("test@example.com", "Example User"),
new EmailAddress("test@example.com"),
"Test Subject",
null,
"HTML Content");
Assert.Equal("{\"from\":{\"name\":\"Example User\",\"email\":\"test@example.com\"},\"personalizations\":[{\"to\":[{\"email\":\"test@example.com\"}],\"subject\":\"Test Subject\"}],\"content\":[{\"type\":\"text/html\",\"value\":\"HTML Content\"}]}", msg2.Serialize());
var msg3 = MailHelper.CreateSingleEmail(new EmailAddress("test@example.com", "Example User"),
new EmailAddress("test@example.com"),
"Test Subject",
"Plain Text Content",
null);
Assert.Equal("{\"from\":{\"name\":\"Example User\",\"email\":\"test@example.com\"},\"personalizations\":[{\"to\":[{\"email\":\"test@example.com\"}],\"subject\":\"Test Subject\"}],\"content\":[{\"type\":\"text/plain\",\"value\":\"Plain Text Content\"}]}", msg3.Serialize());
var msg4 = MailHelper.CreateSingleEmail(new EmailAddress("test@example.com", "Example User"),
new EmailAddress("test@example.com"),
"Test Subject",
"",
"HTML Content");
Assert.Equal("{\"from\":{\"name\":\"Example User\",\"email\":\"test@example.com\"},\"personalizations\":[{\"to\":[{\"email\":\"test@example.com\"}],\"subject\":\"Test Subject\"}],\"content\":[{\"type\":\"text/html\",\"value\":\"HTML Content\"}]}", msg4.Serialize());
var msg5 = MailHelper.CreateSingleEmail(new EmailAddress("test@example.com", "Example User"),
new EmailAddress("test@example.com"),
"Test Subject",
"Plain Text Content",
"");
Assert.Equal("{\"from\":{\"name\":\"Example User\",\"email\":\"test@example.com\"},\"personalizations\":[{\"to\":[{\"email\":\"test@example.com\"}],\"subject\":\"Test Subject\"}],\"content\":[{\"type\":\"text/plain\",\"value\":\"Plain Text Content\"}]}", msg5.Serialize());
}
[Fact]
public void TestCreateSingleEmailToMultipleRecipients()
{
var emails = new List<EmailAddress>
{
new EmailAddress("test1@example.com"),
new EmailAddress("test2@example.com"),
new EmailAddress("test3@example.com")
};
var msg = MailHelper.CreateSingleEmailToMultipleRecipients(new EmailAddress("test@example.com", "Example User"),
emails,
"Test Subject",
"Plain Text Content",
"HTML Content"
);
Assert.Equal("{\"from\":{\"name\":\"Example User\",\"email\":\"test@example.com\"},\"subject\":\"Test Subject\",\"personalizations\":[{\"to\":[{\"email\":\"test1@example.com\"}]},{\"to\":[{\"email\":\"test2@example.com\"}]},{\"to\":[{\"email\":\"test3@example.com\"}]}],\"content\":[{\"type\":\"text/plain\",\"value\":\"Plain Text Content\"},{\"type\":\"text/html\",\"value\":\"HTML Content\"}]}", msg.Serialize());
var msg2 = MailHelper.CreateSingleEmailToMultipleRecipients(new EmailAddress("test@example.com", "Example User"),
emails,
"Test Subject",
null,
"HTML Content"
);
Assert.Equal("{\"from\":{\"name\":\"Example User\",\"email\":\"test@example.com\"},\"subject\":\"Test Subject\",\"personalizations\":[{\"to\":[{\"email\":\"test1@example.com\"}]},{\"to\":[{\"email\":\"test2@example.com\"}]},{\"to\":[{\"email\":\"test3@example.com\"}]}],\"content\":[{\"type\":\"text/html\",\"value\":\"HTML Content\"}]}", msg2.Serialize());
var msg3 = MailHelper.CreateSingleEmailToMultipleRecipients(new EmailAddress("test@example.com", "Example User"),
emails,
"Test Subject",
"Plain Text Content",
null
);
Assert.Equal("{\"from\":{\"name\":\"Example User\",\"email\":\"test@example.com\"},\"subject\":\"Test Subject\",\"personalizations\":[{\"to\":[{\"email\":\"test1@example.com\"}]},{\"to\":[{\"email\":\"test2@example.com\"}]},{\"to\":[{\"email\":\"test3@example.com\"}]}],\"content\":[{\"type\":\"text/plain\",\"value\":\"Plain Text Content\"}]}", msg3.Serialize());
var msg4 = MailHelper.CreateSingleEmailToMultipleRecipients(new EmailAddress("test@example.com", "Example User"),
emails,
"Test Subject",
"",
"HTML Content"
);
Assert.Equal("{\"from\":{\"name\":\"Example User\",\"email\":\"test@example.com\"},\"subject\":\"Test Subject\",\"personalizations\":[{\"to\":[{\"email\":\"test1@example.com\"}]},{\"to\":[{\"email\":\"test2@example.com\"}]},{\"to\":[{\"email\":\"test3@example.com\"}]}],\"content\":[{\"type\":\"text/html\",\"value\":\"HTML Content\"}]}", msg4.Serialize());
var msg5 = MailHelper.CreateSingleEmailToMultipleRecipients(new EmailAddress("test@example.com", "Example User"),
emails,
"Test Subject",
"Plain Text Content",
""
);
Assert.Equal("{\"from\":{\"name\":\"Example User\",\"email\":\"test@example.com\"},\"subject\":\"Test Subject\",\"personalizations\":[{\"to\":[{\"email\":\"test1@example.com\"}]},{\"to\":[{\"email\":\"test2@example.com\"}]},{\"to\":[{\"email\":\"test3@example.com\"}]}],\"content\":[{\"type\":\"text/plain\",\"value\":\"Plain Text Content\"}]}", msg5.Serialize());
}
[Fact]
public void TestCreateSingleEmailToMultipleRecipientsToggleRecipientDisplay()
{
var emails = new List<EmailAddress>
{
new EmailAddress("test1@example.com"),
new EmailAddress("test2@example.com"),
new EmailAddress("test3@example.com")
};
var msg = MailHelper.CreateSingleEmailToMultipleRecipients(new EmailAddress("test@example.com", "Example User"),
emails,
"Test Subject",
"Plain Text Content",
"HTML Content"
);
Assert.Equal("{\"from\":{\"name\":\"Example User\",\"email\":\"test@example.com\"},\"subject\":\"Test Subject\",\"personalizations\":[{\"to\":[{\"email\":\"test1@example.com\"}]},{\"to\":[{\"email\":\"test2@example.com\"}]},{\"to\":[{\"email\":\"test3@example.com\"}]}],\"content\":[{\"type\":\"text/plain\",\"value\":\"Plain Text Content\"},{\"type\":\"text/html\",\"value\":\"HTML Content\"}]}", msg.Serialize());
var msg2 = MailHelper.CreateSingleEmailToMultipleRecipients(new EmailAddress("test@example.com", "Example User"),
emails,
"Test Subject",
null,
"HTML Content"
);
Assert.Equal("{\"from\":{\"name\":\"Example User\",\"email\":\"test@example.com\"},\"subject\":\"Test Subject\",\"personalizations\":[{\"to\":[{\"email\":\"test1@example.com\"}]},{\"to\":[{\"email\":\"test2@example.com\"}]},{\"to\":[{\"email\":\"test3@example.com\"}]}],\"content\":[{\"type\":\"text/html\",\"value\":\"HTML Content\"}]}", msg2.Serialize());
var msg3 = MailHelper.CreateSingleEmailToMultipleRecipients(new EmailAddress("test@example.com", "Example User"),
emails,
"Test Subject",
"Plain Text Content",
null
);
Assert.Equal("{\"from\":{\"name\":\"Example User\",\"email\":\"test@example.com\"},\"subject\":\"Test Subject\",\"personalizations\":[{\"to\":[{\"email\":\"test1@example.com\"}]},{\"to\":[{\"email\":\"test2@example.com\"}]},{\"to\":[{\"email\":\"test3@example.com\"}]}],\"content\":[{\"type\":\"text/plain\",\"value\":\"Plain Text Content\"}]}", msg3.Serialize());
var msg4 = MailHelper.CreateSingleEmailToMultipleRecipients(new EmailAddress("test@example.com", "Example User"),
emails,
"Test Subject",
"",
"HTML Content"
);
Assert.Equal("{\"from\":{\"name\":\"Example User\",\"email\":\"test@example.com\"},\"subject\":\"Test Subject\",\"personalizations\":[{\"to\":[{\"email\":\"test1@example.com\"}]},{\"to\":[{\"email\":\"test2@example.com\"}]},{\"to\":[{\"email\":\"test3@example.com\"}]}],\"content\":[{\"type\":\"text/html\",\"value\":\"HTML Content\"}]}", msg4.Serialize());
var msg5 = MailHelper.CreateSingleEmailToMultipleRecipients(new EmailAddress("test@example.com", "Example User"),
emails,
"Test Subject",
"Plain Text Content",
""
);
Assert.Equal("{\"from\":{\"name\":\"Example User\",\"email\":\"test@example.com\"},\"subject\":\"Test Subject\",\"personalizations\":[{\"to\":[{\"email\":\"test1@example.com\"}]},{\"to\":[{\"email\":\"test2@example.com\"}]},{\"to\":[{\"email\":\"test3@example.com\"}]}],\"content\":[{\"type\":\"text/plain\",\"value\":\"Plain Text Content\"}]}", msg5.Serialize());
var msg6 = MailHelper.CreateSingleEmailToMultipleRecipients(new EmailAddress("test@example.com", "Example User"),
emails,
"Test Subject",
"Plain Text Content",
"HTML Content",
true
);
Assert.Equal("{\"from\":{\"name\":\"Example User\",\"email\":\"test@example.com\"},\"subject\":\"Test Subject\",\"personalizations\":[{\"to\":[{\"email\":\"test1@example.com\"},{\"email\":\"test2@example.com\"},{\"email\":\"test3@example.com\"}]}],\"content\":[{\"type\":\"text/plain\",\"value\":\"Plain Text Content\"},{\"type\":\"text/html\",\"value\":\"HTML Content\"}]}", msg6.Serialize());
}
[Fact]
public void TestCreateMultipleEmailsToMultipleRecipients()
{
var emails = new List<EmailAddress>
{
new EmailAddress("test1@example.com"),
new EmailAddress("test2@example.com"),
new EmailAddress("test3@example.com")
};
var subjects = new List<string> { "Test Subject1", "Test Subject2", "Test Subject3" };
var plainTextContent = "Hello -name-";
var htmlContent = "Goodbye -name-";
var substitutions = new List<Dictionary<string, string>>
{
new Dictionary<string, string>() {{"-name-", "Name1"}},
new Dictionary<string, string>() {{"-name-", "Name1"}},
new Dictionary<string, string>() {{"-name-", "Name1"}}
};
var msg = MailHelper.CreateMultipleEmailsToMultipleRecipients(new EmailAddress("test@example.com", "Example User"),
emails,
subjects,
plainTextContent,
htmlContent,
substitutions
);
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-\"},{\"type\":\"text/html\",\"value\":\"Goodbye -name-\"}]}", msg.Serialize());
plainTextContent = null;
htmlContent = "Goodbye -name-";
var msg2 = MailHelper.CreateMultipleEmailsToMultipleRecipients(new EmailAddress("test@example.com", "Example User"),
emails,
subjects,
plainTextContent,
htmlContent,
substitutions
);
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/html\",\"value\":\"Goodbye -name-\"}]}", msg2.Serialize());
plainTextContent = "Hello -name-";
htmlContent = null;
var msg3 = MailHelper.CreateMultipleEmailsToMultipleRecipients(new EmailAddress("test@example.com", "Example User"),
emails,
subjects,
plainTextContent,
htmlContent,
substitutions
);
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-\"}]}", msg3.Serialize());
plainTextContent = "";
htmlContent = "Goodbye -name-";
var msg4 = MailHelper.CreateMultipleEmailsToMultipleRecipients(new EmailAddress("test@example.com", "Example User"),
emails,
subjects,
plainTextContent,
htmlContent,
substitutions
);
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/html\",\"value\":\"Goodbye -name-\"}]}", msg4.Serialize());
plainTextContent = "Hello -name-";
htmlContent = "";
var msg5 = MailHelper.CreateMultipleEmailsToMultipleRecipients(new EmailAddress("test@example.com", "Example User"),
emails,
subjects,
plainTextContent,
htmlContent,
substitutions
);
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()
{
// Personalization not passed in, Personalization does not exist
var msg = new SendGridMessage();
msg.AddTo(new EmailAddress("test001@example.com", "Example User"));
Assert.Equal("{\"personalizations\":[{\"to\":[{\"name\":\"Example User\",\"email\":\"test001@example.com\"}]}]}", msg.Serialize());
// Personalization passed in, no Personalizations
msg = new SendGridMessage();
var email = new EmailAddress("test002@example.com", "Example User");
var personalization = new Personalization()
{
Tos = new List<EmailAddress>()
{
email
}
};
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\"}]}]}", msg.Serialize());
// Personalization passed in, Personalization exists
msg = new SendGridMessage();
email = new EmailAddress("test004@example.com", "Example User");
msg.Personalizations = new List<Personalization>() {
new Personalization() {
Tos = new List<EmailAddress>()
{
email
}
}
};
email = new EmailAddress("test005@example.com", "Example User");
personalization = new Personalization()
{
Tos = new List<EmailAddress>()
{
email
}
};
msg.AddTo(new EmailAddress("test006@example.com", "Example User"), 1, personalization);
Assert.Equal("{\"personalizations\":[{\"to\":[{\"name\":\"Example User\",\"email\":\"test004@example.com\"}]},{\"to\":[{\"name\":\"Example User\",\"email\":\"test005@example.com\"},{\"name\":\"Example User\",\"email\":\"test006@example.com\"}]}]}", msg.Serialize());
// Personalization not passed in Personalization exists
msg = new SendGridMessage();
email = new EmailAddress("test007@example.com", "Example User");
msg.Personalizations = new List<Personalization>() {
new Personalization() {
Tos = new List<EmailAddress>()
{
email
}
}
};
msg.AddTo(new EmailAddress("test008@example.com", "Example User"));
Assert.Equal("{\"personalizations\":[{\"to\":[{\"name\":\"Example User\",\"email\":\"test007@example.com\"},{\"name\":\"Example User\",\"email\":\"test008@example.com\"}]}]}", msg.Serialize());
// Personalization not passed in Personalizations exists
msg = new SendGridMessage();
email = new EmailAddress("test009@example.com", "Example User");
msg.Personalizations = new List<Personalization>() {
new Personalization() {
Tos = new List<EmailAddress>()
{
email
}
}
};
email = new EmailAddress("test010@example.com", "Example User");
personalization = new Personalization()
{
Tos = new List<EmailAddress>()
{
email
}
};
msg.Personalizations.Add(personalization);
msg.AddTo(new EmailAddress("test011@example.com", "Example User"));
Assert.Equal("{\"personalizations\":[{\"to\":[{\"name\":\"Example User\",\"email\":\"test009@example.com\"},{\"name\":\"Example User\",\"email\":\"test011@example.com\"}]},{\"to\":[{\"name\":\"Example User\",\"email\":\"test010@example.com\"}]}]}", msg.Serialize());
}
[Fact]
public void TestAddToWithOutEmailAddressObject()
{
var msg = new SendGridMessage();
msg.AddTo("test001@example.com", "Example User");
Assert.Equal("{\"personalizations\":[{\"to\":[{\"name\":\"Example User\",\"email\":\"test001@example.com\"}]}]}", msg.Serialize());
msg = new SendGridMessage();
msg.AddTo("test001@example.com");
Assert.Equal("{\"personalizations\":[{\"to\":[{\"email\":\"test001@example.com\"}]}]}", msg.Serialize());
}
[Fact]
public void TestAddToArgumentNullExceptionIfNullEmailAddressIsSupplied()
{
var msg = new SendGridMessage();
Assert.Throws<ArgumentNullException>(() => msg.AddTo(null, "Example User"));
}
[Fact]
public void TestAddToArgumentNullExceptionIfNoEmailAddressIsSupplied()
{
var msg = new SendGridMessage();
Assert.Throws<ArgumentNullException>(() => msg.AddTo(string.Empty, "Example User"));
}
[Fact]
public void TestAddTos()
{
// Personalization not passed in, Personalization does not exist
var msg = new SendGridMessage();
var emails = new List<EmailAddress>
{
new EmailAddress("test012@example.com", "Example User"),
new EmailAddress("test013@example.com", "Example User")
};
msg.AddTos(emails);
Assert.Equal("{\"personalizations\":[{\"to\":[{\"name\":\"Example User\",\"email\":\"test012@example.com\"},{\"name\":\"Example User\",\"email\":\"test013@example.com\"}]}]}", msg.Serialize());
// Personalization passed in, no Personalizations
msg = new SendGridMessage();
emails = new List<EmailAddress>
{
new EmailAddress("test014@example.com", "Example User"),
new EmailAddress("test015@example.com", "Example User")
};
var personalization = new Personalization()
{
Tos = emails
};
emails = new List<EmailAddress>
{
new EmailAddress("test016@example.com", "Example User"),
new EmailAddress("test017@example.com", "Example User")
};
msg.AddTos(emails, 0, personalization);
Assert.Equal("{\"personalizations\":[{\"to\":[{\"name\":\"Example User\",\"email\":\"test014@example.com\"},{\"name\":\"Example User\",\"email\":\"test015@example.com\"},{\"name\":\"Example User\",\"email\":\"test016@example.com\"},{\"name\":\"Example User\",\"email\":\"test017@example.com\"}]}]}", msg.Serialize());
// Personalization passed in, Personalization exists
msg = new SendGridMessage();
emails = new List<EmailAddress>
{
new EmailAddress("test018@example.com", "Example User"),
new EmailAddress("test019@example.com", "Example User")
};
msg.Personalizations = new List<Personalization>() {
new Personalization() {
Tos = emails
}
};
emails = new List<EmailAddress>
{
new EmailAddress("test020@example.com", "Example User"),
new EmailAddress("test021@example.com", "Example User")
};
personalization = new Personalization()
{
Tos = emails
};
emails = new List<EmailAddress>
{
new EmailAddress("test022@example.com", "Example User"),
new EmailAddress("test023@example.com", "Example User")
};
msg.AddTos(emails, 1, personalization);
Assert.Equal("{\"personalizations\":[{\"to\":[{\"name\":\"Example User\",\"email\":\"test018@example.com\"},{\"name\":\"Example User\",\"email\":\"test019@example.com\"}]},{\"to\":[{\"name\":\"Example User\",\"email\":\"test020@example.com\"},{\"name\":\"Example User\",\"email\":\"test021@example.com\"},{\"name\":\"Example User\",\"email\":\"test022@example.com\"},{\"name\":\"Example User\",\"email\":\"test023@example.com\"}]}]}", msg.Serialize());
// Personalization not passed in Personalization exists
msg = new SendGridMessage();
emails = new List<EmailAddress>
{
new EmailAddress("test024@example.com", "Example User"),
new EmailAddress("test025@example.com", "Example User")
};
msg.Personalizations = new List<Personalization>() {
new Personalization() {
Tos = emails
}
};
emails = new List<EmailAddress>
{
new EmailAddress("test026@example.com", "Example User"),
new EmailAddress("test027@example.com", "Example User")
};
msg.AddTos(emails);
Assert.Equal("{\"personalizations\":[{\"to\":[{\"name\":\"Example User\",\"email\":\"test024@example.com\"},{\"name\":\"Example User\",\"email\":\"test025@example.com\"},{\"name\":\"Example User\",\"email\":\"test026@example.com\"},{\"name\":\"Example User\",\"email\":\"test027@example.com\"}]}]}", msg.Serialize());
// Personalization not passed in Personalizations exists
msg = new SendGridMessage();
emails = new List<EmailAddress>
{
new EmailAddress("test028@example.com", "Example User"),
new EmailAddress("test029@example.com", "Example User")
};
msg.Personalizations = new List<Personalization>() {
new Personalization() {
Tos = emails
}
};
emails = new List<EmailAddress>
{
new EmailAddress("test030@example.com", "Example User"),
new EmailAddress("test031@example.com", "Example User")
};
personalization = new Personalization()
{
Tos = emails
};
msg.Personalizations.Add(personalization);
emails = new List<EmailAddress>
{
new EmailAddress("test032@example.com", "Example User"),
new EmailAddress("test033@example.com", "Example User")
};
msg.AddTos(emails);
Assert.Equal("{\"personalizations\":[{\"to\":[{\"name\":\"Example User\",\"email\":\"test028@example.com\"},{\"name\":\"Example User\",\"email\":\"test029@example.com\"},{\"name\":\"Example User\",\"email\":\"test032@example.com\"},{\"name\":\"Example User\",\"email\":\"test033@example.com\"}]},{\"to\":[{\"name\":\"Example User\",\"email\":\"test030@example.com\"},{\"name\":\"Example User\",\"email\":\"test031@example.com\"}]}]}", msg.Serialize());
}
[Fact]
public void TestAddCcWithoutEmailAddressObject()
{
var msg = new SendGridMessage();
msg.AddCc("test001@example.com", "Example User");
Assert.Equal("{\"personalizations\":[{\"cc\":[{\"name\":\"Example User\",\"email\":\"test001@example.com\"}]}]}", msg.Serialize());
msg = new SendGridMessage();
msg.AddCc("test001@example.com");
Assert.Equal("{\"personalizations\":[{\"cc\":[{\"email\":\"test001@example.com\"}]}]}", msg.Serialize());
}
[Fact]
public void TestAddCcArgumentNullExceptionIfNullEmailAddressIsSupplied()
{
var msg = new SendGridMessage();
Assert.Throws<ArgumentNullException>(() => msg.AddCc(null, "Example User"));
}
[Fact]
public void TestAddCcArgumentNullExceptionIfEmptyEmailAddressIsSupplied()
{
var msg = new SendGridMessage();
Assert.Throws<ArgumentNullException>(() => msg.AddCc(string.Empty, "Example User"));
}
[Fact]
public void TestAddCc()
{
// Personalization not passed in, Personalization does not exist
var msg = new SendGridMessage();
msg.AddCc(new EmailAddress("test001@example.com", "Example User"));
Assert.Equal("{\"personalizations\":[{\"cc\":[{\"name\":\"Example User\",\"email\":\"test001@example.com\"}]}]}", msg.Serialize());
// Personalization passed in, no Personalizations
msg = new SendGridMessage();
var email = new EmailAddress("test002@example.com", "Example User");
var personalization = new Personalization()
{
Ccs = new List<EmailAddress>()
{
email
}
};
msg.AddCc(new EmailAddress("test003@example.com", "Example User"), 0, personalization);
Assert.Equal("{\"personalizations\":[{\"cc\":[{\"name\":\"Example User\",\"email\":\"test002@example.com\"},{\"name\":\"Example User\",\"email\":\"test003@example.com\"}]}]}", msg.Serialize());
// Personalization passed in, Personalization exists
msg = new SendGridMessage();
email = new EmailAddress("test004@example.com", "Example User");
msg.Personalizations = new List<Personalization>() {
new Personalization() {
Ccs = new List<EmailAddress>()
{
email
}
}
};
email = new EmailAddress("test005@example.com", "Example User");
personalization = new Personalization()
{
Ccs = new List<EmailAddress>()
{
email
}
};
msg.AddCc(new EmailAddress("test006@example.com", "Example User"), 1, personalization);
Assert.Equal("{\"personalizations\":[{\"cc\":[{\"name\":\"Example User\",\"email\":\"test004@example.com\"}]},{\"cc\":[{\"name\":\"Example User\",\"email\":\"test005@example.com\"},{\"name\":\"Example User\",\"email\":\"test006@example.com\"}]}]}", msg.Serialize());
// Personalization not passed in Personalization exists
msg = new SendGridMessage();
email = new EmailAddress("test007@example.com", "Example User");
msg.Personalizations = new List<Personalization>() {
new Personalization() {
Ccs = new List<EmailAddress>()
{
email
}
}
};
msg.AddCc(new EmailAddress("test008@example.com", "Example User"));
Assert.Equal("{\"personalizations\":[{\"cc\":[{\"name\":\"Example User\",\"email\":\"test007@example.com\"},{\"name\":\"Example User\",\"email\":\"test008@example.com\"}]}]}", msg.Serialize());
// Personalization not passed in Personalizations exists
msg = new SendGridMessage();
email = new EmailAddress("test009@example.com", "Example User");
msg.Personalizations = new List<Personalization>() {
new Personalization() {
Ccs = new List<EmailAddress>()
{
email
}
}
};
email = new EmailAddress("test010@example.com", "Example User");
personalization = new Personalization()