@@ -3,8 +3,7 @@ This documentation provides examples for specific use cases. Please [open an iss
33# Table of Contents
44
55* [ Attachments] ( #attachments )
6- * [ Custom Personalization Usage]
7- * [ Multiple tos, ccs and bccs] ( #multiple_tos_ccs_bccs )
6+ * [ Kitchen Sink - an example with all settings used] ( #kitchensink )
87* [ Transactional Templates] ( #transactional_templates )
98
109<a name =" attachments " ></a >
@@ -19,28 +18,291 @@ using System.IO;
1918
2019namespace Example
2120{
22- internal class Example
23- {
24- private static void Main ()
25- {
26- Execute ().Wait ();
27- }
28-
29- static async Task Execute ()
30- {
31- var apiKey = Environment .GetEnvironmentVariable (" SENDGRID_API_KEY" );
32- var client = new SendGridClient (apiKey );
33- var from = new EmailAddress (
" [email protected] " );
34- var subject = " Subject" ;
35- var to = new EmailAddress (
" [email protected] " );
36- var body = " Email Body" ;
37- var msg = MailHelper .CreateSingleEmail (from , to , subject , body , " " );
38- var bytes = File .ReadAllBytes (" /Users/username/file.txt" );
39- var file = Convert .ToBase64String (bytes );
40- msg .AddAttachment (" file.txt" , file );
41- var response = await client .SendEmailAsync (msg );
42- }
43- }
21+ internal class Example
22+ {
23+ private static void Main ()
24+ {
25+ Execute ().Wait ();
26+ }
27+
28+ static async Task Execute ()
29+ {
30+ var apiKey = Environment .GetEnvironmentVariable (" SENDGRID_API_KEY" );
31+ var client = new SendGridClient (apiKey );
32+ var from = new EmailAddress (
" [email protected] " );
33+ var subject = " Subject" ;
34+ var to = new EmailAddress (
" [email protected] " );
35+ var body = " Email Body" ;
36+ var msg = MailHelper .CreateSingleEmail (from , to , subject , body , " " );
37+ var bytes = File .ReadAllBytes (" /Users/username/file.txt" );
38+ var file = Convert .ToBase64String (bytes );
39+ msg .AddAttachment (" file.txt" , file );
40+ var response = await client .SendEmailAsync (msg );
41+ }
42+ }
43+ }
44+ ```
45+
46+ <a name =" kitchensink " ></a >
47+ # Kitchen Sink - an example with all settings used
48+
49+ ``` csharp
50+ using SendGrid ;
51+ using SendGrid .Helpers .Mail ;
52+ using System ;
53+ using System .Threading .Tasks ;
54+ using System .Collections .Generic ;
55+
56+ namespace KitchenSink
57+ {
58+ internal class KitchenSink
59+ {
60+ private static void Main ()
61+ {
62+ Execute ().Wait ();
63+ }
64+
65+ static async Task Execute ()
66+ {
67+ var apiKey = Environment .GetEnvironmentVariable (" SENDGRID_API_KEY" );
68+ var client = new SendGridClient (apiKey );
69+ var msg = new SendGridMessage ();
70+
71+ // For a detailed description of each of these settings, please see the [documentation](https://sendgrid.com/docs/API_Reference/api_v3.html).
72+
73+ msg .
AddTo (
new EmailAddress (
" [email protected] " ,
" Example User 1" ));
74+ var to_emails = new List <EmailAddress >
75+ {
76+ new EmailAddress (
" [email protected] " ,
" Example User2" ),
77+ new EmailAddress (
" [email protected] " ,
" Example User3" )
78+ };
79+ msg .AddTos (to_emails );
80+
81+ msg .
AddCc (
new EmailAddress (
" [email protected] " ,
" Example User 4" ));
82+ var cc_emails = new List <EmailAddress >
83+ {
84+ new EmailAddress (
" [email protected] " ,
" Example User5" ),
85+ new EmailAddress (
" [email protected] " ,
" Example User6" )
86+ };
87+ msg .AddCcs (cc_emails );
88+
89+ msg .
AddBcc (
new EmailAddress (
" [email protected] " ,
" Example User 7" ));
90+ var bcc_emails = new List <EmailAddress >
91+ {
92+ new EmailAddress (
" [email protected] " ,
" Example User8" ),
93+ new EmailAddress (
" [email protected] " ,
" Example User9" )
94+ };
95+ msg .AddBccs (bcc_emails );
96+
97+ msg .AddHeader (" X-Test1" , " Test1" );
98+ msg .AddHeader (" X-Test2" , " Test2" );
99+ var headers = new Dictionary <string , string >()
100+ {
101+ { " X-Test3" , " Test3" },
102+ { " X-Test4" , " Test4" }
103+ };
104+ msg .AddHeaders (headers );
105+
106+ msg .AddSubstitution (" %name1%" , " Example Name 1" );
107+ msg .AddSubstitution (" %city1%" , " Denver" );
108+ var substitutions = new Dictionary <string , string >()
109+ {
110+ { " %name2%" , " Example Name 2" },
111+ { " %city2%" , " Orange" }
112+ };
113+ msg .AddSubstitutions (substitutions );
114+
115+ msg .AddCustomArg (" marketing1" , " false" );
116+ msg .AddCustomArg (" transactional1" , " true" );
117+ var customArgs = new Dictionary <string , string >()
118+ {
119+ { " marketing2" , " true" },
120+ { " transactional2" , " false" }
121+ };
122+ msg .AddCustomArgs (customArgs );
123+
124+ msg .SetSendAt (1461775051 );
125+
126+ // If you need to add more [Personalizations](https://sendgrid.com/docs/Classroom/Send/v3_Mail_Send/personalizations.html), here is an example of adding another Personalization by passing in a personalization index
127+
128+ msg .
AddTo (
new EmailAddress (
" [email protected] " ,
" Example User 10" ),
1 );
129+ var to_emails1 = new List <EmailAddress >
130+ {
131+ new EmailAddress (
" [email protected] " ,
" Example User11" ),
132+ new EmailAddress (
" [email protected] " ,
" Example User12" )
133+ };
134+ msg .AddTos (to_emails1 , 1 );
135+
136+ msg .
AddCc (
new EmailAddress (
" [email protected] " ,
" Example User 13" ),
1 );
137+ var cc_emails1 = new List <EmailAddress >
138+ {
139+ new EmailAddress (
" [email protected] " ,
" Example User14" ),
140+ new EmailAddress (
" [email protected] " ,
" Example User15" )
141+ };
142+ msg .AddCcs (cc_emails1 , 1 );
143+
144+ msg .
AddBcc (
new EmailAddress (
" [email protected] " ,
" Example User 16" ),
1 );
145+ var bcc_emails1 = new List <EmailAddress >
146+ {
147+ new EmailAddress (
" [email protected] " ,
" Example User17" ),
148+ new EmailAddress (
" [email protected] " ,
" Example User18" )
149+ };
150+ msg .AddBccs (bcc_emails1 , 1 );
151+
152+ msg .AddHeader (" X-Test5" , " Test5" , 1 );
153+ msg .AddHeader (" X-Test6" , " Test6" , 1 );
154+ var headers1 = new Dictionary <string , string >()
155+ {
156+ { " X-Test7" , " Test7" },
157+ { " X-Test8" , " Test8" }
158+ };
159+ msg .AddHeaders (headers1 , 1 );
160+
161+ msg .AddSubstitution (" %name3%" , " Example Name 3" , 1 );
162+ msg .AddSubstitution (" %city3%" , " Redwood City" , 1 );
163+ var substitutions1 = new Dictionary <string , string >()
164+ {
165+ { " %name4%" , " Example Name 4" },
166+ { " %city4%" , " London" }
167+ };
168+ msg .AddSubstitutions (substitutions1 , 1 );
169+
170+ msg .AddCustomArg (" marketing3" , " true" , 1 );
171+ msg .AddCustomArg (" transactional3" , " false" , 1 );
172+ var customArgs1 = new Dictionary <string , string >()
173+ {
174+ { " marketing4" , " false" },
175+ { " transactional4" , " true" }
176+ };
177+ msg .AddCustomArgs (customArgs1 , 1 );
178+
179+ msg .SetSendAt (1461775052 , 1 );
180+
181+ // The values below this comment are global to entire message
182+
183+ msg .
SetFrom (
" [email protected] " ,
" Example User 0" );
184+
185+ msg .SetSubject (" this subject overrides the Global Subject" );
186+
187+ msg .SetGlobalSubject (" Sending with SendGrid is Fun" );
188+
189+ msg .AddContent (MimeType .Text , " and easy to do anywhere, even with C#" );
190+ msg .AddContent (MimeType .Html , " <strong>and easy to do anywhere, even with C#</strong>" );
191+ var contents = new List <Content >()
192+ {
193+ new Content (" text/calendar" , " Party Time!!" ),
194+ new Content (" text/calendar2" , " Party Time2!!" )
195+ };
196+ msg .AddContents (contents );
197+
198+ // For base64 encoding, see [`Convert.ToBase64String`](https://msdn.microsoft.com/en-us/library/system.convert.tobase64string(v=vs.110).aspx)
199+ // For an example using an attachment, please see this [use case](https://github.com/sendgrid/sendgrid-csharp/blob/master/USE_CASES.md#attachments).
200+ msg .AddAttachment (" balance_001.pdf" ,
201+ " base64 encoded string" ,
202+ " application/pdf" ,
203+ " attachment" ,
204+ " Balance Sheet" );
205+ var attachments = new List <Attachment >()
206+ {
207+ new Attachment ()
208+ {
209+ Content = " base64 encoded string" ,
210+ Type = " image/png" ,
211+ Filename = " banner.png" ,
212+ Disposition = " inline" ,
213+ ContentId = " Banner"
214+ },
215+ new Attachment ()
216+ {
217+ Content = " base64 encoded string" ,
218+ Type = " image/png" ,
219+ Filename = " banner2.png" ,
220+ Disposition = " inline" ,
221+ ContentId = " Banner 2"
222+ }
223+ };
224+ msg .AddAttachments (attachments );
225+
226+ // For a full transactional template example, please see this [use case](https://github.com/sendgrid/sendgrid-csharp/blob/master/USE_CASES.md#transactional-templates).
227+ msg .SetTemplateId (" 13b8f94f-bcae-4ec6-b752-70d6cb59f932" );
228+
229+ msg .AddGlobalHeader (" X-Day" , " Monday" );
230+ var globalHeaders = new Dictionary <string , string >
231+ {
232+ { " X-Month" , " January" },
233+ { " X-Year" , " 2017" }
234+ };
235+ msg .AddGlobalHeaders (globalHeaders );
236+
237+ msg .AddSection (" %section1" , " Substitution for Section 1 Tag" );
238+ var sections = new Dictionary <string , string >
239+ {
240+ {" %section2%" , " Substitution for Section 2 Tag" },
241+ {" %section3%" , " Substitution for Section 3 Tag" }
242+ };
243+ msg .AddSections (sections );
244+
245+ msg .AddCategory (" customer" );
246+ var categories = new List <string >
247+ {
248+ " vip" ,
249+ " new_account"
250+ };
251+ msg .AddCategories (categories );
252+
253+ msg .AddGlobalCustomArg (" campaign" , " welcome" );
254+ var globalCustomArgs = new Dictionary <string , string >
255+ {
256+ { " sequence2" , " 2" },
257+ { " sequence3" , " 3" }
258+ };
259+ msg .AddGlobalCustomArgs (globalCustomArgs );
260+
261+ msg .SetAsm (3 , new List <int >() { 1 , 4 , 5 });
262+
263+ msg .SetGlobalSendAt (1461775051 );
264+
265+ msg .SetIpPoolName (" 23" );
266+
267+ // This must be a valid [batch ID](https://sendgrid.com/docs/API_Reference/SMTP_API/scheduling_parameters.html)
268+ // msg.SetBatchId("some_batch_id");
269+
270+ msg .
SetBccSetting (
true ,
" [email protected] " );
271+
272+ msg .SetBypassListManagement (true );
273+
274+ msg .SetFooterSetting (true , " Some Footer HTML" , " Some Footer Text" );
275+
276+ msg .SetSandBoxMode (true );
277+
278+ msg .SetSpamCheck (true , 1 , " https://gotchya.example.com" );
279+
280+ msg .SetClickTracking (true , false );
281+
282+ msg .SetOpenTracking (true , " Optional tag to replace with the open image in the body of the message" );
283+
284+ msg .SetSubscriptionTracking (true ,
285+ " HTML to insert into the text / html portion of the message" ,
286+ " text to insert into the text/plain portion of the message" ,
287+ " substitution tag" );
288+
289+ msg .SetGoogleAnalytics (true ,
290+ " some campaign" ,
291+ " some content" ,
292+ " some medium" ,
293+ " some source" ,
294+ " some term" );
295+
296+ msg .
SetReplyTo (
new EmailAddress (
" [email protected] " ,
" Reply To Me" ));
297+
298+ var response = await client .SendEmailAsync (msg );
299+ Console .WriteLine (msg .Serialize ());
300+ Console .WriteLine (response .StatusCode );
301+ Console .WriteLine (response .Body .ReadAsStringAsync ().Result );
302+ Console .WriteLine (response .Headers );
303+ Console .ReadLine ();
304+ }
305+ }
44306}
45307```
46308
@@ -66,7 +328,7 @@ Template Body:
66328``` html
67329<html >
68330<head >
69- <title ></title >
331+ <title ></title >
70332</head >
71333<body >
72334Hello -name-,
@@ -180,66 +442,3 @@ namespace Example
180442 }
181443}
182444```
183-
184- <a name =" multiple_tos_ccs_bccs " ></a >
185- # Custom Personalization Usage - Multiple tos, ccs and bccs
186-
187- In this example, each email recipient receives the same email and they can see each others email address, except for the bccs.
188-
189- For more on [ Personalizations] ( https://sendgrid.com/docs/Classroom/Send/v3_Mail_Send/personalizations.html ) , please see the documentation.
190-
191- ``` csharp
192- using System ;
193- using System .Collections .Generic ;
194- using System .Threading .Tasks ;
195- using SendGrid ;
196- using SendGrid .Helpers .Mail ;
197-
198- namespace Example
199- {
200- class Program
201- {
202- static void Main (string [] args )
203- {
204- Execute ().Wait ();
205- }
206-
207- static async Task Execute ()
208- {
209- var apiKey = Environment .GetEnvironmentVariable (" NAME_OF_THE_ENVIRONMENT_VARIABLE_FOR_YOUR_SENDGRID_KEY" );
210- var client = new SendGridClient (apiKey );
211- var msg = new SendGridMessage ();
212- msg .
SetFrom (
new EmailAddress (
" [email protected] " ,
" Test1" ));
213- var personalization = new Personalization ()
214- {
215- Subject = " Best Subject Line Ever" ,
216- Tos = new List <EmailAddress >()
217- {
218- new EmailAddress () {
Email = " [email protected] " ,
Name = " Test2" },
219- new EmailAddress () {
Email = " [email protected] " ,
Name = " Test3" },
220- new EmailAddress () {
Email = " [email protected] " ,
Name = " Test4" },
221- },
222- Bccs = new List <EmailAddress >()
223- {
224- new EmailAddress () {
Email = " [email protected] " ,
Name = " Test5" },
225- new EmailAddress () {
Email = " [email protected] " ,
Name = " Test6" },
226- new EmailAddress () {
Email = " [email protected] " ,
Name = " Test7" },
227- },
228- Ccs = new List <EmailAddress >()
229- {
230- new EmailAddress () {
Email = " [email protected] " ,
Name = " Test8" },
231- new EmailAddress () {
Email = " [email protected] " ,
Name = " Test9" },
232- new EmailAddress () {
Email = " [email protected] " ,
Name = " Test10" },
233- }
234- }
235- msg .
AddTo (
new EmailAddress (
" [email protected] " ),
0 ,
personalization );
236- msg .AddContent (MimeType .Text , " Hello Email from the SendGrid C# Library" );
237- var response = await client .SendEmailAsync (msg );
238- Console .WriteLine (response .StatusCode );
239- Console .WriteLine (response .Headers .ToString ());
240- Console .WriteLine (" \n\n Press any key to exit." );
241- Console .ReadLine ();
242- }
243- }
244- }
245- ```
0 commit comments