diff options
author | CJ Buchmann <cj.buchmann@sendgrid.com> | 2012-01-12 16:08:13 -0800 |
---|---|---|
committer | CJ Buchmann <cj.buchmann@sendgrid.com> | 2012-01-12 16:08:13 -0800 |
commit | 149f8c4ddb933f91fa32f6216d3fe78ae0636d0e (patch) | |
tree | d62ffae05d02608b811254f7ea708698b24e1363 /SendGrid/SendGridMail | |
parent | 5be7429848bf36f52381d2dff4001d009f5ee89e (diff) | |
download | sendgrid-csharp-149f8c4ddb933f91fa32f6216d3fe78ae0636d0e.zip sendgrid-csharp-149f8c4ddb933f91fa32f6216d3fe78ae0636d0e.tar.gz sendgrid-csharp-149f8c4ddb933f91fa32f6216d3fe78ae0636d0e.tar.bz2 |
Added support for Rest API, to get mailing working. Also added examples to send a simple web api.
Diffstat (limited to 'SendGrid/SendGridMail')
-rwxr-xr-x | SendGrid/SendGridMail/SendGrid.cs | 23 | ||||
-rwxr-xr-x | SendGrid/SendGridMail/Transport/REST.cs | 40 |
2 files changed, 38 insertions, 25 deletions
diff --git a/SendGrid/SendGridMail/SendGrid.cs b/SendGrid/SendGridMail/SendGrid.cs index 2e238f5..b473d5d 100755 --- a/SendGrid/SendGridMail/SendGrid.cs +++ b/SendGrid/SendGridMail/SendGrid.cs @@ -56,7 +56,6 @@ namespace SendGridMail Bcc = bcc;
message.Subject = subject;
- message.SubjectEncoding = Encoding.GetEncoding(charset);
Text = text;
Html = html;
@@ -235,7 +234,6 @@ namespace SendGridMail }
}
- public Attachment[] Attachments { get; set; }
private List<Attachment> _attachments = new List<Attachment>();
public Attachment[] Attachments
{
@@ -440,12 +438,10 @@ namespace SendGridMail }
}
- if(Attachments != null)
- {
- foreach (Attachment attachment in Attachments)
- {
- message.Attachments.Add(attachment);
- }
+ if (Text != null)
+ { // Encoding.GetEncoding(charset)
+ AlternateView plainView = AlternateView.CreateAlternateViewFromString(Text, null, "text/plain");
+ message.AlternateViews.Add(plainView);
}
if (Html != null)
@@ -453,14 +449,9 @@ namespace SendGridMail AlternateView htmlView = AlternateView.CreateAlternateViewFromString(Html, null, "text/html");
message.AlternateViews.Add(htmlView);
}
-
- if (Text != null)
- {
- AlternateView plainView = AlternateView.CreateAlternateViewFromString(Text, null, "text/plain");
- message.AlternateViews.Add(plainView);
- }
-
- message.BodyEncoding = Encoding.GetEncoding(charset);
+
+ //message.SubjectEncoding = Encoding.GetEncoding(charset);
+ //message.BodyEncoding = Encoding.GetEncoding(charset);
return message;
}
diff --git a/SendGrid/SendGridMail/Transport/REST.cs b/SendGrid/SendGridMail/Transport/REST.cs index dd37567..85e3f5b 100755 --- a/SendGrid/SendGridMail/Transport/REST.cs +++ b/SendGrid/SendGridMail/Transport/REST.cs @@ -4,6 +4,7 @@ using System.Collections.Specialized; using System.IO;
using System.Linq;
using System.Net;
+using System.Net.Mail;
using System.Text;
using System.Web;
using System.Xml;
@@ -17,35 +18,51 @@ namespace SendGridMail.Transport public const String JsonFormat = "json";
public const String XmlFormat = "xml";
+ private readonly List<KeyValuePair<String, String>> _query;
private readonly NameValueCollection _queryParameters;
private readonly String _restEndpoint;
private readonly String _format;
public REST(NetworkCredential credentials, String url = Endpoint)
{
+ _query = new List<KeyValuePair<string, string>>();
_queryParameters = HttpUtility.ParseQueryString(String.Empty);
- _queryParameters["api_user"] = credentials.UserName;
- _queryParameters["api_key"] = credentials.Password;
+
+ addQueryParam("api_user", credentials.UserName);
+ addQueryParam("api_key", credentials.Password);
_format = XmlFormat;
_restEndpoint = url + "." + _format;
}
+ private void addQueryParam(String key, String value)
+ {
+ _query.Add(new KeyValuePair<string, string>(key, value));
+ }
+
public void Deliver(ISendGrid message)
{
// TODO Fix this to include all recipients
- _queryParameters["to"] = message.To.First().ToString();
- _queryParameters["from"] = message.From.ToString();
- _queryParameters["subject"] = message.Subject;
- _queryParameters["text"] = message.Text;
- _queryParameters["html"] = message.Html;
+ 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));
+ //_queryParameters["toname[]"] = String.Join(",", message.To.Select(a => a.DisplayName)); // message.To.First().ToString();
+ addQueryParam("from", message.From.Address);
+ addQueryParam("subject", message.Subject);
+ addQueryParam("text", message.Text);
+ addQueryParam("html", message.Html);
String smtpapi = message.Header.AsJson();
if (!String.IsNullOrEmpty(smtpapi))
- _queryParameters["x-smtpapi"] = smtpapi;
+ addQueryParam("x-smtpapi", smtpapi);
+
+ var queryString = FetchQueryString();
- var restCommand = new Uri(_restEndpoint + "?" + _queryParameters);
+ var restCommand = new Uri(_restEndpoint + "?" + queryString);
+
+ Console.WriteLine(restCommand.AbsoluteUri);
+ Console.WriteLine("DONE!");
var request = (HttpWebRequest)WebRequest.Create(restCommand.AbsoluteUri);
var response = (HttpWebResponse)request.GetResponse();
@@ -78,5 +95,10 @@ namespace SendGridMail.Transport }
}
}
+
+ private string FetchQueryString()
+ {
+ return String.Join("&", _query.Select(kvp => kvp.Key + "=" + kvp.Value));
+ }
}
}
|