diff options
author | Elmer Thomas <elmer@thinkingserious.com> | 2016-08-24 10:44:59 -0700 |
---|---|---|
committer | Elmer Thomas <elmer@thinkingserious.com> | 2016-08-24 10:44:59 -0700 |
commit | 674079432bbb6c0b1b97e2303ded9d1ac1d20c4e (patch) | |
tree | fc3756b8d1e3d2d091afca7b7206c79492a77429 /SendGrid/Example/Example.cs | |
parent | 8f7b752c1764d66da39559ff99479e9937143b43 (diff) | |
download | sendgrid-csharp-674079432bbb6c0b1b97e2303ded9d1ac1d20c4e.zip sendgrid-csharp-674079432bbb6c0b1b97e2303ded9d1ac1d20c4e.tar.gz sendgrid-csharp-674079432bbb6c0b1b97e2303ded9d1ac1d20c4e.tar.bz2 |
Add TOC for README, Add USE_CASES.mdorigin/toc-template
Diffstat (limited to 'SendGrid/Example/Example.cs')
-rw-r--r-- | SendGrid/Example/Example.cs | 80 |
1 files changed, 73 insertions, 7 deletions
diff --git a/SendGrid/Example/Example.cs b/SendGrid/Example/Example.cs index 75c3b47..72bf4ef 100644 --- a/SendGrid/Example/Example.cs +++ b/SendGrid/Example/Example.cs @@ -15,11 +15,84 @@ namespace Example HelloEmail().Wait(); // this will actually send an email KitchenSink().Wait(); // this will only send an email if you set SandBox Mode to false + // v3 Template Example with Mail Helper + TemplateWithHelper().Wait(); + + // v3 Template Example without Mail Helper + TemplateWithoutHelper().Wait(); + // v3 Web API ApiKeys().Wait(); } + private static async Task TemplateWithHelper() + { + String apiKey = Environment.GetEnvironmentVariable("SENDGRID_APIKEY", EnvironmentVariableTarget.User); + dynamic sg = new SendGrid.SendGridAPIClient(apiKey, "https://api.sendgrid.com"); + + Email from = new Email("dx@sendgrid.com"); + String subject = "I'm replacing the subject tag"; + Email to = new Email("elmer@sendgrid.com"); + Content content = new Content("text/html", "I'm replacing the <strong>body tag</strong>"); + Mail mail = new Mail(from, subject, to, content); + + mail.TemplateId = "13b8f94f-bcae-4ec6-b752-70d6cb59f932"; + mail.Personalization[0].AddSubstitution("-name-", "Example User"); + mail.Personalization[0].AddSubstitution("-city-", "Denver"); + + dynamic response = await sg.client.mail.send.post(requestBody: mail.Get()); + Console.WriteLine(response.StatusCode); + Console.WriteLine(response.Body.ReadAsStringAsync().Result); + Console.WriteLine(response.Headers.ToString()); + + Console.ReadLine(); + + } + + private static async Task TemplateWithoutHelper() + { + String apiKey = Environment.GetEnvironmentVariable("SENDGRID_APIKEY", EnvironmentVariableTarget.User); + dynamic sg = new SendGrid.SendGridAPIClient(apiKey, "https://api.sendgrid.com"); + + string data = @"{ + 'personalizations': [ + { + 'to': [ + { + 'email': 'elmer@sendgrid.com' + } + ], + 'substitutions': { + '-name-': 'Example User', + '-city-': 'Denver' + }, + 'subject': 'I\'m replacing the subject tag' + } + ], + 'from': { + 'email': 'dx@sendgrid.com' + }, + 'content': [ + { + 'type': 'text/html', + 'value': 'I\'m replacing the <strong>body tag</strong>' + } + ], + 'template_id': '13b8f94f-bcae-4ec6-b752-70d6cb59f932' + }"; + //test @example.com + Object json = JsonConvert.DeserializeObject<Object>(data); + dynamic response = await sg.client.mail.send.post(requestBody: json.ToString()); + + Console.WriteLine(response.StatusCode); + Console.WriteLine(response.Body.ReadAsStringAsync().Result); + Console.WriteLine(response.Headers.ToString()); + + Console.ReadLine(); + + } + private static async Task HelloEmail() { String apiKey = Environment.GetEnvironmentVariable("SENDGRID_APIKEY", EnvironmentVariableTarget.User); @@ -33,13 +106,6 @@ namespace Example Email email = new Email("test2@example.com"); mail.Personalization[0].AddTo(email); - // If you want to use a transactional [template](https://sendgrid.com/docs/User_Guide/Transactional_Templates/index.html), - // the following code will replace the above subject and content. The sample code assumes you have defined - // substitution variables [KEY_1] and [KEY_2], to be replaced by VALUE_1 and VALUE_2 respectively, in your template. - //mail.TemplateId = "TEMPLATE_ID"; - //mail.Personalization[0].AddSubstitution("[KEY_1]", "VALUE_1"); - //mail.Personalization[0].AddSubstitution("[KEY_2]", "VALUE_2"); - dynamic response = await sg.client.mail.send.post(requestBody: mail.Get()); Console.WriteLine(response.StatusCode); Console.WriteLine(response.Body.ReadAsStringAsync().Result); |