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 | |
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.
-rwxr-xr-x | SendGrid/Example/Example.csproj | 1 | ||||
-rwxr-xr-x | SendGrid/Example/RESTAPI.cs | 78 | ||||
-rwxr-xr-x | SendGrid/Example/SMTPAPI.cs | 59 | ||||
-rwxr-xr-x | SendGrid/SendGridMail/SendGrid.cs | 23 | ||||
-rwxr-xr-x | SendGrid/SendGridMail/Transport/REST.cs | 40 |
5 files changed, 168 insertions, 33 deletions
diff --git a/SendGrid/Example/Example.csproj b/SendGrid/Example/Example.csproj index b2558bd..776c740 100755 --- a/SendGrid/Example/Example.csproj +++ b/SendGrid/Example/Example.csproj @@ -47,6 +47,7 @@ <ItemGroup>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
+ <Compile Include="RESTAPI.cs" />
<Compile Include="SMTPAPI.cs" />
</ItemGroup>
<ItemGroup>
diff --git a/SendGrid/Example/RESTAPI.cs b/SendGrid/Example/RESTAPI.cs new file mode 100755 index 0000000..ca4e507 --- /dev/null +++ b/SendGrid/Example/RESTAPI.cs @@ -0,0 +1,78 @@ +using System;
+using System.Collections.Generic;
+using System.Net;
+using System.Net.Mail;
+using SendGridMail;
+using SendGridMail.Transport;
+
+namespace Example
+{
+ class RESTAPI
+ {
+ private String _username;
+ private String _password;
+ private String _from;
+ private IEnumerable<String> _to;
+ private IEnumerable<String> _bcc;
+ private IEnumerable<String> _cc;
+
+ public RESTAPI(String username, String password, String from, IEnumerable<String> recipients, IEnumerable<String> bcc, IEnumerable<String> cc)
+ {
+ _username = username;
+ _password = password;
+ _from = from;
+ _to = recipients;
+ _bcc = bcc;
+ _cc = cc;
+ }
+
+ public void SimpleHTMLEmail()
+ {
+ //create a new message object
+ var message = new SendGrid(new Header());
+
+ //set the message recipients
+
+ if(_to != null)
+ {
+ foreach (String recipient in _to)
+ {
+ message.AddTo(recipient);
+ }
+ }
+
+
+ if(_bcc != null)
+ {
+ foreach (String blindcc in _bcc)
+ {
+ message.AddBcc(blindcc);
+ }
+ }
+
+ if(_cc != null)
+ {
+ foreach (String cc in _cc)
+ {
+ message.AddCc(cc);
+ }
+ }
+
+
+ //set the sender
+ message.From = new MailAddress(_from);
+
+ //set the message body
+ message.Html = "<html><p>Hello</p><p>World</p></html>";
+
+ //set the message subject
+ message.Subject = "Hello World Simple Test";
+
+ //create an instance of the SMTP transport mechanism
+ var restInstance = new REST(new NetworkCredential(_username, _password));
+
+ //send the mail
+ restInstance.Deliver(message);
+ }
+ }
+}
diff --git a/SendGrid/Example/SMTPAPI.cs b/SendGrid/Example/SMTPAPI.cs index f4a48db..6ed10cb 100755 --- a/SendGrid/Example/SMTPAPI.cs +++ b/SendGrid/Example/SMTPAPI.cs @@ -28,10 +28,13 @@ namespace Example var message = new SendGrid(new Header());
//set the message recipients
- message.AddTo("cj.buchmann@sendgrid.com");
+ foreach(string recipient in _to)
+ {
+ message.AddTo(recipient);
+ }
//set the sender
- message.From = new MailAddress("eric@sendgrid.com");
+ message.From = new MailAddress(_from);
//set the message body
message.Html = "<html><p>Hello</p><p>World</p></html>";
@@ -52,10 +55,13 @@ namespace Example var message = new SendGrid(new Header());
//set the message recipients
- message.AddTo("cj.buchmann@sendgrid.com");
+ foreach(string recipient in _to)
+ {
+ message.AddTo(recipient);
+ }
//set the sender
- message.From = new MailAddress("eric@sendgrid.com");
+ message.From = new MailAddress(_from);
//set the message body
message.Text = "Hello World Plain Text";
@@ -77,19 +83,22 @@ namespace Example var message = new SendGrid(header);
//set the message recipients
- message.AddTo("cj.buchmann@sendgrid.com");
+ foreach (string recipient in _to)
+ {
+ message.AddTo(recipient);
+ }
//set the sender
- message.From = new MailAddress("cj.buchmann@sendgrid.com");
+ message.From = new MailAddress(_from);
//set the message body
- message.Html = "<p style='color:red';>Hello World Plain Text</p>";
+ message.Html = "<p style='color:red';>Hello World Gravatar Email</p>";
//set the message subject
message.Subject = "Hello World Simple Test";
//create an instance of the SMTP transport mechanism
- //var smtpInstance = SMTP.GenerateInstance(new NetworkCredential(_username, _password));
+ var smtpInstance = SMTP.GenerateInstance(new NetworkCredential(_username, _password));
//enable gravatar
message.EnableGravatar();
@@ -98,6 +107,40 @@ namespace Example Console.WriteLine("done");
//send the mail
+ smtpInstance.Deliver(message);
+ }
+
+ public void EnableOpenTrackingEmail()
+ {
+ var header = new Header();
+ //create a new message object
+ var message = new SendGrid(header);
+
+ //set the message recipients
+ foreach (string recipient in _to)
+ {
+ message.AddTo(recipient);
+ }
+
+ //set the sender
+ message.From = new MailAddress(_from);
+
+ //set the message body
+ message.Html = "<p style='color:red';>Hello World Plain Text</p>";
+
+ //set the message subject
+ message.Subject = "Hello World Simple Test";
+
+ //create an instance of the SMTP transport mechanism
+ //var smtpInstance = SMTP.GenerateInstance(new NetworkCredential(_username, _password));
+
+ //enable gravatar
+ message.EnableOpenTracking();
+
+ Console.WriteLine(header.AsJson());
+
+ Console.WriteLine("done");
+ //send the mail
//smtpInstance.Deliver(message);
}
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));
+ }
}
}
|