forked from sendgrid/sendgrid-csharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Integration.cs
6136 lines (5636 loc) · 264 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("[email protected]"));
msg.AddTo(new EmailAddress("[email protected]"));
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\":\"[email protected]\"},\"personalizations\":[{\"to\":[{\"email\":\"[email protected]\"}],\"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("[email protected]", "Example User");
var subject = "Sending with Twilio SendGrid is Fun";
var to = new EmailAddress("[email protected]", "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\":\"[email protected]\"},\"personalizations\":[{\"to\":[{\"name\":\"Example User\",\"email\":\"[email protected]\"}],\"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("[email protected]"));
msg.AddTo("[email protected]");
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\":\"[email protected]\"},\"personalizations\":[{\"to\":[{\"email\":\"[email protected]\"}],\"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("[email protected]"));
msg.AddTo("[email protected]");
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("[email protected]", "Example User1"));
msg.SetGlobalSubject("Hello World from the Twilio SendGrid CSharp Library");
msg.AddTo(new EmailAddress("[email protected]", "Example User2"));
msg.AddTo("[email protected]", "Example User-2");
msg.AddTo(new EmailAddress("[email protected]", "Example User3"));
var emails = new List<EmailAddress>
{
new EmailAddress("[email protected]", "Example User4"),
new EmailAddress("[email protected]", "Example User5")
};
msg.AddTos(emails);
msg.AddCc(new EmailAddress("[email protected]", "Example User6"));
msg.AddCc(new EmailAddress("[email protected]", "Example User7"));
emails = new List<EmailAddress>
{
new EmailAddress("[email protected]", "Example User8"),
new EmailAddress("[email protected]", "Example User9")
};
msg.AddCcs(emails);
msg.AddCc("[email protected]", "Example User-9");
msg.AddBcc(new EmailAddress("test10example.com", "Example User10"));
msg.AddBcc(new EmailAddress("[email protected]", "Example User11"));
emails = new List<EmailAddress>
{
new EmailAddress("[email protected]", "Example User12"),
new EmailAddress("[email protected]", "Example User13")
};
msg.AddBccs(emails);
msg.AddBcc("[email protected]", "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("[email protected]", "Example User14"), 1);
msg.AddTo(new EmailAddress("[email protected]", "Example User15"), 1);
emails = new List<EmailAddress>
{
new EmailAddress("[email protected]", "Example User16"),
new EmailAddress("[email protected]", "Example User17")
};
msg.AddTos(emails, 1);
msg.AddCc(new EmailAddress("[email protected]", "Example User18"), 1);
msg.AddCc(new EmailAddress("[email protected]", "Example User19"), 1);
emails = new List<EmailAddress>
{
new EmailAddress("[email protected]", "Example User20"),
new EmailAddress("[email protected]", "Example User21")
};
msg.AddCcs(emails, 1);
msg.AddBcc(new EmailAddress("test22example.com", "Example User22"), 1);
msg.AddBcc(new EmailAddress("[email protected]", "Example User23"), 1);
emails = new List<EmailAddress>
{
new EmailAddress("[email protected]", "Example User24"),
new EmailAddress("[email protected]", "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("[email protected]", "Example User26"), 2);
msg.AddTo(new EmailAddress("[email protected]", "Example User27"), 2);
emails = new List<EmailAddress>
{
new EmailAddress("[email protected]", "Example User28"),
new EmailAddress("[email protected]", "Example User29")
};
msg.AddTos(emails, 2);
msg.AddCc(new EmailAddress("[email protected]", "Example User30"), 2);
msg.AddCc(new EmailAddress("[email protected]", "Example User31"), 2);
emails = new List<EmailAddress>
{
new EmailAddress("[email protected]", "Example User32"),
new EmailAddress("[email protected]", "Example User33")
};
msg.AddCcs(emails, 2);
msg.AddBcc(new EmailAddress("test34example.com", "Example User34"), 2);
msg.AddBcc(new EmailAddress("[email protected]", "Example User35"), 2);
emails = new List<EmailAddress>
{
new EmailAddress("[email protected]", "Example User36"),
new EmailAddress("[email protected]", "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, "[email protected]");
msg.SetBypassListManagement(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("[email protected]", "Reply To Me"));
Assert.Equal("{\"from\":{\"name\":\"Example User1\",\"email\":\"[email protected]\"},\"subject\":\"Hello World from the Twilio SendGrid CSharp Library\",\"personalizations\":[{\"to\":[{\"name\":\"Example User2\",\"email\":\"[email protected]\"},{\"name\":\"Example User-2\",\"email\":\"[email protected]\"},{\"name\":\"Example User3\",\"email\":\"[email protected]\"},{\"name\":\"Example User4\",\"email\":\"[email protected]\"},{\"name\":\"Example User5\",\"email\":\"[email protected]\"}],\"cc\":[{\"name\":\"Example User6\",\"email\":\"[email protected]\"},{\"name\":\"Example User7\",\"email\":\"[email protected]\"},{\"name\":\"Example User8\",\"email\":\"[email protected]\"},{\"name\":\"Example User9\",\"email\":\"[email protected]\"},{\"name\":\"Example User-9\",\"email\":\"[email protected]\"}],\"bcc\":[{\"name\":\"Example User10\",\"email\":\"test10example.com\"},{\"name\":\"Example User11\",\"email\":\"[email protected]\"},{\"name\":\"Example User12\",\"email\":\"[email protected]\"},{\"name\":\"Example User13\",\"email\":\"[email protected]\"},{\"name\":\"Example User-13\",\"email\":\"[email protected]\"}],\"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\":\"[email protected]\"},{\"name\":\"Example User15\",\"email\":\"[email protected]\"},{\"name\":\"Example User16\",\"email\":\"[email protected]\"},{\"name\":\"Example User17\",\"email\":\"[email protected]\"}],\"cc\":[{\"name\":\"Example User18\",\"email\":\"[email protected]\"},{\"name\":\"Example User19\",\"email\":\"[email protected]\"},{\"name\":\"Example User20\",\"email\":\"[email protected]\"},{\"name\":\"Example User21\",\"email\":\"[email protected]\"}],\"bcc\":[{\"name\":\"Example User22\",\"email\":\"test22example.com\"},{\"name\":\"Example User23\",\"email\":\"[email protected]\"},{\"name\":\"Example User24\",\"email\":\"[email protected]\"},{\"name\":\"Example User25\",\"email\":\"[email protected]\"}],\"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\":\"[email protected]\"},{\"name\":\"Example User27\",\"email\":\"[email protected]\"},{\"name\":\"Example User28\",\"email\":\"[email protected]\"},{\"name\":\"Example User29\",\"email\":\"[email protected]\"}],\"cc\":[{\"name\":\"Example User30\",\"email\":\"[email protected]\"},{\"name\":\"Example User31\",\"email\":\"[email protected]\"},{\"name\":\"Example User32\",\"email\":\"[email protected]\"},{\"name\":\"Example User33\",\"email\":\"[email protected]\"}],\"bcc\":[{\"name\":\"Example User34\",\"email\":\"test34example.com\"},{\"name\":\"Example User35\",\"email\":\"[email protected]\"},{\"name\":\"Example User36\",\"email\":\"[email protected]\"},{\"name\":\"Example User37\",\"email\":\"[email protected]\"}],\"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\":\"[email protected]\"},\"bypass_list_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\":\"[email protected]\"}}", msg.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("[email protected]", "Example User"),
new EmailAddress("[email protected]"),
"Test Subject",
"Plain Text Content",
"HTML Content");
Assert.Equal("{\"from\":{\"name\":\"Example User\",\"email\":\"[email protected]\"},\"personalizations\":[{\"to\":[{\"email\":\"[email protected]\"}],\"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("[email protected]", "Example User"),
new EmailAddress("[email protected]"),
"Test Subject",
null,
"HTML Content");
Assert.Equal("{\"from\":{\"name\":\"Example User\",\"email\":\"[email protected]\"},\"personalizations\":[{\"to\":[{\"email\":\"[email protected]\"}],\"subject\":\"Test Subject\"}],\"content\":[{\"type\":\"text/html\",\"value\":\"HTML Content\"}]}", msg2.Serialize());
var msg3 = MailHelper.CreateSingleEmail(new EmailAddress("[email protected]", "Example User"),
new EmailAddress("[email protected]"),
"Test Subject",
"Plain Text Content",
null);
Assert.Equal("{\"from\":{\"name\":\"Example User\",\"email\":\"[email protected]\"},\"personalizations\":[{\"to\":[{\"email\":\"[email protected]\"}],\"subject\":\"Test Subject\"}],\"content\":[{\"type\":\"text/plain\",\"value\":\"Plain Text Content\"}]}", msg3.Serialize());
var msg4 = MailHelper.CreateSingleEmail(new EmailAddress("[email protected]", "Example User"),
new EmailAddress("[email protected]"),
"Test Subject",
"",
"HTML Content");
Assert.Equal("{\"from\":{\"name\":\"Example User\",\"email\":\"[email protected]\"},\"personalizations\":[{\"to\":[{\"email\":\"[email protected]\"}],\"subject\":\"Test Subject\"}],\"content\":[{\"type\":\"text/html\",\"value\":\"HTML Content\"}]}", msg4.Serialize());
var msg5 = MailHelper.CreateSingleEmail(new EmailAddress("[email protected]", "Example User"),
new EmailAddress("[email protected]"),
"Test Subject",
"Plain Text Content",
"");
Assert.Equal("{\"from\":{\"name\":\"Example User\",\"email\":\"[email protected]\"},\"personalizations\":[{\"to\":[{\"email\":\"[email protected]\"}],\"subject\":\"Test Subject\"}],\"content\":[{\"type\":\"text/plain\",\"value\":\"Plain Text Content\"}]}", msg5.Serialize());
}
[Fact]
public void TestCreateSingleEmailToMultipleRecipients()
{
var emails = new List<EmailAddress>
{
new EmailAddress("[email protected]"),
new EmailAddress("[email protected]"),
new EmailAddress("[email protected]")
};
var msg = MailHelper.CreateSingleEmailToMultipleRecipients(new EmailAddress("[email protected]", "Example User"),
emails,
"Test Subject",
"Plain Text Content",
"HTML Content"
);
Assert.Equal("{\"from\":{\"name\":\"Example User\",\"email\":\"[email protected]\"},\"subject\":\"Test Subject\",\"personalizations\":[{\"to\":[{\"email\":\"[email protected]\"}]},{\"to\":[{\"email\":\"[email protected]\"}]},{\"to\":[{\"email\":\"[email protected]\"}]}],\"content\":[{\"type\":\"text/plain\",\"value\":\"Plain Text Content\"},{\"type\":\"text/html\",\"value\":\"HTML Content\"}]}", msg.Serialize());
var msg2 = MailHelper.CreateSingleEmailToMultipleRecipients(new EmailAddress("[email protected]", "Example User"),
emails,
"Test Subject",
null,
"HTML Content"
);
Assert.Equal("{\"from\":{\"name\":\"Example User\",\"email\":\"[email protected]\"},\"subject\":\"Test Subject\",\"personalizations\":[{\"to\":[{\"email\":\"[email protected]\"}]},{\"to\":[{\"email\":\"[email protected]\"}]},{\"to\":[{\"email\":\"[email protected]\"}]}],\"content\":[{\"type\":\"text/html\",\"value\":\"HTML Content\"}]}", msg2.Serialize());
var msg3 = MailHelper.CreateSingleEmailToMultipleRecipients(new EmailAddress("[email protected]", "Example User"),
emails,
"Test Subject",
"Plain Text Content",
null
);
Assert.Equal("{\"from\":{\"name\":\"Example User\",\"email\":\"[email protected]\"},\"subject\":\"Test Subject\",\"personalizations\":[{\"to\":[{\"email\":\"[email protected]\"}]},{\"to\":[{\"email\":\"[email protected]\"}]},{\"to\":[{\"email\":\"[email protected]\"}]}],\"content\":[{\"type\":\"text/plain\",\"value\":\"Plain Text Content\"}]}", msg3.Serialize());
var msg4 = MailHelper.CreateSingleEmailToMultipleRecipients(new EmailAddress("[email protected]", "Example User"),
emails,
"Test Subject",
"",
"HTML Content"
);
Assert.Equal("{\"from\":{\"name\":\"Example User\",\"email\":\"[email protected]\"},\"subject\":\"Test Subject\",\"personalizations\":[{\"to\":[{\"email\":\"[email protected]\"}]},{\"to\":[{\"email\":\"[email protected]\"}]},{\"to\":[{\"email\":\"[email protected]\"}]}],\"content\":[{\"type\":\"text/html\",\"value\":\"HTML Content\"}]}", msg4.Serialize());
var msg5 = MailHelper.CreateSingleEmailToMultipleRecipients(new EmailAddress("[email protected]", "Example User"),
emails,
"Test Subject",
"Plain Text Content",
""
);
Assert.Equal("{\"from\":{\"name\":\"Example User\",\"email\":\"[email protected]\"},\"subject\":\"Test Subject\",\"personalizations\":[{\"to\":[{\"email\":\"[email protected]\"}]},{\"to\":[{\"email\":\"[email protected]\"}]},{\"to\":[{\"email\":\"[email protected]\"}]}],\"content\":[{\"type\":\"text/plain\",\"value\":\"Plain Text Content\"}]}", msg5.Serialize());
}
[Fact]
public void TestCreateSingleEmailToMultipleRecipientsToggleRecipientDisplay()
{
var emails = new List<EmailAddress>
{
new EmailAddress("[email protected]"),
new EmailAddress("[email protected]"),
new EmailAddress("[email protected]")
};
var msg = MailHelper.CreateSingleEmailToMultipleRecipients(new EmailAddress("[email protected]", "Example User"),
emails,
"Test Subject",
"Plain Text Content",
"HTML Content"
);
Assert.Equal("{\"from\":{\"name\":\"Example User\",\"email\":\"[email protected]\"},\"subject\":\"Test Subject\",\"personalizations\":[{\"to\":[{\"email\":\"[email protected]\"}]},{\"to\":[{\"email\":\"[email protected]\"}]},{\"to\":[{\"email\":\"[email protected]\"}]}],\"content\":[{\"type\":\"text/plain\",\"value\":\"Plain Text Content\"},{\"type\":\"text/html\",\"value\":\"HTML Content\"}]}", msg.Serialize());
var msg2 = MailHelper.CreateSingleEmailToMultipleRecipients(new EmailAddress("[email protected]", "Example User"),
emails,
"Test Subject",
null,
"HTML Content"
);
Assert.Equal("{\"from\":{\"name\":\"Example User\",\"email\":\"[email protected]\"},\"subject\":\"Test Subject\",\"personalizations\":[{\"to\":[{\"email\":\"[email protected]\"}]},{\"to\":[{\"email\":\"[email protected]\"}]},{\"to\":[{\"email\":\"[email protected]\"}]}],\"content\":[{\"type\":\"text/html\",\"value\":\"HTML Content\"}]}", msg2.Serialize());
var msg3 = MailHelper.CreateSingleEmailToMultipleRecipients(new EmailAddress("[email protected]", "Example User"),
emails,
"Test Subject",
"Plain Text Content",
null
);
Assert.Equal("{\"from\":{\"name\":\"Example User\",\"email\":\"[email protected]\"},\"subject\":\"Test Subject\",\"personalizations\":[{\"to\":[{\"email\":\"[email protected]\"}]},{\"to\":[{\"email\":\"[email protected]\"}]},{\"to\":[{\"email\":\"[email protected]\"}]}],\"content\":[{\"type\":\"text/plain\",\"value\":\"Plain Text Content\"}]}", msg3.Serialize());
var msg4 = MailHelper.CreateSingleEmailToMultipleRecipients(new EmailAddress("[email protected]", "Example User"),
emails,
"Test Subject",
"",
"HTML Content"
);
Assert.Equal("{\"from\":{\"name\":\"Example User\",\"email\":\"[email protected]\"},\"subject\":\"Test Subject\",\"personalizations\":[{\"to\":[{\"email\":\"[email protected]\"}]},{\"to\":[{\"email\":\"[email protected]\"}]},{\"to\":[{\"email\":\"[email protected]\"}]}],\"content\":[{\"type\":\"text/html\",\"value\":\"HTML Content\"}]}", msg4.Serialize());
var msg5 = MailHelper.CreateSingleEmailToMultipleRecipients(new EmailAddress("[email protected]", "Example User"),
emails,
"Test Subject",
"Plain Text Content",
""
);
Assert.Equal("{\"from\":{\"name\":\"Example User\",\"email\":\"[email protected]\"},\"subject\":\"Test Subject\",\"personalizations\":[{\"to\":[{\"email\":\"[email protected]\"}]},{\"to\":[{\"email\":\"[email protected]\"}]},{\"to\":[{\"email\":\"[email protected]\"}]}],\"content\":[{\"type\":\"text/plain\",\"value\":\"Plain Text Content\"}]}", msg5.Serialize());
var msg6 = MailHelper.CreateSingleEmailToMultipleRecipients(new EmailAddress("[email protected]", "Example User"),
emails,
"Test Subject",
"Plain Text Content",
"HTML Content",
true
);
Assert.Equal("{\"from\":{\"name\":\"Example User\",\"email\":\"[email protected]\"},\"subject\":\"Test Subject\",\"personalizations\":[{\"to\":[{\"email\":\"[email protected]\"},{\"email\":\"[email protected]\"},{\"email\":\"[email protected]\"}]}],\"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("[email protected]"),
new EmailAddress("[email protected]"),
new EmailAddress("[email protected]")
};
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("[email protected]", "Example User"),
emails,
subjects,
plainTextContent,
htmlContent,
substitutions
);
Assert.Equal("{\"from\":{\"name\":\"Example User\",\"email\":\"[email protected]\"},\"personalizations\":[{\"to\":[{\"email\":\"[email protected]\"}],\"subject\":\"Test Subject1\",\"substitutions\":{\"-name-\":\"Name1\"}},{\"to\":[{\"email\":\"[email protected]\"}],\"subject\":\"Test Subject2\",\"substitutions\":{\"-name-\":\"Name1\"}},{\"to\":[{\"email\":\"[email protected]\"}],\"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("[email protected]", "Example User"),
emails,
subjects,
plainTextContent,
htmlContent,
substitutions
);
Assert.Equal("{\"from\":{\"name\":\"Example User\",\"email\":\"[email protected]\"},\"personalizations\":[{\"to\":[{\"email\":\"[email protected]\"}],\"subject\":\"Test Subject1\",\"substitutions\":{\"-name-\":\"Name1\"}},{\"to\":[{\"email\":\"[email protected]\"}],\"subject\":\"Test Subject2\",\"substitutions\":{\"-name-\":\"Name1\"}},{\"to\":[{\"email\":\"[email protected]\"}],\"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("[email protected]", "Example User"),
emails,
subjects,
plainTextContent,
htmlContent,
substitutions
);
Assert.Equal("{\"from\":{\"name\":\"Example User\",\"email\":\"[email protected]\"},\"personalizations\":[{\"to\":[{\"email\":\"[email protected]\"}],\"subject\":\"Test Subject1\",\"substitutions\":{\"-name-\":\"Name1\"}},{\"to\":[{\"email\":\"[email protected]\"}],\"subject\":\"Test Subject2\",\"substitutions\":{\"-name-\":\"Name1\"}},{\"to\":[{\"email\":\"[email protected]\"}],\"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("[email protected]", "Example User"),
emails,
subjects,
plainTextContent,
htmlContent,
substitutions
);
Assert.Equal("{\"from\":{\"name\":\"Example User\",\"email\":\"[email protected]\"},\"personalizations\":[{\"to\":[{\"email\":\"[email protected]\"}],\"subject\":\"Test Subject1\",\"substitutions\":{\"-name-\":\"Name1\"}},{\"to\":[{\"email\":\"[email protected]\"}],\"subject\":\"Test Subject2\",\"substitutions\":{\"-name-\":\"Name1\"}},{\"to\":[{\"email\":\"[email protected]\"}],\"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("[email protected]", "Example User"),
emails,
subjects,
plainTextContent,
htmlContent,
substitutions
);
Assert.Equal("{\"from\":{\"name\":\"Example User\",\"email\":\"[email protected]\"},\"personalizations\":[{\"to\":[{\"email\":\"[email protected]\"}],\"subject\":\"Test Subject1\",\"substitutions\":{\"-name-\":\"Name1\"}},{\"to\":[{\"email\":\"[email protected]\"}],\"subject\":\"Test Subject2\",\"substitutions\":{\"-name-\":\"Name1\"}},{\"to\":[{\"email\":\"[email protected]\"}],\"subject\":\"Test Subject3\",\"substitutions\":{\"-name-\":\"Name1\"}}],\"content\":[{\"type\":\"text/plain\",\"value\":\"Hello -name-\"}]}", msg5.Serialize());
}
[Fact]
public void TestAddTo()
{
// Personalization not passed in, Personalization does not exist
var msg = new SendGridMessage();
msg.AddTo(new EmailAddress("[email protected]", "Example User"));
Assert.Equal("{\"personalizations\":[{\"to\":[{\"name\":\"Example User\",\"email\":\"[email protected]\"}]}]}", msg.Serialize());
// Personalization passed in, no Personalizations
msg = new SendGridMessage();
var email = new EmailAddress("[email protected]", "Example User");
var personalization = new Personalization()
{
Tos = new List<EmailAddress>()
{
email
}
};
msg.AddTo(new EmailAddress("[email protected]", "Example User"), 0, personalization);
Assert.Equal("{\"personalizations\":[{\"to\":[{\"name\":\"Example User\",\"email\":\"[email protected]\"},{\"name\":\"Example User\",\"email\":\"[email protected]\"}]}]}", msg.Serialize());
// Personalization passed in, Personalization exists
msg = new SendGridMessage();
email = new EmailAddress("[email protected]", "Example User");
msg.Personalizations = new List<Personalization>() {
new Personalization() {
Tos = new List<EmailAddress>()
{
email
}
}
};
email = new EmailAddress("[email protected]", "Example User");
personalization = new Personalization()
{
Tos = new List<EmailAddress>()
{
email
}
};
msg.AddTo(new EmailAddress("[email protected]", "Example User"), 1, personalization);
Assert.Equal("{\"personalizations\":[{\"to\":[{\"name\":\"Example User\",\"email\":\"[email protected]\"}]},{\"to\":[{\"name\":\"Example User\",\"email\":\"[email protected]\"},{\"name\":\"Example User\",\"email\":\"[email protected]\"}]}]}", msg.Serialize());
// Personalization not passed in Personalization exists
msg = new SendGridMessage();
email = new EmailAddress("[email protected]", "Example User");
msg.Personalizations = new List<Personalization>() {
new Personalization() {
Tos = new List<EmailAddress>()
{
email
}
}
};
msg.AddTo(new EmailAddress("[email protected]", "Example User"));
Assert.Equal("{\"personalizations\":[{\"to\":[{\"name\":\"Example User\",\"email\":\"[email protected]\"},{\"name\":\"Example User\",\"email\":\"[email protected]\"}]}]}", msg.Serialize());
// Personalization not passed in Personalizations exists
msg = new SendGridMessage();
email = new EmailAddress("[email protected]", "Example User");
msg.Personalizations = new List<Personalization>() {
new Personalization() {
Tos = new List<EmailAddress>()
{
email
}
}
};
email = new EmailAddress("[email protected]", "Example User");
personalization = new Personalization()
{
Tos = new List<EmailAddress>()
{
email
}
};
msg.Personalizations.Add(personalization);
msg.AddTo(new EmailAddress("[email protected]", "Example User"));
Assert.Equal("{\"personalizations\":[{\"to\":[{\"name\":\"Example User\",\"email\":\"[email protected]\"},{\"name\":\"Example User\",\"email\":\"[email protected]\"}]},{\"to\":[{\"name\":\"Example User\",\"email\":\"[email protected]\"}]}]}", msg.Serialize());
}
[Fact]
public void TestAddToWithOutEmailAddressObject()
{
var msg = new SendGridMessage();
msg.AddTo("[email protected]", "Example User");
Assert.Equal("{\"personalizations\":[{\"to\":[{\"name\":\"Example User\",\"email\":\"[email protected]\"}]}]}", msg.Serialize());
msg = new SendGridMessage();
msg.AddTo("[email protected]");
Assert.Equal("{\"personalizations\":[{\"to\":[{\"email\":\"[email protected]\"}]}]}", 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("[email protected]", "Example User"),
new EmailAddress("[email protected]", "Example User")
};
msg.AddTos(emails);
Assert.Equal("{\"personalizations\":[{\"to\":[{\"name\":\"Example User\",\"email\":\"[email protected]\"},{\"name\":\"Example User\",\"email\":\"[email protected]\"}]}]}", msg.Serialize());
// Personalization passed in, no Personalizations
msg = new SendGridMessage();
emails = new List<EmailAddress>
{
new EmailAddress("[email protected]", "Example User"),
new EmailAddress("[email protected]", "Example User")
};
var personalization = new Personalization()
{
Tos = emails
};
emails = new List<EmailAddress>
{
new EmailAddress("[email protected]", "Example User"),
new EmailAddress("[email protected]", "Example User")
};
msg.AddTos(emails, 0, personalization);
Assert.Equal("{\"personalizations\":[{\"to\":[{\"name\":\"Example User\",\"email\":\"[email protected]\"},{\"name\":\"Example User\",\"email\":\"[email protected]\"},{\"name\":\"Example User\",\"email\":\"[email protected]\"},{\"name\":\"Example User\",\"email\":\"[email protected]\"}]}]}", msg.Serialize());
// Personalization passed in, Personalization exists
msg = new SendGridMessage();
emails = new List<EmailAddress>
{
new EmailAddress("[email protected]", "Example User"),
new EmailAddress("[email protected]", "Example User")
};
msg.Personalizations = new List<Personalization>() {
new Personalization() {
Tos = emails
}
};
emails = new List<EmailAddress>
{
new EmailAddress("[email protected]", "Example User"),
new EmailAddress("[email protected]", "Example User")
};
personalization = new Personalization()
{
Tos = emails
};
emails = new List<EmailAddress>
{
new EmailAddress("[email protected]", "Example User"),
new EmailAddress("[email protected]", "Example User")
};
msg.AddTos(emails, 1, personalization);
Assert.Equal("{\"personalizations\":[{\"to\":[{\"name\":\"Example User\",\"email\":\"[email protected]\"},{\"name\":\"Example User\",\"email\":\"[email protected]\"}]},{\"to\":[{\"name\":\"Example User\",\"email\":\"[email protected]\"},{\"name\":\"Example User\",\"email\":\"[email protected]\"},{\"name\":\"Example User\",\"email\":\"[email protected]\"},{\"name\":\"Example User\",\"email\":\"[email protected]\"}]}]}", msg.Serialize());
// Personalization not passed in Personalization exists
msg = new SendGridMessage();
emails = new List<EmailAddress>
{
new EmailAddress("[email protected]", "Example User"),
new EmailAddress("[email protected]", "Example User")
};
msg.Personalizations = new List<Personalization>() {
new Personalization() {
Tos = emails
}
};
emails = new List<EmailAddress>
{
new EmailAddress("[email protected]", "Example User"),
new EmailAddress("[email protected]", "Example User")
};
msg.AddTos(emails);
Assert.Equal("{\"personalizations\":[{\"to\":[{\"name\":\"Example User\",\"email\":\"[email protected]\"},{\"name\":\"Example User\",\"email\":\"[email protected]\"},{\"name\":\"Example User\",\"email\":\"[email protected]\"},{\"name\":\"Example User\",\"email\":\"[email protected]\"}]}]}", msg.Serialize());
// Personalization not passed in Personalizations exists
msg = new SendGridMessage();
emails = new List<EmailAddress>
{
new EmailAddress("[email protected]", "Example User"),
new EmailAddress("[email protected]", "Example User")
};
msg.Personalizations = new List<Personalization>() {
new Personalization() {
Tos = emails
}
};
emails = new List<EmailAddress>
{
new EmailAddress("[email protected]", "Example User"),
new EmailAddress("[email protected]", "Example User")
};
personalization = new Personalization()
{
Tos = emails
};
msg.Personalizations.Add(personalization);
emails = new List<EmailAddress>
{
new EmailAddress("[email protected]", "Example User"),
new EmailAddress("[email protected]", "Example User")
};
msg.AddTos(emails);
Assert.Equal("{\"personalizations\":[{\"to\":[{\"name\":\"Example User\",\"email\":\"[email protected]\"},{\"name\":\"Example User\",\"email\":\"[email protected]\"},{\"name\":\"Example User\",\"email\":\"[email protected]\"},{\"name\":\"Example User\",\"email\":\"[email protected]\"}]},{\"to\":[{\"name\":\"Example User\",\"email\":\"[email protected]\"},{\"name\":\"Example User\",\"email\":\"[email protected]\"}]}]}", msg.Serialize());
}
[Fact]
public void TestAddCcWithoutEmailAddressObject()
{
var msg = new SendGridMessage();
msg.AddCc("[email protected]", "Example User");
Assert.Equal("{\"personalizations\":[{\"cc\":[{\"name\":\"Example User\",\"email\":\"[email protected]\"}]}]}", msg.Serialize());
msg = new SendGridMessage();
msg.AddCc("[email protected]");
Assert.Equal("{\"personalizations\":[{\"cc\":[{\"email\":\"[email protected]\"}]}]}", 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("[email protected]", "Example User"));
Assert.Equal("{\"personalizations\":[{\"cc\":[{\"name\":\"Example User\",\"email\":\"[email protected]\"}]}]}", msg.Serialize());
// Personalization passed in, no Personalizations
msg = new SendGridMessage();
var email = new EmailAddress("[email protected]", "Example User");
var personalization = new Personalization()
{
Ccs = new List<EmailAddress>()
{
email
}
};
msg.AddCc(new EmailAddress("[email protected]", "Example User"), 0, personalization);
Assert.Equal("{\"personalizations\":[{\"cc\":[{\"name\":\"Example User\",\"email\":\"[email protected]\"},{\"name\":\"Example User\",\"email\":\"[email protected]\"}]}]}", msg.Serialize());
// Personalization passed in, Personalization exists
msg = new SendGridMessage();
email = new EmailAddress("[email protected]", "Example User");
msg.Personalizations = new List<Personalization>() {
new Personalization() {
Ccs = new List<EmailAddress>()
{
email
}
}
};
email = new EmailAddress("[email protected]", "Example User");
personalization = new Personalization()
{
Ccs = new List<EmailAddress>()
{
email
}
};
msg.AddCc(new EmailAddress("[email protected]", "Example User"), 1, personalization);
Assert.Equal("{\"personalizations\":[{\"cc\":[{\"name\":\"Example User\",\"email\":\"[email protected]\"}]},{\"cc\":[{\"name\":\"Example User\",\"email\":\"[email protected]\"},{\"name\":\"Example User\",\"email\":\"[email protected]\"}]}]}", msg.Serialize());
// Personalization not passed in Personalization exists
msg = new SendGridMessage();
email = new EmailAddress("[email protected]", "Example User");
msg.Personalizations = new List<Personalization>() {
new Personalization() {
Ccs = new List<EmailAddress>()
{
email
}
}
};
msg.AddCc(new EmailAddress("[email protected]", "Example User"));
Assert.Equal("{\"personalizations\":[{\"cc\":[{\"name\":\"Example User\",\"email\":\"[email protected]\"},{\"name\":\"Example User\",\"email\":\"[email protected]\"}]}]}", msg.Serialize());
// Personalization not passed in Personalizations exists
msg = new SendGridMessage();
email = new EmailAddress("[email protected]", "Example User");
msg.Personalizations = new List<Personalization>() {
new Personalization() {
Ccs = new List<EmailAddress>()
{
email
}
}
};
email = new EmailAddress("[email protected]", "Example User");
personalization = new Personalization()
{
Ccs = new List<EmailAddress>()
{
email
}
};
msg.Personalizations.Add(personalization);
msg.AddCc(new EmailAddress("[email protected]", "Example User"));
Assert.Equal("{\"personalizations\":[{\"cc\":[{\"name\":\"Example User\",\"email\":\"[email protected]\"},{\"name\":\"Example User\",\"email\":\"[email protected]\"}]},{\"cc\":[{\"name\":\"Example User\",\"email\":\"[email protected]\"}]}]}", msg.Serialize());
}
[Fact]
public void TestAddCcs()
{
// Personalization not passed in, Personalization does not exist
var msg = new SendGridMessage();
var emails = new List<EmailAddress>
{
new EmailAddress("[email protected]", "Example User"),
new EmailAddress("[email protected]", "Example User")
};
msg.AddCcs(emails);
Assert.Equal("{\"personalizations\":[{\"cc\":[{\"name\":\"Example User\",\"email\":\"[email protected]\"},{\"name\":\"Example User\",\"email\":\"[email protected]\"}]}]}", msg.Serialize());
// Personalization passed in, no Personalizations
msg = new SendGridMessage();
emails = new List<EmailAddress>
{
new EmailAddress("[email protected]", "Example User"),
new EmailAddress("[email protected]", "Example User")
};
var personalization = new Personalization()
{
Ccs = emails
};
emails = new List<EmailAddress>
{
new EmailAddress("[email protected]", "Example User"),
new EmailAddress("[email protected]", "Example User")
};
msg.AddCcs(emails, 0, personalization);
Assert.Equal("{\"personalizations\":[{\"cc\":[{\"name\":\"Example User\",\"email\":\"[email protected]\"},{\"name\":\"Example User\",\"email\":\"[email protected]\"},{\"name\":\"Example User\",\"email\":\"[email protected]\"},{\"name\":\"Example User\",\"email\":\"[email protected]\"}]}]}", msg.Serialize());
// Personalization passed in, Personalization exists
msg = new SendGridMessage();
emails = new List<EmailAddress>
{
new EmailAddress("[email protected]", "Example User"),
new EmailAddress("[email protected]", "Example User")
};
msg.Personalizations = new List<Personalization>() {
new Personalization() {
Ccs = emails
}
};
emails = new List<EmailAddress>
{
new EmailAddress("[email protected]", "Example User"),
new EmailAddress("[email protected]", "Example User")
};
personalization = new Personalization()
{
Ccs = emails
};
emails = new List<EmailAddress>
{
new EmailAddress("[email protected]", "Example User"),
new EmailAddress("[email protected]", "Example User")
};
msg.AddCcs(emails, 1, personalization);
Assert.Equal("{\"personalizations\":[{\"cc\":[{\"name\":\"Example User\",\"email\":\"[email protected]\"},{\"name\":\"Example User\",\"email\":\"[email protected]\"}]},{\"cc\":[{\"name\":\"Example User\",\"email\":\"[email protected]\"},{\"name\":\"Example User\",\"email\":\"[email protected]\"},{\"name\":\"Example User\",\"email\":\"[email protected]\"},{\"name\":\"Example User\",\"email\":\"[email protected]\"}]}]}", msg.Serialize());
// Personalization not passed in Personalization exists
msg = new SendGridMessage();
emails = new List<EmailAddress>
{
new EmailAddress("[email protected]", "Example User"),
new EmailAddress("[email protected]", "Example User")
};
msg.Personalizations = new List<Personalization>() {
new Personalization() {
Ccs = emails
}
};
emails = new List<EmailAddress>
{
new EmailAddress("[email protected]", "Example User"),
new EmailAddress("[email protected]", "Example User")
};
msg.AddCcs(emails);
Assert.Equal("{\"personalizations\":[{\"cc\":[{\"name\":\"Example User\",\"email\":\"[email protected]\"},{\"name\":\"Example User\",\"email\":\"[email protected]\"},{\"name\":\"Example User\",\"email\":\"[email protected]\"},{\"name\":\"Example User\",\"email\":\"[email protected]\"}]}]}", msg.Serialize());
// Personalization not passed in Personalizations exists