summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorElmer Thomas <elmer@thinkingserious.com>2016-12-09 08:58:08 -0800
committerElmer Thomas <elmer@thinkingserious.com>2016-12-09 08:58:08 -0800
commit85ee9ec77ecaa924904a8800eced145565b70073 (patch)
tree9f6f3511d27dfa0ee517782fed98a41904c5650f
parent61a3becac71cabcf9e93a16f9f0df69fee871ac1 (diff)
downloadsendgrid-csharp-85ee9ec77ecaa924904a8800eced145565b70073.zip
sendgrid-csharp-85ee9ec77ecaa924904a8800eced145565b70073.tar.gz
sendgrid-csharp-85ee9ec77ecaa924904a8800eced145565b70073.tar.bz2
Begin of Mail Helper Update
-rw-r--r--src/Example/Program.cs43
-rw-r--r--src/SendGrid/Client.cs9
-rw-r--r--src/SendGrid/Helpers/Mail/Mail.cs161
-rw-r--r--src/SendGrid/Helpers/Mail/MailAddress.cs (renamed from src/SendGrid/Helpers/Mail/Email.cs)6
-rw-r--r--src/SendGrid/Helpers/Mail/Personalization.cs18
-rw-r--r--src/SendGrid/Helpers/Mail/SendGridMessage.cs153
6 files changed, 221 insertions, 169 deletions
diff --git a/src/Example/Program.cs b/src/Example/Program.cs
index 572ce7a..67a5709 100644
--- a/src/Example/Program.cs
+++ b/src/Example/Program.cs
@@ -19,6 +19,33 @@ namespace Example
string apiKey = Environment.GetEnvironmentVariable("SENDGRID_APIKEY");
Client client = new Client(apiKey);
+ /* Send a Email using the Mail Helper
+ var msg = new SendGridMessage();
+ var from = new MailAddress("dx@sendgrid");
+ var subject = "Hello World from the SendGrid CSharp Library Helper!";
+ var to = new MailAddress("elmer@sendgrid.com");
+ var content = new Content("text/plain", "Hello, Email from the helper!");
+ Response response = await client.SendEmailAsync(msg);
+ Console.WriteLine(response.StatusCode);
+ Console.WriteLine(response.Body.ReadAsStringAsync().Result);
+ Console.WriteLine(response.Headers);
+ Console.ReadLine();
+ */
+
+ // Send a Single Email using the Mail Helper
+ var from = new MailAddress("dx@sendgrid", "DX Team");
+ var subject = "Hello World from the SendGrid CSharp Library Helper!";
+ var to = new MailAddress("elmer@sendgrid.com", "Elmer Thomas");
+ var contentText = "Hello, Email from the helper [SendSingleEmailAsync]!";
+ var contentHTML = "<strong>Hello, Email from the helper! [SendSingleEmailAsync]</strong>";
+
+ Response response = await client.Mail.SendSingleEmailAsync(from, to, subject , contentText, contentHTML);
+ Console.WriteLine(response.StatusCode);
+ Console.WriteLine(response.Body.ReadAsStringAsync().Result);
+ Console.WriteLine(response.Headers);
+ Console.ReadLine();
+
+ // Without the Mail Helper
string data = @"{
'personalizations': [
{
@@ -41,22 +68,8 @@ namespace Example
]
}";
Object json = JsonConvert.DeserializeObject<Object>(data);
- Response response = await client.RequestAsync(Client.Methods.POST,
- json.ToString(),
- urlPath: "mail/send");
- Console.WriteLine(response.StatusCode);
- Console.WriteLine(response.Body.ReadAsStringAsync().Result);
- Console.WriteLine(response.Headers);
- Console.ReadLine();
-
- Email from = new Email("dx@sendgrid");
- string subject = "Hello World from the SendGrid CSharp Library Helper!";
- Email to = new Email("elmer@sendgrid.com");
- Content content = new Content("text/plain", "Hello, Email from the helper!");
- Mail mail = new Mail(from, subject, to, content);
-
response = await client.RequestAsync(Client.Methods.POST,
- mail.Get(),
+ json.ToString(),
urlPath: "mail/send");
Console.WriteLine(response.StatusCode);
Console.WriteLine(response.Body.ReadAsStringAsync().Result);
diff --git a/src/SendGrid/Client.cs b/src/SendGrid/Client.cs
index 5711a52..7beeeb2 100644
--- a/src/SendGrid/Client.cs
+++ b/src/SendGrid/Client.cs
@@ -1,4 +1,5 @@
using Newtonsoft.Json;
+using SendGrid.Helpers.Mail;
using System;
using System.Collections.Generic;
using System.Linq;
@@ -65,11 +66,8 @@ namespace SendGrid
public string UrlPath;
public string MediaType;
public IWebProxy WebProxy;
- public enum Methods
- {
- DELETE, GET, PATCH, POST, PUT
- }
-
+ public enum Methods { DELETE, GET, PATCH, POST, PUT }
+ public Mail Mail { get; set; }
/// <summary>
/// REST API client.
@@ -109,6 +107,7 @@ namespace SendGrid
}
SetVersion(version);
SetUrlPath(urlPath);
+ Mail = new Mail(this);
}
/// <summary>
diff --git a/src/SendGrid/Helpers/Mail/Mail.cs b/src/SendGrid/Helpers/Mail/Mail.cs
index 2aa1274..f505ec0 100644
--- a/src/SendGrid/Helpers/Mail/Mail.cs
+++ b/src/SendGrid/Helpers/Mail/Mail.cs
@@ -1,153 +1,40 @@
using Newtonsoft.Json;
using System.Collections.Generic;
+using System.Threading.Tasks;
namespace SendGrid.Helpers.Mail
{
- /// <summary>
- /// Class Mail builds an object that sends an email through SendGrid.
- /// </summary>
+
public class Mail
{
- public Mail()
- {
- }
-
- public Mail(Email from, string subject, Email 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 Email From { get; set; }
-
- [JsonProperty(PropertyName = "subject")]
- public string Subject { get; set; }
-
- [JsonProperty(PropertyName = "personalizations")]
- public List<Personalization> Personalization { get; set; }
-
- [JsonProperty(PropertyName = "content")]
- public List<Content> Contents { get; set; }
-
- [JsonProperty(PropertyName = "attachments")]
- public List<Attachment> Attachments { get; set; }
-
- [JsonProperty(PropertyName = "template_id")]
- public string TemplateId { get; set; }
+ private Client _client;
- [JsonProperty(PropertyName = "headers")]
- public Dictionary<string, string> Headers { get; set; }
-
- [JsonProperty(PropertyName = "sections")]
- public Dictionary<string, string> Sections { get; set; }
-
- [JsonProperty(PropertyName = "categories")]
- public List<string> Categories { get; set; }
-
- [JsonProperty(PropertyName = "custom_args")]
- public Dictionary<string, string> CustomArgs { get; set; }
-
- [JsonProperty(PropertyName = "send_at")]
- public long? SendAt { get; set; }
-
- [JsonProperty(PropertyName = "asm")]
- public ASM Asm { get; set; }
-
- [JsonProperty(PropertyName = "batch_id")]
- public string BatchId { get; set; }
-
- [JsonProperty(PropertyName = "ip_pool_name")]
- public string SetIpPoolId { get; set; }
-
- [JsonProperty(PropertyName = "mail_settings")]
- public MailSettings MailSettings { get; set; }
-
- [JsonProperty(PropertyName = "tracking_settings")]
- public TrackingSettings TrackingSettings { get; set; }
-
- [JsonProperty(PropertyName = "reply_to")]
- public Email ReplyTo { get; set; }
-
- public void AddPersonalization(Personalization personalization)
+ public Mail(Client client)
{
- if (Personalization == null)
- {
- Personalization = new List<Personalization>();
- }
- Personalization.Add(personalization);
+ _client = client;
}
- public void AddContent(Content content)
+ public async Task<Response> SendSingleEmailAsync(MailAddress from,
+ MailAddress to,
+ string subject,
+ string contentText,
+ string contentHTML,
+ SendGridMessage msg = null)
{
- if (Contents == null)
+ if(msg == null)
{
- Contents = new List<Content>();
+ msg = new SendGridMessage();
}
- 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 string Get()
- {
- return JsonConvert.SerializeObject(this,
- Formatting.None,
- new JsonSerializerSettings
- {
- NullValueHandling = NullValueHandling.Ignore,
- DefaultValueHandling = DefaultValueHandling.Include,
- StringEscapeHandling = StringEscapeHandling.EscapeHtml
- });
+ msg.From = from;
+ msg.Subject = subject;
+ var text = new Content("text/plain", contentText);
+ msg.AddContent(text);
+ var html = new Content("text/html", contentHTML);
+ msg.AddContent(html);
+ var p = new Personalization();
+ p.AddTo(to);
+ msg.AddPersonalization(p);
+ return await _client.RequestAsync(Client.Methods.POST, msg.Get(),urlPath: "mail/send").ConfigureAwait(false);
}
}
-} \ No newline at end of file
+}
diff --git a/src/SendGrid/Helpers/Mail/Email.cs b/src/SendGrid/Helpers/Mail/MailAddress.cs
index b4b4402..b3b3f18 100644
--- a/src/SendGrid/Helpers/Mail/Email.cs
+++ b/src/SendGrid/Helpers/Mail/MailAddress.cs
@@ -2,13 +2,13 @@
namespace SendGrid.Helpers.Mail
{
- public class Email
+ public class MailAddress
{
- public Email()
+ public MailAddress()
{
}
- public Email(string email, string name = null)
+ public MailAddress(string email, string name = null)
{
this.Address = email;
this.Name = name;
diff --git a/src/SendGrid/Helpers/Mail/Personalization.cs b/src/SendGrid/Helpers/Mail/Personalization.cs
index 47d0832..a728c2c 100644
--- a/src/SendGrid/Helpers/Mail/Personalization.cs
+++ b/src/SendGrid/Helpers/Mail/Personalization.cs
@@ -6,13 +6,13 @@ namespace SendGrid.Helpers.Mail
public class Personalization
{
[JsonProperty(PropertyName = "to")]
- public List<Email> Tos { get; set; }
+ public List<MailAddress> Tos { get; set; }
[JsonProperty(PropertyName = "cc")]
- public List<Email> Ccs { get; set; }
+ public List<MailAddress> Ccs { get; set; }
[JsonProperty(PropertyName = "bcc")]
- public List<Email> Bccs { get; set; }
+ public List<MailAddress> Bccs { get; set; }
[JsonProperty(PropertyName = "subject")]
public string Subject { get; set; }
@@ -29,26 +29,26 @@ namespace SendGrid.Helpers.Mail
[JsonProperty(PropertyName = "send_at")]
public long? SendAt { get; set; }
- public void AddTo(Email email)
+ public void AddTo(MailAddress email)
{
if (Tos == null)
- Tos = new List<Email>();
+ Tos = new List<MailAddress>();
Tos.Add(email);
}
- public void AddCc(Email email)
+ public void AddCc(MailAddress email)
{
if (Ccs == null)
- Ccs = new List<Email>();
+ Ccs = new List<MailAddress>();
Ccs.Add(email);
}
- public void AddBcc(Email email)
+ public void AddBcc(MailAddress email)
{
if (Bccs == null)
- Bccs = new List<Email>();
+ Bccs = new List<MailAddress>();
Bccs.Add(email);
}
diff --git a/src/SendGrid/Helpers/Mail/SendGridMessage.cs b/src/SendGrid/Helpers/Mail/SendGridMessage.cs
new file mode 100644
index 0000000..97f7146
--- /dev/null
+++ b/src/SendGrid/Helpers/Mail/SendGridMessage.cs
@@ -0,0 +1,153 @@
+using Newtonsoft.Json;
+using System.Collections.Generic;
+
+namespace SendGrid.Helpers.Mail
+{
+ /// <summary>
+ /// Class Mail builds an object that sends an email through SendGrid.
+ /// </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; }
+
+ [JsonProperty(PropertyName = "subject")]
+ public string Subject { get; set; }
+
+ [JsonProperty(PropertyName = "personalizations")]
+ public List<Personalization> Personalization { get; set; }
+
+ [JsonProperty(PropertyName = "content")]
+ public List<Content> Contents { get; set; }
+
+ [JsonProperty(PropertyName = "attachments")]
+ public List<Attachment> Attachments { get; set; }
+
+ [JsonProperty(PropertyName = "template_id")]
+ public string TemplateId { get; set; }
+
+ [JsonProperty(PropertyName = "headers")]
+ public Dictionary<string, string> Headers { get; set; }
+
+ [JsonProperty(PropertyName = "sections")]
+ public Dictionary<string, string> Sections { get; set; }
+
+ [JsonProperty(PropertyName = "categories")]
+ public List<string> Categories { get; set; }
+
+ [JsonProperty(PropertyName = "custom_args")]
+ public Dictionary<string, string> CustomArgs { get; set; }
+
+ [JsonProperty(PropertyName = "send_at")]
+ public long? SendAt { get; set; }
+
+ [JsonProperty(PropertyName = "asm")]
+ public ASM Asm { get; set; }
+
+ [JsonProperty(PropertyName = "batch_id")]
+ public string BatchId { get; set; }
+
+ [JsonProperty(PropertyName = "ip_pool_name")]
+ public string SetIpPoolId { get; set; }
+
+ [JsonProperty(PropertyName = "mail_settings")]
+ public MailSettings MailSettings { get; set; }
+
+ [JsonProperty(PropertyName = "tracking_settings")]
+ 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 string Get()
+ {
+ return JsonConvert.SerializeObject(this,
+ Formatting.None,
+ new JsonSerializerSettings
+ {
+ NullValueHandling = NullValueHandling.Ignore,
+ DefaultValueHandling = DefaultValueHandling.Include,
+ StringEscapeHandling = StringEscapeHandling.EscapeHtml
+ });
+ }
+ }
+} \ No newline at end of file