summaryrefslogtreecommitdiffstats
path: root/SendGrid/SendGridMail/MailBuilder.cs
blob: 2457d2e404afe76da89a5b6eb75686ce1def38e4 (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
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Mail;
using System.Text;
using Smtpapi;

namespace SendGridMail {
    public sealed class MailBuilder {
        private SendGrid sendgrid;
        private bool hideRecipients = false;

        public static MailBuilder Create() {
            var mailBuilder = new MailBuilder();
            mailBuilder.sendgrid = SendGrid.GetInstance();
            return mailBuilder;
        }
        public SendGrid Build() {
            var exceptions = new List<Exception>();
            if (string.IsNullOrEmpty(sendgrid.Html) && string.IsNullOrEmpty(sendgrid.Text)) {
                exceptions.Add(new InvalidOperationException("Mail does not contain a body."));
            }
            if (sendgrid.To.Length == 0) { exceptions.Add(new InvalidOperationException("Mail does not have any recipients.")); }
            if (sendgrid.From == null) { exceptions.Add(new InvalidOperationException("Mail does not have a valid sender's email address.")); }
            if (string.IsNullOrEmpty(sendgrid.Subject)) { exceptions.Add(new InvalidOperationException("Mail does not have a subject.")); }
            if (exceptions.Count > 0) { throw new AggregateException("Mail has one or more issues and cannot be built.  Check InnerExceptions for details.", exceptions); }
            if (hideRecipients) {
                sendgrid.Header.SetTo(sendgrid.To.ToListString());
                sendgrid.To = new MailAddress[1] { sendgrid.From };
            }
            return sendgrid;
        }

        public MailBuilder HideRecipients() {
            hideRecipients = true;
            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(EmailFormat(displayName, email));
            return this;
        }
        public MailBuilder To(IEnumerable<string> addresses) {
            sendgrid.AddTo(addresses);
            return this;
        }
        public MailBuilder To(IEnumerable<MailAddress> addresses) {
            return this.To(addresses.ToListString());
        }
        public MailBuilder To(MailAddressCollection addresses) {
            return this.To(addresses.ToList());
        }

        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(EmailFormat(displayName, email));
            return this;
        }
        public MailBuilder Cc(IEnumerable<string> addresses) {
            sendgrid.AddCc(addresses);
            return this;
        }
        public MailBuilder Cc(IEnumerable<MailAddress> addresses) {
            return this.Cc(addresses.ToListString());
        }
        public MailBuilder Cc(MailAddressCollection addresses) {
            return this.Cc(addresses.ToList());
        }

        public MailBuilder Bcc(MailAddress address) {
            return this.Bcc(address.Address, address.DisplayName);
        }
        public MailBuilder Bcc(string email) {
            sendgrid.AddBcc(email);
            return this;
        }
        public MailBuilder Bcc(string email, string displayName) {
            sendgrid.AddBcc(EmailFormat(displayName, email));
            return this;
        }
        public MailBuilder Bcc(IEnumerable<string> addresses) {
            sendgrid.AddBcc(addresses);
            return this;
        }
        public MailBuilder Bcc(IEnumerable<MailAddress> addresses) {
            return this.Bcc(addresses.ToListString());
        }
        public MailBuilder Bcc(MailAddressCollection addresses) {
            return this.Bcc(addresses.ToList());
        }

        public MailBuilder Subject(string subject) {
            sendgrid.Subject = subject;
            return this;
        }

        public MailBuilder Html(AlternateView view) {
            return this
                .Html(GetAlternateViewAsString(view))
                .EmbedImages(view.LinkedResources);
        }
        public MailBuilder Html(string html) {
            sendgrid.Html = html;
            return this;
        }

        public MailBuilder Text(AlternateView view) {
            return this
                .Text(GetAlternateViewAsString(view));
        }
        public MailBuilder Text(string text) {
            sendgrid.Text = text;
            return this;
        }

        public MailBuilder AttachFile(Stream stream, string name) {
            sendgrid.AddAttachment(stream, name);
            return this;
        }
        public MailBuilder AttachFile(string filePath) {
            sendgrid.AddAttachment(filePath);
            return this;
        }
        public MailBuilder AttachFile(Attachment attachment) {
            return this.AttachFile(attachment.ContentStream, attachment.Name);
        }
        public MailBuilder AttachFiles(IEnumerable<Attachment> attachments) {
            foreach (var attachment in attachments) { this.AttachFile(attachment); }
            return this;
        }
        public MailBuilder AttachFiles(AttachmentCollection attachments) {
            return this.AttachFiles(attachments.ToList());
        }

        public MailBuilder EmbedImage(Stream stream, string name, string cid) {
            sendgrid.AddAttachment(stream, name);
            sendgrid.EmbedImage(name, cid);
            return this;
        }
        public MailBuilder EmbedImage(string filePath, string cid) {
            sendgrid.AddAttachment(filePath);
            sendgrid.EmbedImage(new FileInfo(filePath).Name, cid);
            return this;
        }
        public MailBuilder EmbedImage(LinkedResource resource) {
            return this.EmbedImage(resource.ContentStream, resource.ContentId, resource.ContentId);
        }
        public MailBuilder EmbedImages(IEnumerable<LinkedResource> resources) {
            foreach (var resource in resources) { this.EmbedImage(resource); }
            return this;
        }
        public MailBuilder EmbedImages(LinkedResourceCollection resources) {
            return this.EmbedImages(resources.ToList());
        }

        public MailBuilder AddHeader(string key, string value) {
            sendgrid.AddHeaders(new Dictionary<string, string> { { key, value } });
            return this;
        }
        public MailBuilder AddHeaders(IDictionary<string, string> headers) {
            sendgrid.AddHeaders(headers);
            return this;
        }
        public MailBuilder Substitute(string replacementTag, IEnumerable<string> substitutionValues) {
            sendgrid.AddSubstitution(replacementTag, substitutionValues.ToList());
            return this;
        }

        public MailBuilder IncludeUniqueArg(string key, string value) {
            sendgrid.AddUniqueArgs(new Dictionary<string, string> { { key, value } });
            return this;
        }
        public MailBuilder IncludeUniqueArgs(IDictionary<string, string> identifiers) {
            sendgrid.AddUniqueArgs(identifiers);
            return this;
        }

        public MailBuilder SetCategory(string category) {
            sendgrid.SetCategory(category);
            return this;
        }
        public MailBuilder SetCategories(IEnumerable<string> categories) {
            sendgrid.SetCategories(categories);
            return this;
        }

        public MailBuilder EnableGravatar() {
            sendgrid.EnableGravatar();
            return this;
        }
        public MailBuilder EnableOpenTracking() {
            sendgrid.EnableOpenTracking();
            return this;
        }
        public MailBuilder EnableClickTracking(bool includePlainText = false) {
            sendgrid.EnableClickTracking(includePlainText);
            return this;
        }
        public MailBuilder EnableSpamCheck(int score = 5, string url = null) {
            sendgrid.EnableSpamCheck(score, url);
            return this;
        }
        public MailBuilder EnableUnsubscribe(string text, string html) {
            sendgrid.EnableUnsubscribe(text, html);
            return this;
        }
        public MailBuilder EnableUnsubscribe(string replace) {
            sendgrid.EnableUnsubscribe(replace);
            return this;
        }
        public MailBuilder EnableFooter(string text = null, string html = null) {
            sendgrid.EnableFooter(text, html);
            return this;
        }
        public MailBuilder EnableGoogleAnalytics(string source, string medium, string term, string content = null, string campaign = null) {
            sendgrid.EnableGoogleAnalytics(source, medium, term, content, campaign);
            return this;
        }
        public MailBuilder EnableTemplate(string html) {
            sendgrid.EnableTemplate(html);
            return this;
        }
        public MailBuilder EnableBcc(string email) {
            sendgrid.EnableBcc(email);
            return this;
        }
        public MailBuilder EnableBypassListManagement() {
            sendgrid.EnableBypassListManagement();
            return this;
        }

        public MailBuilder DisableGravatar() {
            sendgrid.DisableGravatar();
            return this;
        }
        public MailBuilder DisableOpenTracking() {
            sendgrid.DisableOpenTracking();
            return this;
        }
        public MailBuilder DisableClickTracking() {
            sendgrid.DisableClickTracking();
            return this;
        }
        public MailBuilder DisableSpamCheck() {
            sendgrid.DisableSpamCheck();
            return this;
        }
        public MailBuilder DisableUnsubscribe() {
            sendgrid.DisableUnsubscribe();
            return this;
        }
        public MailBuilder DisableFooter() {
            sendgrid.DisableFooter();
            return this;
        }
        public MailBuilder DisableGoogleAnalytics() {
            sendgrid.DisableGoogleAnalytics();
            return this;
        }
        public MailBuilder DisableTemplate() {
            sendgrid.DisableTemplate();
            return this;
        }
        public MailBuilder DisableBcc() {
            sendgrid.DisableBcc();
            return this;
        }
        public MailBuilder DisableBypassListManagement() {
            sendgrid.DisableBypassListManagement();
            return this;
        }

        private string GetAlternateViewAsString(AlternateView view) {
            var dataStream = view.ContentStream;
            byte[] byteBuffer = new byte[dataStream.Length];
            var encoding = Encoding.GetEncoding(view.ContentType.CharSet);
            return encoding.GetString(byteBuffer, 0, dataStream.Read(byteBuffer, 0, byteBuffer.Length));
        }
        private static string EmailFormat(string displayName, string email) {
            return string.Format("{0} <{1}>", displayName, email);
        }
    }

    internal static class Extensions {
        public static IEnumerable<string> ToListString(this IEnumerable<MailAddress> addresses) {
            return addresses.ToList().ConvertAll<string>(address => string.Format("{0} <{1}>", address.DisplayName, address.Address));
        }

    }
}