summaryrefslogtreecommitdiffstats
path: root/SendGrid/Example/Program.cs
blob: 447bd0478d18bcaf141fe494a7a63f883cea9de6 (plain)
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
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Mail;
using Newtonsoft.Json.Linq;

namespace Example
{
    internal class Program
    {
        private static void Main()
        {
            // Test sending email 
            var to = "example@example.com";
            var from = "example@example.com";
            var fromName = "Jane Doe";
            SendEmail(to, from, fromName);
            // Test viewing, creating, modifying and deleting API keys through our v3 Web API 
            ApiKeys();
            UnsubscribeGroups();
            Suppressions();
            GlobalSuppressions();
            GlobalStats();
        }
        
        private static void SendAsync(SendGrid.SendGridMessage message)
        {
            string apikey = Environment.GetEnvironmentVariable("SENDGRID_APIKEY");
            // Create a Web transport for sending email.
            var transportWeb = new SendGrid.Web(apikey);

            // Send the email.
            try
            {
                transportWeb.DeliverAsync(message).Wait();
                Console.WriteLine("Email sent to " + message.To.GetValue(0));
                Console.WriteLine("\n\nPress any key to continue.");
                Console.ReadKey();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.WriteLine("\n\nPress any key to continue.");
                Console.ReadKey();
            }
        }

        private static void SendEmail(string to, string from, string fromName)
        {
            // Create the email object first, then add the properties.
            var myMessage = new SendGrid.SendGridMessage();
            myMessage.AddTo(to);
            myMessage.From = new MailAddress(from, fromName);
            myMessage.Subject = "Testing the SendGrid Library";
            myMessage.Text = "Hello World! %tag%";

            var subs = new List<String> { "私は%type%ラーメンが大好き" };
            myMessage.AddSubstitution("%tag%", subs);
            myMessage.AddSection("%type%", "とんこつ");

            SendAsync(myMessage);
        }

        private static void ApiKeys()
        {
            String apiKey = Environment.GetEnvironmentVariable("SENDGRID_APIKEY", EnvironmentVariableTarget.User);
            var client = new SendGrid.Client(apiKey);

            // GET API KEYS
            HttpResponseMessage responseGet = client.ApiKeys.Get().Result;
            Console.WriteLine(responseGet.StatusCode);
            Console.WriteLine(responseGet.Content.ReadAsStringAsync().Result);
            Console.WriteLine("These are your current API Keys.\n\nPress any key to continue.");
            Console.ReadKey();

            // POST API KEYS
            HttpResponseMessage responsePost = client.ApiKeys.Post("CSharpTestKey").Result;
            var rawString = responsePost.Content.ReadAsStringAsync().Result;
            dynamic jsonObject = JObject.Parse(rawString);
            var apiKeyId = jsonObject.api_key_id.ToString();
            Console.WriteLine(responsePost.StatusCode);
            Console.WriteLine(responsePost.Content.ReadAsStringAsync().Result);
            Console.WriteLine("API Key created.\n\nPress any key to continue.");
            Console.ReadKey();

            // PATCH API KEYS
            HttpResponseMessage responsePatch = client.ApiKeys.Patch(apiKeyId, "CSharpTestKeyPatched").Result;
            Console.WriteLine(responsePatch.StatusCode);
            Console.WriteLine(responsePatch.Content.ReadAsStringAsync().Result);
            Console.WriteLine("API Key patched.\n\nPress any key to continue.");
            Console.ReadKey();

            // DELETE API KEYS
            Console.WriteLine("Deleting API Key, please wait.");
            HttpResponseMessage responseDelete = client.ApiKeys.Delete(apiKeyId).Result;
            Console.WriteLine(responseDelete.StatusCode);
            HttpResponseMessage responseFinal = client.ApiKeys.Get().Result;
            Console.WriteLine(responseFinal.StatusCode);
            Console.WriteLine(responseFinal.Content.ReadAsStringAsync().Result);
            Console.WriteLine("API Key Deleted.\n\nPress any key to end.");
            Console.ReadKey();
        }

        private static void UnsubscribeGroups()
        {
            String apiKey = Environment.GetEnvironmentVariable("SENDGRID_APIKEY", EnvironmentVariableTarget.User);
            var client = new SendGrid.Client(apiKey);

            // GET UNSUBSCRIBE GROUPS
            HttpResponseMessage responseGet = client.UnsubscribeGroups.Get().Result;
            Console.WriteLine(responseGet.StatusCode);
            Console.WriteLine(responseGet.Content.ReadAsStringAsync().Result);
            Console.WriteLine("These are your current Unsubscribe Groups. Press any key to continue.");
            Console.ReadKey();

            // GET A PARTICULAR UNSUBSCRIBE GROUP
            int unsubscribeGroupID = 69;
            HttpResponseMessage responseGetUnique = client.UnsubscribeGroups.Get(unsubscribeGroupID).Result;
            Console.WriteLine(responseGetUnique.StatusCode);
            Console.WriteLine(responseGetUnique.Content.ReadAsStringAsync().Result);
            Console.WriteLine("This is an Unsubscribe Group with ID: " + unsubscribeGroupID.ToString() + ".\n\nPress any key to continue.");
            Console.ReadKey();

            // POST UNSUBSCRIBE GROUP
            HttpResponseMessage responsePost = client.UnsubscribeGroups.Post("C Sharp Unsubscribes", "Testing the C Sharp Library", false).Result;
            var rawString = responsePost.Content.ReadAsStringAsync().Result;
            dynamic jsonObject = JObject.Parse(rawString);
            var unsubscribeGroupId = jsonObject.id.ToString();
            Console.WriteLine(responsePost.StatusCode);
            Console.WriteLine(responsePost.Content.ReadAsStringAsync().Result);
            Console.WriteLine("Unsubscribe Group created.\n\nPress any key to continue.");
            Console.ReadKey();

            // DELETE UNSUBSCRIBE GROUP
            Console.WriteLine("Deleting Unsubscribe Group, please wait.");
            HttpResponseMessage responseDelete = client.UnsubscribeGroups.Delete(unsubscribeGroupId).Result;
            Console.WriteLine(responseDelete.StatusCode);
            HttpResponseMessage responseFinal = client.UnsubscribeGroups.Get().Result;
            Console.WriteLine(responseFinal.StatusCode);
            Console.WriteLine(responseFinal.Content.ReadAsStringAsync().Result);
            Console.WriteLine("Unsubscribe Group Deleted.\n\nPress any key to end.");
            Console.ReadKey();
        }
        
        private static void Suppressions()
        {
            String apiKey = Environment.GetEnvironmentVariable("SENDGRID_APIKEY", EnvironmentVariableTarget.User);
            var client = new SendGrid.Client(apiKey);

            // GET SUPPRESSED ADDRESSES FOR A GIVEN GROUP
            int groupID = 69;
            HttpResponseMessage responseGetUnique = client.Suppressions.Get(groupID).Result;
            Console.WriteLine(responseGetUnique.StatusCode);
            Console.WriteLine(responseGetUnique.Content.ReadAsStringAsync().Result);
            Console.WriteLine("These are the suppressed emails with group ID: " + groupID.ToString() + ". Press any key to continue.");
            Console.ReadKey();

            // ADD EMAILS TO A SUPPRESSION GROUP
            string[] emails = { "example@example.com", "example2@example.com" };
            HttpResponseMessage responsePost = client.Suppressions.Post(groupID, emails).Result;
            var rawString = responsePost.Content.ReadAsStringAsync().Result;
            dynamic jsonObject = JObject.Parse(rawString);
            Console.WriteLine(responsePost.StatusCode);
            Console.WriteLine(responsePost.Content.ReadAsStringAsync().Result);
            Console.WriteLine("Emails added to Suppression Group:" + groupID.ToString() + ".\n\nPress any key to continue.");
            Console.ReadKey();

            // DELETE EMAILS FROM A SUPPRESSION GROUP
            Console.WriteLine("Deleting emails from Suppression Group, please wait.");
            HttpResponseMessage responseDelete1 = client.Suppressions.Delete(groupID, "example@example.com").Result;
            Console.WriteLine(responseDelete1.StatusCode);
            HttpResponseMessage responseDelete2 = client.Suppressions.Delete(groupID, "example2@example.com").Result;
            Console.WriteLine(responseDelete2.StatusCode);
            HttpResponseMessage responseFinal = client.Suppressions.Get(groupID).Result;
            Console.WriteLine(responseFinal.StatusCode);
            Console.WriteLine(responseFinal.Content.ReadAsStringAsync().Result);
            Console.WriteLine("Emails removed from Suppression Group" + groupID.ToString() + "Deleted.\n\nPress any key to end.");
            Console.ReadKey();
        }

        private static void GlobalSuppressions()
        {
            String apiKey = Environment.GetEnvironmentVariable("SENDGRID_APIKEY", EnvironmentVariableTarget.User);
            var client = new SendGrid.Client(apiKey);

            // CHECK IF EMAIL IS ON THE GLOBAL SUPPRESSION LIST
            var email = "elmer.thomas+test_global@gmail.com";
            HttpResponseMessage responseGetUnique = client.GlobalSuppressions.Get(email).Result;
            Console.WriteLine(responseGetUnique.StatusCode);
            Console.WriteLine(responseGetUnique.Content.ReadAsStringAsync().Result);
            Console.WriteLine("Determines if the given email is listed on the Global Suppressions list. Press any key to continue.");
            Console.ReadKey();

            // ADD EMAILS TO THE GLOBAL SUPPRESSION LIST
            string[] emails = { "example@example.com", "example2@example.com" };
            HttpResponseMessage responsePost = client.GlobalSuppressions.Post(emails).Result;
            var rawString = responsePost.Content.ReadAsStringAsync().Result;
            dynamic jsonObject = JObject.Parse(rawString);
            Console.WriteLine(responsePost.StatusCode);
            Console.WriteLine(responsePost.Content.ReadAsStringAsync().Result);
            Console.WriteLine("Emails added to Global Suppression Group.\n\nPress any key to continue.");
            Console.ReadKey();

            // DELETE EMAILS FROM THE GLOBAL SUPPRESSION GROUP
            Console.WriteLine("Deleting emails from Global Suppression Group, please wait.");
            HttpResponseMessage responseDelete1 = client.GlobalSuppressions.Delete("example@example.com").Result;
            Console.WriteLine(responseDelete1.StatusCode);
            HttpResponseMessage responseDelete2 = client.GlobalSuppressions.Delete("example2@example.com").Result;
            Console.WriteLine(responseDelete2.StatusCode);
            HttpResponseMessage responseFinal = client.GlobalSuppressions.Get("example@example.com").Result;
            Console.WriteLine(responseFinal.StatusCode);
            Console.WriteLine(responseFinal.Content.ReadAsStringAsync().Result);
            HttpResponseMessage responseFinal2 = client.GlobalSuppressions.Get("example2@example.com").Result;
            Console.WriteLine(responseFinal2.StatusCode);
            Console.WriteLine(responseFinal2.Content.ReadAsStringAsync().Result);
            Console.WriteLine("Emails removed from Global Suppression Group.\n\nPress any key to end.");
            Console.ReadKey();
        }

        private static void GlobalStats()
        {
            String apiKey = Environment.GetEnvironmentVariable("SENDGRID_APIKEY", EnvironmentVariableTarget.User);
            var client = new SendGrid.Client(apiKey);

            // Global Stats provide all of your user’s email statistics for a given date range.
            var startDate = "2015-11-01";
            HttpResponseMessage response = client.GlobalStats.Get(startDate).Result;
            Console.WriteLine(response.StatusCode);
            Console.WriteLine(response.Content.ReadAsStringAsync().Result);
            Console.WriteLine("Display global email stats, with start date " + startDate + "and no end date.\n\nPress any key to continue.");
            Console.ReadKey();

            var endDate = "2015-12-01";
            response = client.GlobalStats.Get(startDate, endDate).Result;
            Console.WriteLine(response.StatusCode);
            Console.WriteLine(response.Content.ReadAsStringAsync().Result);
            Console.WriteLine("Display global email stats, with start date " + startDate + "and end date " + endDate + ".\n\nPress any key to continue.");
            Console.ReadKey();

            var aggregatedBy = "day";
            response = client.GlobalStats.Get(startDate, endDate, aggregatedBy).Result;
            Console.WriteLine(response.StatusCode);
            Console.WriteLine(response.Content.ReadAsStringAsync().Result);
            Console.WriteLine("Display global email stats, with start date " + startDate + "and end date " + endDate + " and aggregated by " + aggregatedBy + ".\n\nPress any key to continue.");
            Console.ReadKey();

            aggregatedBy = "week";
            response = client.GlobalStats.Get(startDate, endDate, aggregatedBy).Result;
            Console.WriteLine(response.StatusCode);
            Console.WriteLine(response.Content.ReadAsStringAsync().Result);
            Console.WriteLine("Display global email stats, with start date " + startDate + "and end date " + endDate + " and aggregated by " + aggregatedBy + ".\n\nPress any key to continue.");
            Console.ReadKey();

            aggregatedBy = "month";
            response = client.GlobalStats.Get(startDate, endDate, aggregatedBy).Result;
            Console.WriteLine(response.StatusCode);
            Console.WriteLine(response.Content.ReadAsStringAsync().Result);
            Console.WriteLine("Display global email stats, with start date " + startDate + "and end date " + endDate + " and aggregated by " + aggregatedBy + ".\n\nPress any key to continue.");
            Console.ReadKey();
        }

    }
}