summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBrandon West <brawest@gmail.com>2013-07-27 12:33:36 -0600
committerBrandon West <brawest@gmail.com>2013-07-29 09:28:31 -0600
commit9257d2669284f01311421e1c16b09a2a43dde725 (patch)
tree581435f3fa14cfbfc334d5bba785030eedfb1dfb
parentc650896e0705fd01c3967211e72e89854d2ec339 (diff)
downloadsendgrid-csharp-9257d2669284f01311421e1c16b09a2a43dde725.zip
sendgrid-csharp-9257d2669284f01311421e1c16b09a2a43dde725.tar.gz
sendgrid-csharp-9257d2669284f01311421e1c16b09a2a43dde725.tar.bz2
working on uploads
-rw-r--r--SendGrid/SendGridMail/Transport/Web.cs316
-rwxr-xr-xSendGrid/SendGridMail/lib/CodeScales.Http.dllbin40960 -> 0 bytes
2 files changed, 159 insertions, 157 deletions
diff --git a/SendGrid/SendGridMail/Transport/Web.cs b/SendGrid/SendGridMail/Transport/Web.cs
index 8832e4c..b7c5507 100644
--- a/SendGrid/SendGridMail/Transport/Web.cs
+++ b/SendGrid/SendGridMail/Transport/Web.cs
@@ -1,186 +1,188 @@
-using System;
-using System.Collections.Generic;
-using System.IO;
-using System.Linq;
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Linq;
using System.Net;
using System.Net.Http.Headers;
-using System.Xml;
-using System.Net.Http;
-
-namespace SendGridMail.Transport
-{
- public class Web : ITransport
- {
- #region Properties
- //TODO: Make this configurable
- public const String BaseUrl = "sendgrid.com/api/";
- public const String Endpoint = "mail.send";
- public const String JsonFormat = "json";
- public const String XmlFormat = "xml";
-
- private readonly NetworkCredential _credentials;
- private readonly bool _https;
- #endregion
-
- /// <summary>
- /// Factory method for Web transport of sendgrid messages
- /// </summary>
- /// <param name="credentials">SendGrid credentials for sending mail messages</param>
- /// <param name="https">Use https?</param>
- /// <returns>New instance of the transport mechanism</returns>
- public static Web GetInstance(NetworkCredential credentials, bool https = true)
- {
- return new Web(credentials, https);
- }
-
- /// <summary>
- /// Creates a new Web interface for sending mail. Preference is using the Factory method.
- /// </summary>
- /// <param name="credentials">SendGrid user parameters</param>
- /// <param name="https">Use https?</param>
- internal Web(NetworkCredential credentials, bool https = true)
- {
- _https = https;
- _credentials = credentials;
- }
-
- /// <summary>
- /// Delivers a message over SendGrid's Web interface
- /// </summary>
- /// <param name="message"></param>
- public async void Deliver(ISendGrid message)
- {
+using System.Xml;
+using System.Net.Http;
+
+namespace SendGridMail.Transport
+{
+ public class Web : ITransport
+ {
+ #region Properties
+ //TODO: Make this configurable
+ public const String BaseUrl = "sendgrid-com-ddrst15a2d5q.runscope.net";//"sendgrid.com/api/";
+ public const String Endpoint = "/api/mail.send";
+ public const String JsonFormat = "json";
+ public const String XmlFormat = "xml";
+
+ private readonly NetworkCredential _credentials;
+ private readonly bool _https;
+ #endregion
+
+ /// <summary>
+ /// Factory method for Web transport of sendgrid messages
+ /// </summary>
+ /// <param name="credentials">SendGrid credentials for sending mail messages</param>
+ /// <param name="https">Use https?</param>
+ /// <returns>New instance of the transport mechanism</returns>
+ public static Web GetInstance(NetworkCredential credentials, bool https = true)
+ {
+ return new Web(credentials, https);
+ }
+
+ /// <summary>
+ /// Creates a new Web interface for sending mail. Preference is using the Factory method.
+ /// </summary>
+ /// <param name="credentials">SendGrid user parameters</param>
+ /// <param name="https">Use https?</param>
+ internal Web(NetworkCredential credentials, bool https = true)
+ {
+ _https = https;
+ _credentials = credentials;
+ }
+
+ /// <summary>
+ /// Delivers a message over SendGrid's Web interface
+ /// </summary>
+ /// <param name="message"></param>
+ public void Deliver(ISendGrid message)
+ {
var client = new HttpClient
{
BaseAddress = _https ? new Uri("https://" + BaseUrl) : new Uri("http://" + BaseUrl)
};
var content = new MultipartFormDataContent();
- AttachFormParams(message, content);
+ AttachFormParams(message, content);
AttachFiles(message, content);
- var response = await client.PostAsync(Endpoint + ".xml", content);
- response.EnsureSuccessStatusCode();
- CheckForErrors(response);
- }
-
- #region Support Methods
- private void AttachFormParams(ISendGrid message, MultipartFormDataContent content)
- {
+ var response = client.PostAsync(Endpoint + ".xml", content).Result;
+ CheckForErrors(response);
+ }
+
+ #region Support Methods
+ private void AttachFormParams(ISendGrid message, MultipartFormDataContent content)
+ {
var formParams = FetchFormParams(message);
foreach (var keyValuePair in formParams)
{
content.Add(new StringContent(keyValuePair.Value), keyValuePair.Key);
- }
+ }
}
- private void AttachFiles(ISendGrid message, MultipartFormDataContent content)
- {
+ private void AttachFiles(ISendGrid message, MultipartFormDataContent content)
+ {
var files = FetchFileBodies (message);
foreach (var file in files)
{
- var fileContent = new StreamContent(File.OpenRead(file.Value.FullName));
- fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
+ var fs = new FileStream(file.Key, FileMode.Open, FileAccess.Read);
+ var fileContent = new StreamContent(fs);
+
+ fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
{
FileName = "files[" + Path.GetFileName(file.Key) + "]"
};
- content.Add(fileContent);
+
+ content.Add(fileContent);
}
-
- var streamingFiles = FetchStreamingFileBodies(message);
- foreach (KeyValuePair<string, MemoryStream> file in streamingFiles) {
- var name = file.Key;
- var stream = file.Value;
- var fileContent = new StreamContent(stream);
-
+
+ var streamingFiles = FetchStreamingFileBodies(message);
+ foreach (KeyValuePair<string, MemoryStream> file in streamingFiles) {
+ var name = file.Key;
+ var stream = file.Value;
+ var fileContent = new StreamContent(stream);
+
fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{
FileName = "files[" + name + "]"
};
- content.Add(fileContent);
- }
- }
-
- private void CheckForErrors (HttpResponseMessage response)
- {
- //transport error
- if (response.StatusCode != HttpStatusCode.OK) {
- throw new Exception(response.ReasonPhrase);
- }
-
- //TODO: check for HTTP errors... don't throw exceptions just pass info along?
- var content = response.Content.ReadAsStreamAsync().Result;
-
- using (var reader = XmlReader.Create(content))
- {
- while (reader.Read())
- {
- if (reader.IsStartElement())
- {
- switch (reader.Name)
- {
- case "result":
- break;
- case "message": // success
- bool errors = reader.ReadToNextSibling("errors");
- if (errors)
+ content.Add(fileContent);
+ }
+ }
+
+ private void CheckForErrors (HttpResponseMessage response)
+ {
+ //transport error
+ if (response.StatusCode != HttpStatusCode.OK) {
+ throw new Exception(response.ReasonPhrase);
+ }
+
+ //TODO: check for HTTP errors... don't throw exceptions just pass info along?
+ var content = response.Content.ReadAsStreamAsync().Result;
+
+ using (var reader = XmlReader.Create(content))
+ {
+ while (reader.Read())
+ {
+ if (reader.IsStartElement())
+ {
+ switch (reader.Name)
+ {
+ case "result":
+ break;
+ case "message": // success
+ bool errors = reader.ReadToNextSibling("errors");
+ if (errors)
throw new ProtocolViolationException();
return;
- case "error": // failure
- throw new ProtocolViolationException();
- default:
- throw new ArgumentException("Unknown element: " + reader.Name);
- }
- }
- }
- }
- }
-
- internal List<KeyValuePair<String, String>> FetchFormParams(ISendGrid message)
- {
+ case "error": // failure
+ throw new ProtocolViolationException();
+ default:
+ throw new ArgumentException("Unknown element: " + reader.Name);
+ }
+ }
+ }
+ }
+ }
+
+ internal List<KeyValuePair<String, String>> FetchFormParams(ISendGrid message)
+ {
var result = new List<KeyValuePair<string, string>>
- {
- new KeyValuePair<String, String>("api_user", _credentials.UserName),
- new KeyValuePair<String, String>("api_key", _credentials.Password),
- new KeyValuePair<String, String>("headers", message.Headers.Count == 0 ? null : Utils.SerializeDictionary(message.Headers)),
- new KeyValuePair<String, String>("replyto", message.ReplyTo.Length == 0 ? null : message.ReplyTo.ToList().First().Address),
- new KeyValuePair<String, String>("from", message.From.Address),
- new KeyValuePair<String, String>("fromname", message.From.DisplayName),
- new KeyValuePair<String, String>("subject", message.Subject),
- new KeyValuePair<String, String>("text", message.Text),
- new KeyValuePair<String, String>("html", message.Html),
- new KeyValuePair<String, String>("x-smtpapi", message.Header.AsJson())
- };
- if(message.To != null)
- {
- result = result.Concat(message.To.ToList().Select(a => new KeyValuePair<String, String>("to[]", a.Address)))
- .Concat(message.To.ToList().Select(a => new KeyValuePair<String, String>("toname[]", a.DisplayName)))
- .ToList();
- }
- if(message.Bcc != null)
- {
- result = result.Concat(message.Bcc.ToList().Select(a => new KeyValuePair<String, String>("bcc[]", a.Address)))
- .ToList();
- }
- if(message.Cc != null)
- {
- result = result.Concat(message.Cc.ToList().Select(a => new KeyValuePair<String, String>("cc[]", a.Address)))
- .ToList();
- }
- return result.Where(r => !String.IsNullOrEmpty(r.Value)).ToList();
- }
-
- internal List<KeyValuePair<String, MemoryStream>> FetchStreamingFileBodies(ISendGrid message)
- {
- return message.StreamedAttachments.Select(kvp => kvp).ToList();
- }
-
- internal List<KeyValuePair<String, FileInfo>> FetchFileBodies(ISendGrid message)
- {
- if(message.Attachments == null)
- return new List<KeyValuePair<string, FileInfo>>();
- return message.Attachments.Select(name => new KeyValuePair<String, FileInfo>(name, new FileInfo(name))).ToList();
- }
-
- #endregion
- }
-}
+ {
+ new KeyValuePair<String, String>("api_user", _credentials.UserName),
+ new KeyValuePair<String, String>("api_key", _credentials.Password),
+ new KeyValuePair<String, String>("headers", message.Headers.Count == 0 ? null : Utils.SerializeDictionary(message.Headers)),
+ new KeyValuePair<String, String>("replyto", message.ReplyTo.Length == 0 ? null : message.ReplyTo.ToList().First().Address),
+ new KeyValuePair<String, String>("from", message.From.Address),
+ new KeyValuePair<String, String>("fromname", message.From.DisplayName),
+ new KeyValuePair<String, String>("subject", message.Subject),
+ new KeyValuePair<String, String>("text", message.Text),
+ new KeyValuePair<String, String>("html", message.Html),
+ new KeyValuePair<String, String>("x-smtpapi", message.Header.AsJson())
+ };
+ if(message.To != null)
+ {
+ result = result.Concat(message.To.ToList().Select(a => new KeyValuePair<String, String>("to[]", a.Address)))
+ .Concat(message.To.ToList().Select(a => new KeyValuePair<String, String>("toname[]", a.DisplayName)))
+ .ToList();
+ }
+ if(message.Bcc != null)
+ {
+ result = result.Concat(message.Bcc.ToList().Select(a => new KeyValuePair<String, String>("bcc[]", a.Address)))
+ .ToList();
+ }
+ if(message.Cc != null)
+ {
+ result = result.Concat(message.Cc.ToList().Select(a => new KeyValuePair<String, String>("cc[]", a.Address)))
+ .ToList();
+ }
+ return result.Where(r => !String.IsNullOrEmpty(r.Value)).ToList();
+ }
+
+ internal List<KeyValuePair<String, MemoryStream>> FetchStreamingFileBodies(ISendGrid message)
+ {
+ return message.StreamedAttachments.Select(kvp => kvp).ToList();
+ }
+
+ internal List<KeyValuePair<String, FileInfo>> FetchFileBodies(ISendGrid message)
+ {
+ if(message.Attachments == null)
+ return new List<KeyValuePair<string, FileInfo>>();
+ return message.Attachments.Select(name => new KeyValuePair<String, FileInfo>(name, new FileInfo(name))).ToList();
+ }
+
+ #endregion
+ }
+}
diff --git a/SendGrid/SendGridMail/lib/CodeScales.Http.dll b/SendGrid/SendGridMail/lib/CodeScales.Http.dll
deleted file mode 100755
index 2509b52..0000000
--- a/SendGrid/SendGridMail/lib/CodeScales.Http.dll
+++ /dev/null
Binary files differ