diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/Example/Program.cs | 32 | ||||
-rw-r--r-- | src/SendGrid/Client.cs | 24 | ||||
-rw-r--r-- | src/SendGrid/Helpers/Mail/Mail.cs | 47 | ||||
-rw-r--r-- | src/SendGrid/Helpers/Mail/MailHelper.cs | 33 | ||||
-rw-r--r-- | src/SendGrid/Helpers/Mail/Model/ASM.cs (renamed from src/SendGrid/Helpers/Mail/ASM.cs) | 0 | ||||
-rw-r--r-- | src/SendGrid/Helpers/Mail/Model/Attachment.cs (renamed from src/SendGrid/Helpers/Mail/Attachment.cs) | 0 | ||||
-rw-r--r-- | src/SendGrid/Helpers/Mail/Model/BCCSettings.cs (renamed from src/SendGrid/Helpers/Mail/BCCSettings.cs) | 0 | ||||
-rw-r--r-- | src/SendGrid/Helpers/Mail/Model/BypassListManagement.cs (renamed from src/SendGrid/Helpers/Mail/BypassListManagement.cs) | 0 | ||||
-rw-r--r-- | src/SendGrid/Helpers/Mail/Model/ClickTracking.cs (renamed from src/SendGrid/Helpers/Mail/ClickTracking.cs) | 0 | ||||
-rw-r--r-- | src/SendGrid/Helpers/Mail/Model/Content.cs (renamed from src/SendGrid/Helpers/Mail/Content.cs) | 18 | ||||
-rw-r--r-- | src/SendGrid/Helpers/Mail/Model/EmailAddress.cs (renamed from src/SendGrid/Helpers/Mail/MailAddress.cs) | 10 | ||||
-rw-r--r-- | src/SendGrid/Helpers/Mail/Model/FooterSettings.cs (renamed from src/SendGrid/Helpers/Mail/FooterSettings.cs) | 0 | ||||
-rw-r--r-- | src/SendGrid/Helpers/Mail/Model/Ganalytics.cs (renamed from src/SendGrid/Helpers/Mail/Ganalytics.cs) | 0 | ||||
-rw-r--r-- | src/SendGrid/Helpers/Mail/Model/MailSettings.cs (renamed from src/SendGrid/Helpers/Mail/MailSettings.cs) | 0 | ||||
-rw-r--r-- | src/SendGrid/Helpers/Mail/Model/OpenTracking.cs (renamed from src/SendGrid/Helpers/Mail/OpenTracking.cs) | 0 | ||||
-rw-r--r-- | src/SendGrid/Helpers/Mail/Model/Personalization.cs | 32 | ||||
-rw-r--r-- | src/SendGrid/Helpers/Mail/Model/SandboxMode.cs (renamed from src/SendGrid/Helpers/Mail/SandboxMode.cs) | 0 | ||||
-rw-r--r-- | src/SendGrid/Helpers/Mail/Model/SpamCheck.cs (renamed from src/SendGrid/Helpers/Mail/SpamCheck.cs) | 0 | ||||
-rw-r--r-- | src/SendGrid/Helpers/Mail/Model/SubscriptionTracking.cs (renamed from src/SendGrid/Helpers/Mail/SubscriptionTracking.cs) | 0 | ||||
-rw-r--r-- | src/SendGrid/Helpers/Mail/Model/TrackingSettings.cs (renamed from src/SendGrid/Helpers/Mail/TrackingSettings.cs) | 0 | ||||
-rw-r--r-- | src/SendGrid/Helpers/Mail/Personalization.cs | 80 | ||||
-rw-r--r-- | src/SendGrid/Helpers/Mail/SendGridMessage.cs | 88 |
22 files changed, 137 insertions, 227 deletions
diff --git a/src/Example/Program.cs b/src/Example/Program.cs index ab7b4ec..08c3b54 100644 --- a/src/Example/Program.cs +++ b/src/Example/Program.cs @@ -19,14 +19,38 @@ namespace Example string apiKey = Environment.GetEnvironmentVariable("SENDGRID_APIKEY"); Client client = new Client(apiKey); + // Generic Hello World Send using the Mail Helper + var msg = new SendGridMessage() + { + From = new EmailAddress("dx@sendgrid.com", "DX Team"), + Personalization = new List<Personalization>() { + new Personalization() { + Tos = new List<EmailAddress>() { + new EmailAddress("elmer.thomas@sendgrid.com", "Elmer Thomas") + } + } + }, + Subject = "Hello World from the SendGrid CSharp Library Helper!", + Contents = new List<Content>() { + new PlainTextContent("Hello, Email from the helper [SendEmailAsync]!"), + new HtmlContent("<strong>Hello, Email from the helper! [SendEmailAsync]</strong>") + } + }; + + var response = await client.SendEmailAsync(msg); + Console.WriteLine(response.StatusCode); + Console.WriteLine(response.Headers); + Console.ReadLine(); + // Send a Single Email using the Mail Helper - var from = new MailAddress("dx@sendgrid", "DX Team"); + var from = new EmailAddress("dx@sendgrid", "DX Team"); var subject = "Hello World from the SendGrid CSharp Library Helper!"; - var to = new MailAddress("elmer@sendgrid.com", "Elmer Thomas"); + var to = new EmailAddress("elmer@sendgrid.com", "Elmer Thomas"); var contentText = "Hello, Email from the helper [SendSingleEmailAsync]!"; - var contentHTML = "<strong>Hello, Email from the helper! [SendSingleEmailAsync]</strong>"; + var contentHtml = "<strong>Hello, Email from the helper! [SendSingleEmailAsync]</strong>"; + msg = MailHelper.CreateSingleEmail(from, to, subject , contentText, contentHtml); - Response response = await client.Mail.SendSingleEmailAsync(from, to, subject , contentText, contentHTML); + response = await client.SendEmailAsync(msg); Console.WriteLine(response.StatusCode); Console.WriteLine(response.Headers); Console.ReadLine(); diff --git a/src/SendGrid/Client.cs b/src/SendGrid/Client.cs index 4eabcea..3528fef 100644 --- a/src/SendGrid/Client.cs +++ b/src/SendGrid/Client.cs @@ -8,6 +8,7 @@ using System.Net.Http; using System.Net.Http.Headers; using System.Reflection; using System.Text; +using System.Threading; using System.Threading.Tasks; namespace SendGrid @@ -60,7 +61,7 @@ namespace SendGrid public static class MimeType { - public static readonly string HTML = "text/html"; + public static readonly string Html = "text/html"; public static readonly string Text = "text/plain"; } @@ -73,7 +74,6 @@ namespace SendGrid public string MediaType; public IWebProxy WebProxy; public enum Method { DELETE, GET, PATCH, POST, PUT } - public Mail Mail { get; set; } /// <summary> /// REST API client. @@ -113,7 +113,6 @@ namespace SendGrid } SetVersion(version); SetUrlPath(urlPath); - Mail = new Mail(this); } /// <summary> @@ -222,7 +221,7 @@ namespace SendGrid /// <summary> /// Get the version of the API, override to customize /// </summary> - /// <returns>Version of the API</param> + /// <returns>Version of the API</returns> public string GetVersion() { return Version; @@ -234,9 +233,9 @@ namespace SendGrid /// <param name="client">Client object ready for communication with API</param> /// <param name="request">The parameters for the API call</param> /// <returns>Response object</returns> - public async Task<Response> MakeRequest(HttpClient client, HttpRequestMessage request) + public async Task<Response> MakeRequest(HttpClient client, HttpRequestMessage request, CancellationToken cancellationToken = default(CancellationToken)) { - HttpResponseMessage response = await client.SendAsync(request).ConfigureAwait(false); + HttpResponseMessage response = await client.SendAsync(request, cancellationToken).ConfigureAwait(false); return new Response(response.StatusCode, response.Content, response.Headers); } @@ -251,7 +250,8 @@ namespace SendGrid string requestBody = null, Dictionary<string, string> requestHeaders = null, string queryParams = null, - string urlPath = null) + string urlPath = null, + CancellationToken cancellationToken = default(CancellationToken)) { using (var client = BuildHttpClient()) { @@ -305,7 +305,7 @@ namespace SendGrid RequestUri = new Uri(endpoint), Content = content }; - return await MakeRequest(client, request).ConfigureAwait(false); + return await MakeRequest(client, request, cancellationToken).ConfigureAwait(false); } catch (Exception ex) @@ -319,5 +319,13 @@ namespace SendGrid } } } + + public async Task<Response> SendEmailAsync(SendGridMessage msg, CancellationToken cancellationToken = default(CancellationToken)) + { + return await RequestAsync(Client.Method.POST, + msg.Serialize(), + urlPath: "mail/send", + cancellationToken: cancellationToken).ConfigureAwait(false); + } } } diff --git a/src/SendGrid/Helpers/Mail/Mail.cs b/src/SendGrid/Helpers/Mail/Mail.cs deleted file mode 100644 index d9ad829..0000000 --- a/src/SendGrid/Helpers/Mail/Mail.cs +++ /dev/null @@ -1,47 +0,0 @@ -using Newtonsoft.Json; -using System.Collections.Generic; -using System.Threading.Tasks; - -namespace SendGrid.Helpers.Mail -{ - - public class Mail - { - private Client _client; - - public Mail(Client client) - { - _client = client; - } - - public async Task<Response> SendEmailAsync(SendGridMessage msg) - { - return await _client.RequestAsync(Client.Method.POST, - msg.Get(), - urlPath: "mail/send").ConfigureAwait(false); - } - - public async Task<Response> SendSingleEmailAsync(MailAddress from, - MailAddress to, - string subject, - string contentText, - string contentHTML, - SendGridMessage msg = null) - { - if(msg == null) - { - msg = new SendGridMessage(); - } - msg.From = from; - msg.Subject = subject; - var text = new Content(MimeType.Text, contentText); - msg.AddContent(text); - var html = new Content(MimeType.HTML, contentHTML); - msg.AddContent(html); - var p = new Personalization(); - p.AddTo(to); - msg.AddPersonalization(p); - return await SendEmailAsync(msg).ConfigureAwait(false); - } - } -} diff --git a/src/SendGrid/Helpers/Mail/MailHelper.cs b/src/SendGrid/Helpers/Mail/MailHelper.cs new file mode 100644 index 0000000..f151c1f --- /dev/null +++ b/src/SendGrid/Helpers/Mail/MailHelper.cs @@ -0,0 +1,33 @@ +using System.Collections.Generic; + +namespace SendGrid.Helpers.Mail +{ + + public class MailHelper + { + public static SendGridMessage CreateSingleEmail(EmailAddress from, + EmailAddress to, + string subject, + string contentText, + string contentHtml) + { + var msg = new SendGridMessage() + { + From = from, + Personalization = new List<Personalization>() { + new Personalization() { + Tos = new List<EmailAddress>() { + to + } + } + }, + Subject = subject, + Contents = new List<Content>() { + new PlainTextContent(contentText), + new HtmlContent(contentHtml) + } + }; + return msg; + } + } +}
\ No newline at end of file diff --git a/src/SendGrid/Helpers/Mail/ASM.cs b/src/SendGrid/Helpers/Mail/Model/ASM.cs index f300780..f300780 100644 --- a/src/SendGrid/Helpers/Mail/ASM.cs +++ b/src/SendGrid/Helpers/Mail/Model/ASM.cs diff --git a/src/SendGrid/Helpers/Mail/Attachment.cs b/src/SendGrid/Helpers/Mail/Model/Attachment.cs index cc2bfd6..cc2bfd6 100644 --- a/src/SendGrid/Helpers/Mail/Attachment.cs +++ b/src/SendGrid/Helpers/Mail/Model/Attachment.cs diff --git a/src/SendGrid/Helpers/Mail/BCCSettings.cs b/src/SendGrid/Helpers/Mail/Model/BCCSettings.cs index 79f989e..79f989e 100644 --- a/src/SendGrid/Helpers/Mail/BCCSettings.cs +++ b/src/SendGrid/Helpers/Mail/Model/BCCSettings.cs diff --git a/src/SendGrid/Helpers/Mail/BypassListManagement.cs b/src/SendGrid/Helpers/Mail/Model/BypassListManagement.cs index cb4cfed..cb4cfed 100644 --- a/src/SendGrid/Helpers/Mail/BypassListManagement.cs +++ b/src/SendGrid/Helpers/Mail/Model/BypassListManagement.cs diff --git a/src/SendGrid/Helpers/Mail/ClickTracking.cs b/src/SendGrid/Helpers/Mail/Model/ClickTracking.cs index b8f82f5..b8f82f5 100644 --- a/src/SendGrid/Helpers/Mail/ClickTracking.cs +++ b/src/SendGrid/Helpers/Mail/Model/ClickTracking.cs diff --git a/src/SendGrid/Helpers/Mail/Content.cs b/src/SendGrid/Helpers/Mail/Model/Content.cs index 68b01d3..0f05156 100644 --- a/src/SendGrid/Helpers/Mail/Content.cs +++ b/src/SendGrid/Helpers/Mail/Model/Content.cs @@ -20,4 +20,22 @@ namespace SendGrid.Helpers.Mail [JsonProperty(PropertyName = "value")] public string Value { get; set; } } + + public class PlainTextContent : Content + { + public PlainTextContent(string value) + { + this.Type = MimeType.Text; + this.Value = value; + } + } + + public class HtmlContent : Content + { + public HtmlContent(string value) + { + this.Type = MimeType.Html; + this.Value = value; + } + } }
\ No newline at end of file diff --git a/src/SendGrid/Helpers/Mail/MailAddress.cs b/src/SendGrid/Helpers/Mail/Model/EmailAddress.cs index b3b3f18..e1ba319 100644 --- a/src/SendGrid/Helpers/Mail/MailAddress.cs +++ b/src/SendGrid/Helpers/Mail/Model/EmailAddress.cs @@ -2,15 +2,15 @@ namespace SendGrid.Helpers.Mail { - public class MailAddress + public class EmailAddress { - public MailAddress() + public EmailAddress() { } - public MailAddress(string email, string name = null) + public EmailAddress(string email, string name = null) { - this.Address = email; + this.Email = email; this.Name = name; } @@ -18,6 +18,6 @@ namespace SendGrid.Helpers.Mail public string Name { get; set; } [JsonProperty(PropertyName = "email")] - public string Address { get; set; } + public string Email { get; set; } } }
\ No newline at end of file diff --git a/src/SendGrid/Helpers/Mail/FooterSettings.cs b/src/SendGrid/Helpers/Mail/Model/FooterSettings.cs index 5ef64f7..5ef64f7 100644 --- a/src/SendGrid/Helpers/Mail/FooterSettings.cs +++ b/src/SendGrid/Helpers/Mail/Model/FooterSettings.cs diff --git a/src/SendGrid/Helpers/Mail/Ganalytics.cs b/src/SendGrid/Helpers/Mail/Model/Ganalytics.cs index 071da50..071da50 100644 --- a/src/SendGrid/Helpers/Mail/Ganalytics.cs +++ b/src/SendGrid/Helpers/Mail/Model/Ganalytics.cs diff --git a/src/SendGrid/Helpers/Mail/MailSettings.cs b/src/SendGrid/Helpers/Mail/Model/MailSettings.cs index 1875603..1875603 100644 --- a/src/SendGrid/Helpers/Mail/MailSettings.cs +++ b/src/SendGrid/Helpers/Mail/Model/MailSettings.cs diff --git a/src/SendGrid/Helpers/Mail/OpenTracking.cs b/src/SendGrid/Helpers/Mail/Model/OpenTracking.cs index b171a0b..b171a0b 100644 --- a/src/SendGrid/Helpers/Mail/OpenTracking.cs +++ b/src/SendGrid/Helpers/Mail/Model/OpenTracking.cs diff --git a/src/SendGrid/Helpers/Mail/Model/Personalization.cs b/src/SendGrid/Helpers/Mail/Model/Personalization.cs new file mode 100644 index 0000000..54a3506 --- /dev/null +++ b/src/SendGrid/Helpers/Mail/Model/Personalization.cs @@ -0,0 +1,32 @@ +using System.Collections.Generic; +using Newtonsoft.Json; + +namespace SendGrid.Helpers.Mail +{ + public class Personalization + { + [JsonProperty(PropertyName = "to")] + public List<EmailAddress> Tos { get; set; } + + [JsonProperty(PropertyName = "cc")] + public List<EmailAddress> Ccs { get; set; } + + [JsonProperty(PropertyName = "bcc")] + public List<EmailAddress> Bccs { get; set; } + + [JsonProperty(PropertyName = "subject")] + public string Subject { get; set; } + + [JsonProperty(PropertyName = "headers")] + public Dictionary<string, string> Headers { get; set; } + + [JsonProperty(PropertyName = "substitutions")] + public Dictionary<string, string> Substitutions { get; set; } + + [JsonProperty(PropertyName = "custom_args")] + public Dictionary<string, string> CustomArgs { get; set; } + + [JsonProperty(PropertyName = "send_at")] + public long? SendAt { get; set; } + } +}
\ No newline at end of file diff --git a/src/SendGrid/Helpers/Mail/SandboxMode.cs b/src/SendGrid/Helpers/Mail/Model/SandboxMode.cs index 7dea93e..7dea93e 100644 --- a/src/SendGrid/Helpers/Mail/SandboxMode.cs +++ b/src/SendGrid/Helpers/Mail/Model/SandboxMode.cs diff --git a/src/SendGrid/Helpers/Mail/SpamCheck.cs b/src/SendGrid/Helpers/Mail/Model/SpamCheck.cs index 8b1abf4..8b1abf4 100644 --- a/src/SendGrid/Helpers/Mail/SpamCheck.cs +++ b/src/SendGrid/Helpers/Mail/Model/SpamCheck.cs diff --git a/src/SendGrid/Helpers/Mail/SubscriptionTracking.cs b/src/SendGrid/Helpers/Mail/Model/SubscriptionTracking.cs index ce5704e..ce5704e 100644 --- a/src/SendGrid/Helpers/Mail/SubscriptionTracking.cs +++ b/src/SendGrid/Helpers/Mail/Model/SubscriptionTracking.cs diff --git a/src/SendGrid/Helpers/Mail/TrackingSettings.cs b/src/SendGrid/Helpers/Mail/Model/TrackingSettings.cs index 73f8056..73f8056 100644 --- a/src/SendGrid/Helpers/Mail/TrackingSettings.cs +++ b/src/SendGrid/Helpers/Mail/Model/TrackingSettings.cs diff --git a/src/SendGrid/Helpers/Mail/Personalization.cs b/src/SendGrid/Helpers/Mail/Personalization.cs deleted file mode 100644 index a728c2c..0000000 --- a/src/SendGrid/Helpers/Mail/Personalization.cs +++ /dev/null @@ -1,80 +0,0 @@ -using System.Collections.Generic; -using Newtonsoft.Json; - -namespace SendGrid.Helpers.Mail -{ - public class Personalization - { - [JsonProperty(PropertyName = "to")] - public List<MailAddress> Tos { get; set; } - - [JsonProperty(PropertyName = "cc")] - public List<MailAddress> Ccs { get; set; } - - [JsonProperty(PropertyName = "bcc")] - public List<MailAddress> Bccs { get; set; } - - [JsonProperty(PropertyName = "subject")] - public string Subject { get; set; } - - [JsonProperty(PropertyName = "headers")] - public Dictionary<string, string> Headers { get; set; } - - [JsonProperty(PropertyName = "substitutions")] - public Dictionary<string, string> Substitutions { get; set; } - - [JsonProperty(PropertyName = "custom_args")] - public Dictionary<string, string> CustomArgs { get; set; } - - [JsonProperty(PropertyName = "send_at")] - public long? SendAt { get; set; } - - public void AddTo(MailAddress email) - { - if (Tos == null) - Tos = new List<MailAddress>(); - - Tos.Add(email); - } - - public void AddCc(MailAddress email) - { - if (Ccs == null) - Ccs = new List<MailAddress>(); - - Ccs.Add(email); - } - - public void AddBcc(MailAddress email) - { - if (Bccs == null) - Bccs = new List<MailAddress>(); - - Bccs.Add(email); - } - - public void AddHeader(string key, string value) - { - if (Headers == null) - Headers = new Dictionary<string, string>(); - - Headers.Add(key, value); - } - - public void AddSubstitution(string key, string value) - { - if (Substitutions == null) - Substitutions = new Dictionary<string, string>(); - - Substitutions.Add(key, value); - } - - public void AddCustomArgs(string key, string value) - { - if (CustomArgs == null) - CustomArgs = new Dictionary<string, string>(); - - CustomArgs.Add(key, value); - } - } -}
\ No newline at end of file diff --git a/src/SendGrid/Helpers/Mail/SendGridMessage.cs b/src/SendGrid/Helpers/Mail/SendGridMessage.cs index 97f7146..2bde42a 100644 --- a/src/SendGrid/Helpers/Mail/SendGridMessage.cs +++ b/src/SendGrid/Helpers/Mail/SendGridMessage.cs @@ -1,4 +1,5 @@ -using Newtonsoft.Json; +using System; +using Newtonsoft.Json; using System.Collections.Generic; namespace SendGrid.Helpers.Mail @@ -8,24 +9,8 @@ namespace SendGrid.Helpers.Mail /// </summary> public class SendGridMessage { - public SendGridMessage() - { - } - - public SendGridMessage(MailAddress from, string subject, MailAddress to, Content content) - { - this.From = from; - - var personalization = new Personalization(); - personalization.AddTo(to); - this.AddPersonalization(personalization); - - this.Subject = subject; - this.AddContent(content); - } - [JsonProperty(PropertyName = "from")] - public MailAddress From { get; set; } + public EmailAddress From { get; set; } [JsonProperty(PropertyName = "subject")] public string Subject { get; set; } @@ -73,72 +58,9 @@ namespace SendGrid.Helpers.Mail public TrackingSettings TrackingSettings { get; set; } [JsonProperty(PropertyName = "reply_to")] - public MailAddress ReplyTo { get; set; } - - public void AddPersonalization(Personalization personalization) - { - if (Personalization == null) - { - Personalization = new List<Personalization>(); - } - Personalization.Add(personalization); - } - - public void AddContent(Content content) - { - if (Contents == null) - { - Contents = new List<Content>(); - } - Contents.Add(content); - } - - public void AddAttachment(Attachment attachment) - { - if (Attachments == null) - { - Attachments = new List<Attachment>(); - } - Attachments.Add(attachment); - } - - public void AddHeader(string key, string value) - { - if (Headers == null) - { - Headers = new Dictionary<string, string>(); - } - Headers.Add(key, value); - } - - public void AddSection(string key, string value) - { - if (Sections == null) - { - Sections = new Dictionary<string, string>(); - } - Sections.Add(key, value); - } - - public void AddCategory(string category) - { - if (Categories == null) - { - Categories = new List<string>(); - } - Categories.Add(category); - } - - public void AddCustomArgs(string key, string value) - { - if (CustomArgs == null) - { - CustomArgs = new Dictionary<string, string>(); - } - CustomArgs.Add(key, value); - } + public EmailAddress ReplyTo { get; set; } - public string Get() + public string Serialize() { return JsonConvert.SerializeObject(this, Formatting.None, |