diff options
-rwxr-xr-x | SendGrid/Example/Program.cs | 8 | ||||
-rwxr-xr-x | SendGrid/SendGridMail/ISendGrid.cs | 16 | ||||
-rwxr-xr-x | SendGrid/SendGridMail/Mail.csproj | 2 | ||||
-rwxr-xr-x | SendGrid/SendGridMail/SendGrid.cs | 22 | ||||
-rwxr-xr-x | SendGrid/SendGridMail/Transport/REST.cs | 114 | ||||
-rwxr-xr-x | SendGrid/Tests/TestSendgridMessageSetup.cs | 20 | ||||
-rwxr-xr-x | SendGrid/packages/CodeScales.Http.dll | bin | 0 -> 40960 bytes |
7 files changed, 80 insertions, 102 deletions
diff --git a/SendGrid/Example/Program.cs b/SendGrid/Example/Program.cs index 4709470..6230d06 100755 --- a/SendGrid/Example/Program.cs +++ b/SendGrid/Example/Program.cs @@ -13,7 +13,7 @@ namespace Example {
/*static void Main(String[] args)
{
- var restInstance = REST.GetInstance(new NetworkCredential("cjbuchmann", "Gadget_15"));
+ var restInstance = REST.GetInstance(new NetworkCredential("cjbuchmann", "gadget15"));
//create a new message object
var message = SendGrid.GenerateInstance();
@@ -22,10 +22,10 @@ namespace Example message.From = new MailAddress("cj.buchmann@sendgrid.com");
message.Html = "<div>hello world</div>";
message.Subject = "Hello World SUbject";
- message.AddAttachment(@"D:\att_proj\21.jpg");
+ message.AddAttachment(@"C:\Users\Public\Pictures\Sample Pictures\Koala.jpg");
+ message.AddAttachment(@"C:\Users\Public\Pictures\Sample Pictures\Penguins.jpg");
- //restInstance.Deliver(message);
- restInstance.TestDeliver(message);
+ restInstance.Deliver(message);
Console.WriteLine("Message Sent");
Console.WriteLine("DONE!");
diff --git a/SendGrid/SendGridMail/ISendGrid.cs b/SendGrid/SendGridMail/ISendGrid.cs index 81952d3..a292edc 100755 --- a/SendGrid/SendGridMail/ISendGrid.cs +++ b/SendGrid/SendGridMail/ISendGrid.cs @@ -31,7 +31,7 @@ namespace SendGridMail MailAddress[] Cc { get; }
MailAddress[] Bcc { get; }
MailAddress[] ReplyTo { get; set; }
- Attachment[] Attachments { get; set; }
+ String[] Attachments { get; set; }
String Subject { get; set; }
Dictionary<String, String> Headers { get; set; }
IHeader Header { get; set; }
@@ -120,20 +120,6 @@ namespace SendGridMail /// </summary>
/// <param name="filePath"></param>
void AddAttachment(String filePath);
- /// <summary>
- /// Add an attachment to the message
- /// The 'attachment' paramater expects an argument of the Attachment type.
- /// </summary>
- /// <param name="attachment"></param>
- void AddAttachment(Attachment attachment);
- /// <summary>
- /// Add an attachment to the message
- /// The 'attachment' paramater expects an argument of the Attachment type.
- /// The 'type' parameter expects an argument of the ContentType type.
- /// </summary>
- /// <param name="attachment"></param>
- /// <param name="type"></param>
- void AddAttachment(Stream attachment, ContentType type);
/// <summary>
/// GetRecipients returns a list of all the recepients by retrieving the to, cc, and bcc lists.
diff --git a/SendGrid/SendGridMail/Mail.csproj b/SendGrid/SendGridMail/Mail.csproj index 5d422b9..f333f36 100755 --- a/SendGrid/SendGridMail/Mail.csproj +++ b/SendGrid/SendGridMail/Mail.csproj @@ -32,7 +32,7 @@ </PropertyGroup>
<ItemGroup>
<Reference Include="CodeScales.Http">
- <HintPath>..\..\..\references\CodeScales.Http.dll</HintPath>
+ <HintPath>..\packages\CodeScales.Http.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
diff --git a/SendGrid/SendGridMail/SendGrid.cs b/SendGrid/SendGridMail/SendGrid.cs index 7237895..0a6a466 100755 --- a/SendGrid/SendGridMail/SendGrid.cs +++ b/SendGrid/SendGridMail/SendGrid.cs @@ -282,8 +282,8 @@ namespace SendGridMail }
}
- private List<Attachment> _attachments = new List<Attachment>();
- public Attachment[] Attachments
+ private List<String> _attachments = new List<String>();
+ public String[] Attachments
{
get { return _attachments.ToArray(); }
set { _attachments = value.ToList(); }
@@ -297,19 +297,7 @@ namespace SendGridMail public void AddAttachment(String filePath)
{
- var data = new Attachment(filePath, MediaTypeNames.Application.Octet);
- _attachments.Add(data);
- }
-
- public void AddAttachment(Attachment attachment)
- {
- _attachments.Add(attachment);
- }
-
- public void AddAttachment(Stream attachment, ContentType type)
- {
- var data = new Attachment(attachment, type);
- _attachments.Add(data);
+ _attachments.Add(filePath);
}
public IEnumerable<String> GetRecipients()
@@ -493,9 +481,9 @@ namespace SendGridMail if(Attachments != null)
{
- foreach (Attachment attachment in Attachments)
+ foreach (var attachment in Attachments)
{
- message.Attachments.Add(attachment);
+ message.Attachments.Add(new Attachment(attachment, MediaTypeNames.Application.Octet));
}
}
diff --git a/SendGrid/SendGridMail/Transport/REST.cs b/SendGrid/SendGridMail/Transport/REST.cs index 3bb23a6..f96fa21 100755 --- a/SendGrid/SendGridMail/Transport/REST.cs +++ b/SendGrid/SendGridMail/Transport/REST.cs @@ -8,24 +8,29 @@ using System.Net.Mail; using System.Text;
using System.Web;
using System.Xml;
+using CodeScales.Http;
+using CodeScales.Http.Entity;
+using CodeScales.Http.Entity.Mime;
+using CodeScales.Http.Methods;
+using HttpResponse = System.Web.HttpResponse;
namespace SendGridMail.Transport
{
public class REST : ITransport
{
// REST delivery settings
- public const String Endpoint = "https://sendgrid.com/api/mail.send";
+ public const String Endpoint = "http://sendgrid.com/api/mail.send";
public const String JsonFormat = "json";
public const String XmlFormat = "xml";
private readonly List<KeyValuePair<String, String>> _query;
+ private readonly NetworkCredential _credentials;
private readonly NameValueCollection _queryParameters;
private readonly String _restEndpoint;
private readonly String _format;
private WebFileUpload _fileUpload;
-
public static REST GetInstance(NetworkCredential credentials, String url = Endpoint)
{
return new REST(credentials, url);
@@ -33,74 +38,78 @@ namespace SendGridMail.Transport internal REST(NetworkCredential credentials, String url = Endpoint)
{
- _query = new List<KeyValuePair<string, string>>();
- _queryParameters = HttpUtility.ParseQueryString(String.Empty);
-
- addQueryParam("api_user", credentials.UserName);
- addQueryParam("api_key", credentials.Password);
+ _credentials = credentials;
_format = XmlFormat;
_restEndpoint = url + "." + _format;
}
- private void addQueryParam(String key, String value)
+ private List<KeyValuePair<String, String>> FetchFormParams(ISendGrid message)
{
- _query.Add(new KeyValuePair<string, string>(key, value));
+ 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();
}
- public void TestDeliver(ISendGrid Message)
+ private List<KeyValuePair<String, FileInfo>> FetchFileBodies(ISendGrid message)
{
- new WebFileUpload(null).SendAttachments();
+ if(message.Attachments == null)
+ return new List<KeyValuePair<string, FileInfo>>();
+ return message.Attachments.Select(name => new KeyValuePair<String, FileInfo>(name, new FileInfo(name))).ToList();
}
public void Deliver(ISendGrid message)
{
- // TODO Fix this to include all recipients
- message.To.ToList().ForEach(a => addQueryParam("to[]", a.Address));
- message.Bcc.ToList().ForEach(a => addQueryParam("bcc[]", a.Address));
- message.Cc.ToList().ForEach(a => addQueryParam("cc[]", a.Address));
+ var client = new HttpClient();
+ var postMethod = new HttpPost(new Uri(_restEndpoint));
- message.To.ToList().ForEach(a => addQueryParam("toname[]", a.DisplayName));
+ var multipartEntity = new MultipartEntity();
+ postMethod.Entity = multipartEntity;
+
+ var formParams = FetchFormParams(message);
+
+ formParams.ForEach(kvp => multipartEntity.AddBody(new StringBody(Encoding.UTF8, kvp.Key, kvp.Value)));
- addQueryParam("headers", Utils.SerializeDictionary(message.Headers));
+ var files = FetchFileBodies(message);
+ files.ForEach(kvp => multipartEntity.AddBody(new FileBody("files["+kvp.Key+"]", kvp.Key, kvp.Value)));
- message.ReplyTo.ToList().ForEach(a => addQueryParam("replyto", a.Address));
- //addQueryParam("", message.From.Address);
+ CodeScales.Http.Methods.HttpResponse response = client.Execute(postMethod);
- addQueryParam("from", message.From.Address);
- addQueryParam("fromname", message.From.DisplayName);
+ Console.WriteLine("Response Code: " + response.ResponseCode);
+ Console.WriteLine("Response Content: " + EntityUtils.ToString(response.Entity));
- addQueryParam("subject", message.Subject);
- addQueryParam("text", message.Text);
- addQueryParam("html", message.Html);
+ Console.WriteLine("Res");
- String smtpapi = message.Header.AsJson();
+ var status = EntityUtils.ToString(response.Entity);
+ var stream = new MemoryStream(Encoding.UTF8.GetBytes(status));
- if (!String.IsNullOrEmpty(smtpapi))
- addQueryParam("x-smtpapi", smtpapi);
-
- var queryString = FetchQueryString();
-
- var restCommand = new Uri(_restEndpoint + "?" + queryString);
-
- var request = (HttpWebRequest)WebRequest.Create(restCommand.AbsoluteUri);
-
- Console.WriteLine(restCommand.AbsoluteUri);
-
- //if we have message attachments, we'll send them via the WebFileUpload
- /*if(message.Attachments.Length > 0)
- {
- Console.WriteLine("Initializing the File Upload Library");
- new WebFileUpload(request).testNoAttach(message.Attachments.First());
- }*/
- var response = (HttpWebResponse)request.GetResponse();
-
- // Basically, read the entire message out before we parse the XML.
- // That way, if we detect an error, we can give the whole response to the client.
- var r = new StreamReader(response.GetResponseStream());
- var status = r.ReadToEnd();
- var bytes = Encoding.ASCII.GetBytes(status);
- var stream = new MemoryStream(bytes);
using (var reader = XmlReader.Create(stream))
{
@@ -123,10 +132,5 @@ namespace SendGridMail.Transport }
}
}
-
- private string FetchQueryString()
- {
- return String.Join("&", _query.Where(kvp => !String.IsNullOrEmpty(kvp.Value)).Select(kvp => kvp.Key + "=" + kvp.Value));
- }
}
}
diff --git a/SendGrid/Tests/TestSendgridMessageSetup.cs b/SendGrid/Tests/TestSendgridMessageSetup.cs index 219dbf3..efbb853 100755 --- a/SendGrid/Tests/TestSendgridMessageSetup.cs +++ b/SendGrid/Tests/TestSendgridMessageSetup.cs @@ -143,20 +143,20 @@ namespace Tests var data = new Attachment("pnunit.framework.dll", MediaTypeNames.Application.Octet);
sg.AddAttachment("pnunit.framework.dll");
sg.AddAttachment("pnunit.framework.dll");
- Assert.AreEqual(data.ContentStream, sg.Attachments.First().ContentStream, "Attach via path");
- Assert.AreEqual(data.ContentStream, sg.Attachments.Skip(1).First().ContentStream, "Attach via path x2");
+ //Assert.AreEqual(data.ContentStream, sg.Attachments.First().ContentStream, "Attach via path");
+ //Assert.AreEqual(data.ContentStream, sg.Attachments.Skip(1).First().ContentStream, "Attach via path x2");
sg = new SendGrid(foo.Object);
- sg.AddAttachment(data);
- sg.AddAttachment(data);
- Assert.AreEqual(data.ContentStream, sg.Attachments.First().ContentStream, "Attach via attachment");
- Assert.AreEqual(data.ContentStream, sg.Attachments.Skip(1).First().ContentStream, "Attach via attachment x2");
+ //sg.AddAttachment(data);
+ //sg.AddAttachment(data);
+ //Assert.AreEqual(data.ContentStream, sg.Attachments.First().ContentStream, "Attach via attachment");
+ //Assert.AreEqual(data.ContentStream, sg.Attachments.Skip(1).First().ContentStream, "Attach via attachment x2");
sg = new SendGrid(foo.Object);
- sg.AddAttachment(data.ContentStream, data.ContentType);
- sg.AddAttachment(data.ContentStream, data.ContentType);
- Assert.AreEqual(data.ContentStream, sg.Attachments.First().ContentStream, "Attach via stream");
- Assert.AreEqual(data.ContentStream, sg.Attachments.Skip(1).First().ContentStream, "Attach via stream x2");
+ //sg.AddAttachment(data.ContentStream, data.ContentType);
+ //sg.AddAttachment(data.ContentStream, data.ContentType);
+ //Assert.AreEqual(data.ContentStream, sg.Attachments.First().ContentStream, "Attach via stream");
+ //Assert.AreEqual(data.ContentStream, sg.Attachments.Skip(1).First().ContentStream, "Attach via stream x2");
}
}
}
diff --git a/SendGrid/packages/CodeScales.Http.dll b/SendGrid/packages/CodeScales.Http.dll Binary files differnew file mode 100755 index 0000000..2509b52 --- /dev/null +++ b/SendGrid/packages/CodeScales.Http.dll |