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
|
using Smtpapi;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Mail;
using System.Text;
namespace SendGridMail {
public class MailBuilder {
private SendGrid sendgrid;
private bool? enableFlag;
public static MailBuilder Create() {
var mailBuilder = new MailBuilder();
mailBuilder.sendgrid = SendGrid.GetInstance();
return new MailBuilder();
}
public SendGrid Build() {
if (string.IsNullOrEmpty(sendgrid.Html) && string.IsNullOrEmpty(sendgrid.Text)) {
throw new InvalidOperationException("Mail does not contain a body.");
}
if (sendgrid.To.Length == 0) { throw new InvalidOperationException("Mail does not have any recipients."); }
if (sendgrid.From == null) { throw new InvalidOperationException("Mail does not have a valid sender's email address."); }
if (string.IsNullOrEmpty(sendgrid.Subject)) { throw new InvalidOperationException("Mail does not have a subject."); }
return sendgrid;
}
public MailBuilder Enable {
get {
enableFlag = true;
return this;
}
}
public MailBuilder Disable {
get {
enableFlag = false;
return this;
}
}
public MailBuilder From(MailAddress address) {
sendgrid.From = address;
return this;
}
public MailBuilder From(string email) {
sendgrid.From = new MailAddress(email);
return this;
}
public MailBuilder From(string email, string displayName) {
sendgrid.From = new MailAddress(email, displayName);
return this;
}
public MailBuilder To(MailAddress address) {
return this.To(address.Address, address.DisplayName);
}
public MailBuilder To(string email) {
sendgrid.AddTo(email);
return this;
}
public MailBuilder To(string email, string displayName) {
sendgrid.AddTo(string.Format("{0} <{1}>", displayName, email));
return this;
}
public MailBuilder Cc(MailAddress address) {
return this.Cc(address.Address, address.DisplayName);
}
public MailBuilder Cc(string email) {
sendgrid.AddCc(email);
return this;
}
public MailBuilder Cc(string email, string displayName) {
sendgrid.AddCc(string.Format("{0} <{1}>", displayName, email));
return this;
}
public MailBuilder Bcc(MailAddress address) {
return this.Bcc(address.Address, address.DisplayName);
}
public MailBuilder Bcc(string email, string displayName) {
sendgrid.AddBcc(string.Format("{0} <{1}>", displayName, email));
return this;
}
public MailBuilder Subject(string subject) {
sendgrid.Subject = subject;
return this;
}
public MailBuilder Html(string html) {
sendgrid.Html = html;
return this;
}
public MailBuilder Text(string text) {
sendgrid.Text = text;
return this;
}
public MailBuilder WithSubstitution(string replacementTag, List<string> substitutionValues) {
sendgrid.AddSubstitution(replacementTag, substitutionValues);
return this;
}
public MailBuilder WithUniqueArgs(IDictionary<string, string> identifiers) {
sendgrid.AddUniqueArgs(identifiers);
return this;
}
public MailBuilder WithCategory(string category) {
sendgrid.SetCategory(category);
return this;
}
public MailBuilder WithCategories(IEnumerable<string> categories) {
sendgrid.SetCategories(categories);
return this;
}
public MailBuilder WithAttachment(Stream stream, string name) {
sendgrid.AddAttachment(stream, name);
return this;
}
public MailBuilder WithAttachment(string filePath) {
sendgrid.AddAttachment(filePath);
return this;
}
public MailBuilder WithHeaders(IDictionary<string, string> headers) {
sendgrid.AddHeaders(headers);
return this;
}
/// <summary>
/// Preface with Enable or Disable to set the status of the Gravatar filter
/// </summary>
/// <returns></returns>
public MailBuilder Gravatar() {
if (!enableFlag.HasValue) { throw new InvalidOperationException("Builder requires 'Enable' or 'Disable' before this method."); }
if (enableFlag.Value) { sendgrid.EnableGravatar(); }
else { sendgrid.DisableGravatar(); }
enableFlag = null;
return this;
}
/// <summary>
/// Preface with Enable or Disable to set the status of the Open Tracking filter
/// </summary>
/// <returns></returns>
public MailBuilder OpenTracking() {
if (!enableFlag.HasValue) { throw new InvalidOperationException("Builder requires 'Enable' or 'Disable' before this method."); }
if (enableFlag.Value) { sendgrid.EnableOpenTracking(); }
else { sendgrid.DisableOpenTracking(); }
enableFlag = null;
return this;
}
/// <summary>
/// Preface with Enable or Disable to set the status of the Click Tracking filter
/// </summary>
/// <param name="includePlainText"></param>
/// <returns></returns>
public MailBuilder ClickTracking(bool includePlainText = false) {
if (!enableFlag.HasValue) { throw new InvalidOperationException("Builder requires 'Enable' or 'Disable' before this method."); }
if (enableFlag.Value) { sendgrid.EnableClickTracking(includePlainText); }
else { sendgrid.DisableClickTracking(); }
enableFlag = null;
return this;
}
/// <summary>
/// Preface with Enable or Disable to set the status of the Spam Checking filter
/// </summary>
/// <param name="score"></param>
/// <param name="url"></param>
/// <returns></returns>
public MailBuilder SpamCheck(int score = 5, string url = null) {
if (!enableFlag.HasValue) { throw new InvalidOperationException("Builder requires 'Enable' or 'Disable' before this method."); }
if (enableFlag.Value) { sendgrid.EnableSpamCheck(score, url); }
else { sendgrid.DisableSpamCheck(); }
enableFlag = null;
return this;
}
/// <summary>
/// When prefaced with Disable, turns off the Unsubscribe Tracking filter
/// </summary>
/// <returns></returns>
public MailBuilder Unsubscribe() {
if (!enableFlag.HasValue) { throw new InvalidOperationException("Builder requires 'Enable' or 'Disable' before this method."); }
if (enableFlag.Value) { throw new ArgumentException("Arguments need to be specified to enable this item."); }
else { sendgrid.DisableUnsubscribe(); }
enableFlag = null;
return this;
}
/// <summary>
/// When prefaced with Enable, turns on the Unsubscribe Tracking filter
/// </summary>
/// <param name="text"></param>
/// <param name="html"></param>
/// <returns></returns>
public MailBuilder Unsubscribe(string text, string html) {
if (!enableFlag.HasValue) { throw new InvalidOperationException("Builder requires 'Enable' or 'Disable' before this method."); }
if (enableFlag.Value) { sendgrid.EnableUnsubscribe(text, html); }
else { sendgrid.DisableUnsubscribe(); }
enableFlag = null;
return this;
}
/// <summary>
/// When prefaced with Enable, turns on the Unsubscribe Tracking filter
/// </summary>
/// <param name="replace"></param>
/// <returns></returns>
public MailBuilder Unsubscribe(string replace) {
if (!enableFlag.HasValue) { throw new InvalidOperationException("Builder requires 'Enable' or 'Disable' before this method."); }
if (enableFlag.Value) { sendgrid.EnableUnsubscribe(replace); }
else { sendgrid.DisableUnsubscribe(); }
enableFlag = null;
return this;
}
/// <summary>
/// Preface with Enable or Disable to set the status of the Footer filter
/// </summary>
/// <param name="text"></param>
/// <param name="html"></param>
/// <returns></returns>
public MailBuilder Footer(string text = null, string html = null) {
if (!enableFlag.HasValue) { throw new InvalidOperationException("Builder requires 'Enable' or 'Disable' before this method."); }
if (enableFlag.Value) { sendgrid.EnableFooter(text, html); }
else { sendgrid.DisableFooter(); }
enableFlag = null;
return this;
}
/// <summary>
/// When prefaced with Disable, turns off the Google Analytics filter
/// </summary>
/// <returns></returns>
public MailBuilder GoogleAnalytics() {
if (!enableFlag.HasValue) { throw new InvalidOperationException("Builder requires 'Enable' or 'Disable' before this method."); }
if (enableFlag.Value) { throw new ArgumentException("Arguments need to be specified to enable this item."); }
else { sendgrid.DisableGoogleAnalytics(); }
enableFlag = null;
return this;
}
/// <summary>
/// When prefaced with Enable, turns on the Google Analytics filter
/// </summary>
/// <param name="source"></param>
/// <param name="medium"></param>
/// <param name="term"></param>
/// <param name="content"></param>
/// <param name="campaign"></param>
/// <returns></returns>
public MailBuilder GoogleAnalytics(string source, string medium, string term, string content = null, string campaign = null) {
if (!enableFlag.HasValue) { throw new InvalidOperationException("Builder requires 'Enable' or 'Disable' before this method."); }
if (enableFlag.Value) { sendgrid.EnableGoogleAnalytics(source, medium, term, content, campaign); }
else { sendgrid.DisableGoogleAnalytics(); }
enableFlag = null;
return this;
}
/// <summary>
/// When prefaced with Disable, turns off the Template filter
/// </summary>
/// <returns></returns>
public MailBuilder Template() {
if (!enableFlag.HasValue) { throw new InvalidOperationException("Builder requires 'Enable' or 'Disable' before this method."); }
if (enableFlag.Value) { throw new ArgumentException("Arguments need to be specified to enable this item."); }
else { sendgrid.DisableTemplate(); }
enableFlag = null;
return this;
}
/// <summary>
/// When prefaced with Enable, turns on the Template filter
/// </summary>
/// <param name="html"></param>
/// <returns></returns>
public MailBuilder Template(string html) {
if (!enableFlag.HasValue) { throw new InvalidOperationException("Builder requires 'Enable' or 'Disable' before this method."); }
if (enableFlag.Value) { sendgrid.EnableTemplate(html); }
else { sendgrid.DisableTemplate(); }
enableFlag = null;
return this;
}
/// <summary>
/// When prefaced with Disable, turns off the Bcc filter
/// </summary>
/// <returns></returns>
public MailBuilder Bcc() {
if (!enableFlag.HasValue) { throw new InvalidOperationException("Builder requires 'Enable' or 'Disable' before this method."); }
if (enableFlag.Value) { throw new ArgumentException("Arguments need to be specified to enable this item."); }
else { sendgrid.DisableBcc(); }
enableFlag = null;
return this;
}
/// <summary>
/// When prefaced with Enable, turns on the Bcc filter. Otherwise, sets the Bcc address
/// </summary>
/// <param name="email"></param>
/// <returns></returns>
public MailBuilder Bcc(string email) {
if (!enableFlag.HasValue) { sendgrid.AddBcc(email); }
else if (enableFlag.Value) { sendgrid.EnableBcc(email); }
else { sendgrid.DisableBcc(); }
enableFlag = null;
return this;
}
/// <summary>
/// Preface with Enable or Disable to set the status of the Bypass List Management filter
/// </summary>
/// <returns></returns>
public MailBuilder BypassListManagement() {
if (!enableFlag.HasValue) { throw new InvalidOperationException("Builder requires 'Enable' or 'Disable' before this method."); }
if (enableFlag.Value) { sendgrid.EnableBypassListManagement(); }
else { sendgrid.DisableBypassListManagement(); }
enableFlag = null;
return this;
}
}
}
|