summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--SendGrid/SendGridMail/MailBuilder.cs384
1 files changed, 188 insertions, 196 deletions
diff --git a/SendGrid/SendGridMail/MailBuilder.cs b/SendGrid/SendGridMail/MailBuilder.cs
index 3ab6211..1d3d611 100644
--- a/SendGrid/SendGridMail/MailBuilder.cs
+++ b/SendGrid/SendGridMail/MailBuilder.cs
@@ -7,36 +7,34 @@ using System.Net.Mail;
using System.Text;
namespace SendGridMail {
- public class MailBuilder {
+ public sealed class MailBuilder {
private SendGrid sendgrid;
- private bool? enableFlag;
+ private bool hideRecipients = false;
public static MailBuilder Create() {
var mailBuilder = new MailBuilder();
mailBuilder.sendgrid = SendGrid.GetInstance();
- return new MailBuilder();
+ return mailBuilder;
}
public SendGrid Build() {
+ var exceptions = new List<Exception>();
if (string.IsNullOrEmpty(sendgrid.Html) && string.IsNullOrEmpty(sendgrid.Text)) {
- throw new InvalidOperationException("Mail does not contain a body.");
+ 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 };
}
- 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 HideRecipients() {
+ hideRecipients = true;
+ return this;
}
public MailBuilder From(MailAddress address) {
@@ -51,6 +49,7 @@ namespace SendGridMail {
sendgrid.From = new MailAddress(email, displayName);
return this;
}
+
public MailBuilder To(MailAddress address) {
return this.To(address.Address, address.DisplayName);
}
@@ -59,9 +58,20 @@ namespace SendGridMail {
return this;
}
public MailBuilder To(string email, string displayName) {
- sendgrid.AddTo(string.Format("{0} <{1}>", displayName, email));
+ 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);
}
@@ -70,254 +80,236 @@ namespace SendGridMail {
return this;
}
public MailBuilder Cc(string email, string displayName) {
- sendgrid.AddCc(string.Format("{0} <{1}>", displayName, email));
+ 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(string.Format("{0} <{1}>", displayName, email));
+ 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))
+ .WithLinkedResources(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 WithSubstitution(string replacementTag, List<string> substitutionValues) {
- sendgrid.AddSubstitution(replacementTag, substitutionValues);
+
+ public MailBuilder WithAttachment(Stream stream, string name) {
+ sendgrid.AddAttachment(stream, name);
return this;
}
- public MailBuilder WithUniqueArgs(IDictionary<string, string> identifiers) {
- sendgrid.AddUniqueArgs(identifiers);
+ public MailBuilder WithAttachment(string filePath) {
+ sendgrid.AddAttachment(filePath);
return this;
}
- public MailBuilder WithCategory(string category) {
- sendgrid.SetCategory(category);
- return this;
+ public MailBuilder WithAttachment(Attachment attachment) {
+ return this.WithAttachment(attachment.ContentStream, attachment.Name);
}
- public MailBuilder WithCategories(IEnumerable<string> categories) {
- sendgrid.SetCategories(categories);
+ public MailBuilder WithAttachments(IEnumerable<Attachment> attachments) {
+ foreach (var attachment in attachments) { this.WithAttachment(attachment); }
return this;
}
- public MailBuilder WithAttachment(Stream stream, string name) {
+ public MailBuilder WithAttachments(AttachmentCollection attachments) {
+ return this.WithAttachments(attachments.ToList());
+ }
+
+ public MailBuilder WithLinkedResource(Stream stream, string name) {
sendgrid.AddAttachment(stream, name);
return this;
}
- public MailBuilder WithAttachment(string filePath) {
+ public MailBuilder WithLinkedResource(string filePath) {
sendgrid.AddAttachment(filePath);
return this;
}
+ public MailBuilder WithLinkedResource(LinkedResource resource) {
+ return this.WithAttachment(resource.ContentStream, resource.ContentId);
+ }
+ public MailBuilder WithLinkedResources(IEnumerable<LinkedResource> resources) {
+ foreach (var resource in resources) { this.WithLinkedResource(resource); }
+ return this;
+ }
+ public MailBuilder WithLinkedResources(LinkedResourceCollection resources) {
+ return this.WithLinkedResources(resources.ToList());
+ }
+
+ public MailBuilder WithHeader(string key, string value) {
+ sendgrid.AddHeaders(new Dictionary<string, string> { { key, value } });
+ 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;
+ public MailBuilder WithSubstitution(string replacementTag, IEnumerable<string> substitutionValues) {
+ sendgrid.AddSubstitution(replacementTag, substitutionValues.ToList());
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;
+ public MailBuilder WithUniqueArg(string key, string value) {
+ sendgrid.AddUniqueArgs(new Dictionary<string, string> { { key, value } });
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;
+ public MailBuilder WithUniqueArgs(IDictionary<string, string> identifiers) {
+ sendgrid.AddUniqueArgs(identifiers);
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;
+ public MailBuilder SetCategory(string category) {
+ sendgrid.SetCategory(category);
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;
+ public MailBuilder SetCategories(IEnumerable<string> categories) {
+ sendgrid.SetCategories(categories);
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;
+ public MailBuilder EnableGravatar() {
+ sendgrid.EnableGravatar();
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;
+ public MailBuilder EnableOpenTracking() {
+ sendgrid.EnableOpenTracking();
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;
+ public MailBuilder EnableClickTracking(bool includePlainText = false) {
+ sendgrid.EnableClickTracking(includePlainText);
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;
+ public MailBuilder EnableSpamCheck(int score = 5, string url = null) {
+ sendgrid.EnableSpamCheck(score, url);
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;
+ public MailBuilder EnableUnsubscribe(string text, string html) {
+ sendgrid.EnableUnsubscribe(text, html);
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;
+ public MailBuilder EnableUnsubscribe(string replace) {
+ sendgrid.EnableUnsubscribe(replace);
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;
+ public MailBuilder EnableFooter(string text = null, string html = null) {
+ sendgrid.EnableFooter(text, html);
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;
+ 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;
}
- /// <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;
+ 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));
+ }
}
}