forked from sendgrid/sendgrid-csharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
176 lines (159 loc) · 7.79 KB
/
Program.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
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
using Newtonsoft.Json;
using SendGrid;
using SendGrid.Helpers.Mail;
namespace Example
{
internal class Program
{
// HttpClient is intended to be instantiated once per application, rather than per-use.
// See https://docs.microsoft.com/dotnet/api/system.net.http.httpclient#remarks
private static readonly HttpClient HttpClient = new HttpClient();
private static async Task Main()
{
var env = Environment.GetEnvironmentVariable("DOTNET_ENVIRONMENT") ?? "Production";
var configuration = new ConfigurationBuilder()
.AddJsonFile("appsettings.json", optional: true)
.AddJsonFile($"appsettings.{env}.json", optional: true)
.Build();
// Retrieve the API key.
var apiKey = Environment.GetEnvironmentVariable("SENDGRID_API_KEY") ?? configuration["SendGrid:ApiKey"];
var client = new SendGridClient(HttpClient, new SendGridClientOptions { ApiKey = apiKey, HttpErrorAsException = true });
// Send a Single Email using the Mail Helper
var from = new EmailAddress(configuration.GetValue("SendGrid:From", "[email protected]"), "Example User");
var subject = "Hello World from the Twilio SendGrid CSharp Library Helper!";
var to = new EmailAddress(configuration.GetValue("SendGrid:To", "[email protected]"), "Example User");
var plainTextContent = "Hello, Email from the helper [SendSingleEmailAsync]!";
var htmlContent = "<strong>Hello, Email from the helper! [SendSingleEmailAsync]</strong>";
var msg = MailHelper.CreateSingleEmail(from, to, subject, plainTextContent, htmlContent);
var response = await client.SendEmailAsync(msg);
Console.WriteLine(msg.Serialize());
Console.WriteLine(response.StatusCode);
Console.WriteLine(response.Headers);
Console.WriteLine("\n\nPress <Enter> to continue.");
Console.ReadLine();
// Send a Single Email using the Mail Helper with convenience methods and initialized SendGridMessage object
msg = new SendGridMessage
{
From = from,
Subject = subject,
PlainTextContent = plainTextContent,
HtmlContent = htmlContent
};
msg.AddTo(to);
response = await client.SendEmailAsync(msg);
Console.WriteLine(msg.Serialize());
Console.WriteLine(response.StatusCode);
Console.WriteLine(response.Headers);
Console.WriteLine("\n\nPress <Enter> to continue.");
Console.ReadLine();
// Send a Single Email using the Mail Helper, entirely with convenience methods
msg = new SendGridMessage();
msg.SetFrom(from);
msg.SetSubject(subject);
msg.AddContent(MimeType.Text, plainTextContent);
msg.AddContent(MimeType.Html, htmlContent);
msg.AddTo(to);
response = await client.SendEmailAsync(msg);
Console.WriteLine(msg.Serialize());
Console.WriteLine(response.StatusCode);
Console.WriteLine(response.Headers);
Console.WriteLine("\n\nPress <Enter> to continue.");
Console.ReadLine();
// Send a Single Email Without the Mail Helper
var data = @"{
'personalizations': [
{
'to': [
{
'email': '[email protected]'
}
],
'subject': 'Hello World from the Twilio SendGrid C# Library!'
}
],
'from': {
'email': '[email protected]'
},
'content': [
{
'type': 'text/plain',
'value': 'Hello, Email!'
}
]
}";
var json = JsonConvert.DeserializeObject<object>(data);
response = await client.RequestAsync(BaseClient.Method.POST,
json.ToString(),
urlPath: "mail/send");
Console.WriteLine(response.StatusCode);
Console.WriteLine(response.Headers);
Console.WriteLine("\n\nPress <Enter> to continue.");
Console.ReadLine();
// GET Collection
var queryParams = @"{
'limit': 100
}";
response = await client.RequestAsync(method: BaseClient.Method.GET,
urlPath: "asm/groups",
queryParams: queryParams);
Console.WriteLine(response.StatusCode);
Console.WriteLine(response.Body.ReadAsStringAsync().Result);
Console.WriteLine(response.Headers);
Console.WriteLine("\n\nPress <Enter> to continue to POST.");
Console.ReadLine();
// POST
var requestBody = @"{
'description': 'Suggestions for products our users might like.',
'is_default': false,
'name': 'Magic Products'
}";
json = JsonConvert.DeserializeObject<object>(requestBody);
response = await client.RequestAsync(method: BaseClient.Method.POST,
urlPath: "asm/groups",
requestBody: json.ToString());
var dsResponse = JsonConvert.DeserializeObject<Dictionary<string, dynamic>>(response.Body.ReadAsStringAsync().Result);
Console.WriteLine(response.StatusCode);
Console.WriteLine(response.Body.ReadAsStringAsync().Result);
Console.WriteLine(response.Headers);
Console.WriteLine("\n\nPress <Enter> to continue to GET single.");
Console.ReadLine();
if (dsResponse != null && dsResponse.ContainsKey("id"))
{
var groupId = dsResponse["id"].ToString();
// GET Single
response = await client.RequestAsync(method: BaseClient.Method.GET,
urlPath: $"asm/groups/{groupId}");
Console.WriteLine(response.StatusCode);
Console.WriteLine(response.Body.ReadAsStringAsync().Result);
Console.WriteLine(response.Headers);
Console.WriteLine("\n\nPress <Enter> to continue to PATCH.");
Console.ReadLine();
// PATCH
requestBody = @"{
'name': 'Cool Magic Products'
}";
json = JsonConvert.DeserializeObject<object>(requestBody);
response = await client.RequestAsync(method: BaseClient.Method.PATCH,
urlPath: $"asm/groups/{groupId}",
requestBody: json.ToString());
Console.WriteLine(response.StatusCode);
Console.WriteLine(response.Body.ReadAsStringAsync().Result);
Console.WriteLine(response.Headers.ToString());
Console.WriteLine("\n\nPress <Enter> to continue to PUT.");
Console.ReadLine();
// DELETE
response = await client.RequestAsync(method: BaseClient.Method.DELETE,
urlPath: $"asm/groups/{groupId}");
Console.WriteLine(response.StatusCode);
Console.WriteLine(response.Headers.ToString());
Console.WriteLine("\n\nPress <Enter> to DELETE and exit.");
Console.ReadLine();
}
}
}
}