Skip to content

Commit 0b21784

Browse files
Merge pull request #310 from sendgrid/toc-template
Add TOC for README, Add USE_CASES.md
2 parents 8f7b752 + 6740794 commit 0b21784

File tree

3 files changed

+234
-7
lines changed

3 files changed

+234
-7
lines changed

README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,20 @@ Please browse the rest of this README for further detail.
1010

1111
We appreciate your continued support, thank you!
1212

13+
# Table of Contents
14+
15+
* [Installation](#installation)
16+
* [Quick Start](#quick_start)
17+
* [Usage](#usage)
18+
* [Use Cases](#use_cases)
19+
* [Announcements](#announcements)
20+
* [Roadmap](#roadmap)
21+
* [How to Contribute](#contribute)
22+
* [Troubleshooting](#troubleshooting)
23+
* [About](#about)
24+
25+
26+
<a name="installation"></a>
1327
# Installation
1428

1529
## Prerequisites
@@ -45,6 +59,7 @@ using SendGrid.Helpers.Mail; // Include if you want to use the Mail Helper
4559
- [SendGrid.CSharp.HTTP.Client](https://github.com/sendgrid/csharp-http-client)
4660
- [Newtonsoft.Json](http://www.newtonsoft.com/json)
4761

62+
<a name="quick_start"></a>
4863
# Quick Start
4964

5065
## Hello Email
@@ -191,6 +206,7 @@ namespace Example
191206
}
192207
```
193208

209+
<a name="usage"></a>
194210
# Usage
195211

196212
- [SendGrid Docs](https://sendgrid.com/docs/API_Reference/Web_API_v3/index.html)
@@ -199,14 +215,22 @@ namespace Example
199215
- [How-to: Migration from v2 to v3](https://sendgrid.com/docs/Classroom/Send/v3_Mail_Send/how_to_migrate_from_v2_to_v3_mail_send.html)
200216
- [v3 Web API Mail Send Helper](https://github.com/sendgrid/sendgrid-csharp/tree/master/SendGrid/SendGrid/Helpers/Mail)
201217

218+
<a name="use_cases">
219+
# Use Cases
220+
221+
[Examples of common API use cases](https://github.com/sendgrid/sendgrid-csharp/blob/master/USE_CASES.md), such as how to send an email with a transactional template.
222+
223+
<a name="announcements"></a>
202224
# Announcements
203225

204226
All updates to this library is documented in our [CHANGELOG](https://github.com/sendgrid/sendgrid-csharp/blob/master/CHANGELOG.md) and [releases](https://github.com/sendgrid/sendgrid-csharp/releases).
205227

228+
<a name="roadmap"></a>
206229
# Roadmap
207230

208231
If you are interested in the future direction of this project, please take a look at our open [issues](https://github.com/sendgrid/sendgrid-csharp/issues) and [pull requests](https://github.com/sendgrid/sendgrid-csharp/pulls). We would love to hear your feedback.
209232

233+
<a name="contribute"></a>
210234
# How to Contribute
211235

212236
We encourage contribution to our library (you might even score some nifty swag), please see our [CONTRIBUTING](https://github.com/sendgrid/sendgrid-csharp/tree/master/CONTRIBUTING.md) guide for details.
@@ -218,10 +242,12 @@ Quick links:
218242
- [Sign the CLA to Create a Pull Request](https://github.com/sendgrid/sendgrid-csharp/tree/master/CONTRIBUTING.md#cla)
219243
- [Improvements to the Codebase](https://github.com/sendgrid/sendgrid-csharp/tree/master/CONTRIBUTING.md#improvements_to_the_codebase)
220244

245+
<a name="troubleshooting"></a>
221246
# Troubleshooting
222247

223248
Please see our [troubleshooting guide](https://github.com/sendgrid/sendgrid-csharp/blob/master/TROUBLESHOOTING.md) for common library issues.
224249

250+
<a name="about"></a>
225251
# About
226252

227253
sendgrid-csharp is guided and supported by the SendGrid [Developer Experience Team](mailto:[email protected]).

SendGrid/Example/Example.cs

Lines changed: 73 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,84 @@ private static void Main()
1515
HelloEmail().Wait(); // this will actually send an email
1616
KitchenSink().Wait(); // this will only send an email if you set SandBox Mode to false
1717

18+
// v3 Template Example with Mail Helper
19+
TemplateWithHelper().Wait();
20+
21+
// v3 Template Example without Mail Helper
22+
TemplateWithoutHelper().Wait();
23+
1824
// v3 Web API
1925
ApiKeys().Wait();
2026

2127
}
2228

29+
private static async Task TemplateWithHelper()
30+
{
31+
String apiKey = Environment.GetEnvironmentVariable("SENDGRID_APIKEY", EnvironmentVariableTarget.User);
32+
dynamic sg = new SendGrid.SendGridAPIClient(apiKey, "https://api.sendgrid.com");
33+
34+
Email from = new Email("[email protected]");
35+
String subject = "I'm replacing the subject tag";
36+
Email to = new Email("[email protected]");
37+
Content content = new Content("text/html", "I'm replacing the <strong>body tag</strong>");
38+
Mail mail = new Mail(from, subject, to, content);
39+
40+
mail.TemplateId = "13b8f94f-bcae-4ec6-b752-70d6cb59f932";
41+
mail.Personalization[0].AddSubstitution("-name-", "Example User");
42+
mail.Personalization[0].AddSubstitution("-city-", "Denver");
43+
44+
dynamic response = await sg.client.mail.send.post(requestBody: mail.Get());
45+
Console.WriteLine(response.StatusCode);
46+
Console.WriteLine(response.Body.ReadAsStringAsync().Result);
47+
Console.WriteLine(response.Headers.ToString());
48+
49+
Console.ReadLine();
50+
51+
}
52+
53+
private static async Task TemplateWithoutHelper()
54+
{
55+
String apiKey = Environment.GetEnvironmentVariable("SENDGRID_APIKEY", EnvironmentVariableTarget.User);
56+
dynamic sg = new SendGrid.SendGridAPIClient(apiKey, "https://api.sendgrid.com");
57+
58+
string data = @"{
59+
'personalizations': [
60+
{
61+
'to': [
62+
{
63+
'email': '[email protected]'
64+
}
65+
],
66+
'substitutions': {
67+
'-name-': 'Example User',
68+
'-city-': 'Denver'
69+
},
70+
'subject': 'I\'m replacing the subject tag'
71+
}
72+
],
73+
'from': {
74+
'email': '[email protected]'
75+
},
76+
'content': [
77+
{
78+
'type': 'text/html',
79+
'value': 'I\'m replacing the <strong>body tag</strong>'
80+
}
81+
],
82+
'template_id': '13b8f94f-bcae-4ec6-b752-70d6cb59f932'
83+
}";
84+
//test @example.com
85+
Object json = JsonConvert.DeserializeObject<Object>(data);
86+
dynamic response = await sg.client.mail.send.post(requestBody: json.ToString());
87+
88+
Console.WriteLine(response.StatusCode);
89+
Console.WriteLine(response.Body.ReadAsStringAsync().Result);
90+
Console.WriteLine(response.Headers.ToString());
91+
92+
Console.ReadLine();
93+
94+
}
95+
2396
private static async Task HelloEmail()
2497
{
2598
String apiKey = Environment.GetEnvironmentVariable("SENDGRID_APIKEY", EnvironmentVariableTarget.User);
@@ -33,13 +106,6 @@ private static async Task HelloEmail()
33106
Email email = new Email("[email protected]");
34107
mail.Personalization[0].AddTo(email);
35108

36-
// If you want to use a transactional [template](https://sendgrid.com/docs/User_Guide/Transactional_Templates/index.html),
37-
// the following code will replace the above subject and content. The sample code assumes you have defined
38-
// substitution variables [KEY_1] and [KEY_2], to be replaced by VALUE_1 and VALUE_2 respectively, in your template.
39-
//mail.TemplateId = "TEMPLATE_ID";
40-
//mail.Personalization[0].AddSubstitution("[KEY_1]", "VALUE_1");
41-
//mail.Personalization[0].AddSubstitution("[KEY_2]", "VALUE_2");
42-
43109
dynamic response = await sg.client.mail.send.post(requestBody: mail.Get());
44110
Console.WriteLine(response.StatusCode);
45111
Console.WriteLine(response.Body.ReadAsStringAsync().Result);

USE_CASES.md

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
This documentation provides examples for specific use cases. Please [open an issue](https://github.com/sendgrid/sendgrid-sharp/issues) or make a pull request for any use cases you would like us to document here. Thank you!
2+
3+
# Table of Contents
4+
5+
* [Transactional Templates](#transactional_templates)
6+
7+
<a name="transactional_templates"></a>
8+
# Transactional Templates
9+
10+
For this example, we assume you have created a [transactional template](https://sendgrid.com/docs/User_Guide/Transactional_Templates/index.html). Following is the template content we used for testing.
11+
12+
Template ID (replace with your own):
13+
14+
```text
15+
13b8f94f-bcae-4ec6-b752-70d6cb59f932
16+
```
17+
18+
Email Subject:
19+
20+
```text
21+
<%subject%>
22+
```
23+
24+
Template Body:
25+
26+
```html
27+
<html>
28+
<head>
29+
<title></title>
30+
</head>
31+
<body>
32+
Hello -name-,
33+
<br /><br/>
34+
I'm glad you are trying out the template feature!
35+
<br /><br/>
36+
<%body%>
37+
<br /><br/>
38+
I hope you are having a great day in -city- :)
39+
<br /><br/>
40+
</body>
41+
</html>
42+
```
43+
44+
## With Mail Helper Class
45+
46+
```charp
47+
using System;
48+
using SendGrid;
49+
using SendGrid.Helpers.Mail;
50+
using System.Threading.Tasks;
51+
52+
namespace Example
53+
{
54+
internal class Example
55+
{
56+
private static void Main()
57+
{
58+
Execute().Wait();
59+
}
60+
61+
static async Task Execute()
62+
{
63+
string apiKey = Environment.GetEnvironmentVariable("NAME_OF_THE_ENVIRONMENT_VARIABLE_FOR_YOUR_SENDGRID_KEY", EnvironmentVariableTarget.User);
64+
dynamic sg = new SendGridAPIClient(apiKey);
65+
66+
Email from = new Email("[email protected]");
67+
String subject = "I'm replacing the subject tag";
68+
Email to = new Email("[email protected]");
69+
Content content = new Content("text/html", "I'm replacing the <strong>body tag</strong>");
70+
Mail mail = new Mail(from, subject, to, content);
71+
72+
mail.TemplateId = "13b8f94f-bcae-4ec6-b752-70d6cb59f932";
73+
mail.Personalization[0].AddSubstitution("-name-", "Example User");
74+
mail.Personalization[0].AddSubstitution("-city-", "Denver");
75+
76+
dynamic response = await sg.client.mail.send.post(requestBody: mail.Get());
77+
}
78+
}
79+
}
80+
```
81+
82+
## Without Mail Helper Class
83+
84+
```csharp
85+
using System;
86+
using SendGrid;
87+
using Newtonsoft.Json; // You can generate your JSON string yourelf or with another library if you prefer
88+
using System.Threading.Tasks;
89+
90+
namespace Example
91+
{
92+
internal class Example
93+
{
94+
private static void Main()
95+
{
96+
Execute().Wait();
97+
}
98+
99+
static async Task Execute()
100+
{
101+
String apiKey = Environment.GetEnvironmentVariable("NAME_OF_THE_ENVIRONMENT_VARIABLE_FOR_YOUR_SENDGRID_KEY", EnvironmentVariableTarget.User);
102+
dynamic sg = new SendGridAPIClient(apiKey);
103+
104+
string data = @"{
105+
'personalizations': [
106+
{
107+
'to': [
108+
{
109+
'email': '[email protected]'
110+
}
111+
],
112+
'substitutions': {
113+
'-name-': 'Example User',
114+
'-city-': 'Denver'
115+
},
116+
'subject': 'I\'m replacing the subject tag'
117+
}
118+
],
119+
'from': {
120+
'email': '[email protected]'
121+
},
122+
'content': [
123+
{
124+
'type': 'text/html',
125+
'value': 'I\'m replacing the <strong>body tag</strong>'
126+
}
127+
],
128+
'template_id': '13b8f94f-bcae-4ec6-b752-70d6cb59f932'
129+
}";
130+
Object json = JsonConvert.DeserializeObject<Object>(data);
131+
dynamic response = await sg.client.mail.send.post(requestBody: json.ToString());
132+
}
133+
}
134+
}
135+
```

0 commit comments

Comments
 (0)