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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
|
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net.Mail;
using System.Net.Mime;
using System.Runtime.InteropServices.ComTypes;
using System.Text;
namespace SendGridMail
{
public class SendGrid : ISendGrid
{
private IHeader header;
private Dictionary<String, String> _filters;
//apps list and settings
private const String ReText = @"<\%\s*\%>";
private const String ReHtml = @"<\%\s*[^\s]+\s*\%>";
public SendGrid(IHeader header)
{
this.header = header;
//initialize the filters, for use within the library
this.InitializeFilters();
}
public void InitializeFilters()
{
this._filters =
new Dictionary<string, string>
{
{"Gravatar", "gravatar"},
{"OpenTracking", "opentrack"},
{"ClickTracking", "clicktrack"},
{"SpamCheck", "spamcheck"},
{"Unsubscribe", "subscriptiontrack"},
{"Footer", "footer"},
{"GoogleAnalytics", "ganalytics"},
{"Template", "template"},
{"Bcc", "bcc"},
{"BypassListManagement", "bypass_list_management"}
};
}
private MailMessage message;
// TODO find appropriate types for these
const string encoding = "quoted-printable";
const string charset = "utf-8";
/*
if (Html != null )
{
AlternateView htmlView = AlternateView.CreateAlternateViewFromString(html, null, "text/html");
message.AlternateViews.Add(htmlView);
}
if (Text != null )
{
AlternateView plainView = AlternateView.CreateAlternateViewFromString(Text, null, "text/plain");
message.AlternateViews.Add(plainView);
}
message.BodyEncoding = Encoding.GetEncoding(charset);
*/
public SendGrid(MailAddress from, MailAddress[] to, MailAddress[] cc, MailAddress[] bcc,
String subject, String html, String text, TransportType transport, IHeader header = null )
{
message = new MailMessage();
Header = header;
From = from;
To = to;
_subs = new Dictionary<string, string>();
message.Subject = subject;
message.SubjectEncoding = Encoding.GetEncoding(charset);
Text = text;
Html = html;
}
#region Properties
public MailAddress From
{
get
{
return message.From;
}
set
{
if (value != null) message.From = value;
}
}
public MailAddress[] To
{
get
{
return message.To.ToArray();
}
set
{
message.To.Clear();
foreach (var mailAddress in value)
{
message.To.Add(mailAddress);
}
}
}
public MailAddress[] Cc
{
get
{
return message.CC.ToArray();
}
set
{
message.CC.Clear();
foreach (var mailAddress in value)
{
message.CC.Add(mailAddress);
}
}
}
public MailAddress[] Bcc
{
get
{
return message.Bcc.ToArray();
}
set
{
message.Bcc.Clear();
foreach (var mailAddress in value)
{
message.Bcc.Add(mailAddress);
}
}
}
public String Subject
{
get
{
return message.Subject;
}
set
{
if (value != null) message.Subject = value;
}
}
public IHeader Header
{
get { throw new NotImplementedException(); }
set { throw new NotImplementedException(); }
}
public String Html { get; set; }
public String Text { get; set; }
public TransportType Transport { get; set; }
#endregion
#region Methods for setting data
public void AddTo(String address)
{
var mailAddress = new MailAddress(address);
message.To.Add(mailAddress);
}
public void AddTo(IEnumerable<String> addresses)
{
if (addresses != null)
{
foreach (var address in addresses)
{
if (address != null) AddTo(address);
}
}
}
public void AddTo(IDictionary<String, IDictionary<String, String>> addresssInfo)
{
foreach (var address in addresssInfo.Keys)
{
var table = addresssInfo[address];
//DisplayName is the only option that this implementation of MailAddress implements.
var mailAddress = new MailAddress(address, table.ContainsKey("DisplayName") ? table["DisplayName"] : null);
message.To.Add(mailAddress);
}
}
public void AddCc(String address)
{
var mailAddress = new MailAddress(address);
message.CC.Add(mailAddress);
}
public void AddCc(IEnumerable<String> addresses)
{
if (addresses != null)
{
foreach (var address in addresses)
{
if (address != null) AddCc(address);
}
}
}
public void AddCc(IDictionary<String, IDictionary<String, String>> addresssInfo)
{
foreach (var address in addresssInfo.Keys)
{
var table = addresssInfo[address];
//DisplayName is the only option that this implementation of MailAddress implements.
var mailAddress = new MailAddress(address, table.ContainsKey("DisplayName") ? table["DisplayName"] : null);
message.CC.Add(mailAddress);
}
}
public void AddBcc(String address)
{
var mailAddress = new MailAddress(address);
message.Bcc.Add(mailAddress);
}
public void AddBcc(IEnumerable<String> addresses)
{
if (addresses != null)
{
foreach (var address in addresses)
{
if (address != null) AddBcc(address);
}
}
}
public void AddBcc(IDictionary<String, IDictionary<String, String>> addresssInfo)
{
foreach (var address in addresssInfo.Keys)
{
var table = addresssInfo[address];
//DisplayName is the only option that this implementation of MailAddress implements.
var mailAddress = new MailAddress(address, table.ContainsKey("DisplayName") ? table["DisplayName"] : null);
message.Bcc.Add(mailAddress);
}
}
private Dictionary<string, string> _subs;
public void AddSubVal(string tag, string value)
{
//let the system complain if they do something bad, since the function returns null
_subs[tag] = value;
}
public void AddAttachment(string filePath)
{
var data = new Attachment(filePath, MediaTypeNames.Application.Octet);
message.Attachments.Add(data);
}
public void AddAttachment(Attachment attachment)
{
message.Attachments.Add(attachment);
}
public void AddAttachment(Stream attachment, ContentType type)
{
var data = new Attachment(attachment, type);
}
public IEnumerable<string> GetRecipients()
{
List<MailAddress> tos = message.To.ToList();
List<MailAddress> ccs = message.CC.ToList();
List<MailAddress> bccs = message.Bcc.ToList();
var rcpts = tos.Union(ccs.Union(bccs)).Select(address => address.Address);
return rcpts;
}
private string Get(string field)
{
throw new NotImplementedException();
}
private void Set(string field, string value)
{
throw new NotImplementedException();
}
#endregion
#region SMTP API Functions
public void DisableGravatar()
{
this.header.Disable(this._filters["Gravatar"]);
}
public void DisableOpenTracking()
{
this.header.Disable(this._filters["OpenTracking"]);
}
public void DisableClickTracking()
{
this.header.Disable(this._filters["ClickTracking"]);
}
public void DisableSpamCheck()
{
this.header.Disable(this._filters["SpamCheck"]);
}
public void DisableUnsubscribe()
{
this.header.Disable(this._filters["Unsubscribe"]);
}
public void DisableFooter()
{
this.header.Disable(this._filters["Footer"]);
}
public void DisableGoogleAnalytics()
{
this.header.Disable(this._filters["GoogleAnalytics"]);
}
public void DisableTemplate()
{
this.header.Disable(this._filters["Template"]);
}
public void DisableBcc()
{
this.header.Disable(this._filters["Bcc"]);
}
public void DisableBypassListManagement()
{
this.header.Disable(this._filters["BypassListManagement"]);
}
public void EnableGravatar()
{
this.header.Enable(this._filters["Gravatar"]);
}
public void EnableOpenTracking()
{
this.header.Enable(this._filters["OpenTracking"]);
}
public void EnableClickTracking(string text = null)
{
var filter = this._filters["ClickTracking"];
this.header.Enable(filter);
this.header.AddFilterSetting(filter, new List<string>(){ "text" }, text);
}
public void EnableSpamCheck(int score = 5, string url = null)
{
var filter = this._filters["SpamCheck"];
this.header.Enable(filter);
this.header.AddFilterSetting(filter, new List<string>(){ "score" }, score.ToString(CultureInfo.InvariantCulture));
this.header.AddFilterSetting(filter, new List<string>(){ "url" }, url);
}
public void EnableUnsubscribe(string text, string html, string replace, string url, string landing)
{
var filter = this._filters["Unsubscribe"];
if(!System.Text.RegularExpressions.Regex.IsMatch(text, SendGrid.ReText))
{
throw new Exception("Missing substitution tag in text");
}
if(!System.Text.RegularExpressions.Regex.IsMatch(html, SendGrid.ReHtml))
{
throw new Exception("Missing substitution tag in html");
}
this.header.Enable(filter);
this.header.AddFilterSetting(filter, new List<string>(){ "text" }, text);
this.header.AddFilterSetting(filter, new List<string>(){ "html" }, html);
this.header.AddFilterSetting(filter, new List<string>(){ "replace"}, replace);
this.header.AddFilterSetting(filter, new List<string>(){ "landing" }, landing);
}
public void EnableFooter(string text = null, string html = null)
{
var filter = this._filters["Footer"];
this.header.Enable(filter);
this.header.AddFilterSetting(filter, new List<string>(){ "text" }, text);
this.header.AddFilterSetting(filter, new List<string>(){ "html" }, html);
}
public void EnableGoogleAnalytics(string source, string medium, string term, string content = null, string campaign = null)
{
var filter = this._filters["GoogleAnalytics"];
this.header.Enable(filter);
this.header.AddFilterSetting(filter, new List<string>(){ "source " }, source);
this.header.AddFilterSetting(filter, new List<string>(){ "medium" }, medium);
this.header.AddFilterSetting(filter, new List<string>(){ "term" }, term);
this.header.AddFilterSetting(filter, new List<string>(){ "content" }, content);
this.header.AddFilterSetting(filter, new List<string>(){ "compaign" }, campaign);
}
public void EnableTemplate(string html)
{
var filter = this._filters["Template"];
if (!System.Text.RegularExpressions.Regex.IsMatch(html, SendGrid.ReHtml))
{
throw new Exception("Missing substitution tag in html");
}
this.header.Enable(filter);
this.header.AddFilterSetting(filter, new List<string>(){ "html" }, html);
}
public void EnableBcc(string email)
{
var filter = this._filters["Bcc"];
this.header.Enable(filter);
this.header.AddFilterSetting(filter, new List<string>(){ "email" }, email);
}
public void EnableBypassListManagement()
{
this.header.Enable(this._filters["BypassListManagement"]);
}
#endregion
public MailMessage CreateMimeMessage()
{
String smtpapi = Header.AsJson();
if (!String.IsNullOrEmpty(smtpapi))
message.Headers.Add("X-SmtpApi", "{" + smtpapi + "}");
return message;
}
public void Mail()
{
throw new NotImplementedException();
}
}
}
|