diff options
author | Tyler Bischel <tyler.bischel@sendgrid.com> | 2012-01-10 13:40:15 -0800 |
---|---|---|
committer | Tyler Bischel <tyler.bischel@sendgrid.com> | 2012-01-10 13:40:15 -0800 |
commit | e7e7ea043de835793b9d65e6176e6148d0b51e29 (patch) | |
tree | 04490de0b36b3fcbdc4433a678b30cd18f31dc8c /SendGrid/SendGridMail/Transport | |
parent | e40ecaea2026a525650c02f6b84526c68762fbcb (diff) | |
download | sendgrid-csharp-e7e7ea043de835793b9d65e6176e6148d0b51e29.zip sendgrid-csharp-e7e7ea043de835793b9d65e6176e6148d0b51e29.tar.gz sendgrid-csharp-e7e7ea043de835793b9d65e6176e6148d0b51e29.tar.bz2 |
fixed namespace weirdness, made changes to .gitignore
Diffstat (limited to 'SendGrid/SendGridMail/Transport')
-rwxr-xr-x | SendGrid/SendGridMail/Transport/ITransport.cs | 14 | ||||
-rwxr-xr-x | SendGrid/SendGridMail/Transport/REST.cs | 82 | ||||
-rwxr-xr-x | SendGrid/SendGridMail/Transport/SMTP.cs | 132 |
3 files changed, 228 insertions, 0 deletions
diff --git a/SendGrid/SendGridMail/Transport/ITransport.cs b/SendGrid/SendGridMail/Transport/ITransport.cs new file mode 100755 index 0000000..26fccf4 --- /dev/null +++ b/SendGrid/SendGridMail/Transport/ITransport.cs @@ -0,0 +1,14 @@ +namespace SendGridMail.Transport
+{
+ /// <summary>
+ ///
+ /// </summary>
+ public interface ITransport
+ {
+ /// <summary>
+ /// Delivers a message using the protocol of the derived class
+ /// </summary>
+ /// <param name="message">the message to be delivered</param>
+ void Deliver(ISendGrid message);
+ }
+}
diff --git a/SendGrid/SendGridMail/Transport/REST.cs b/SendGrid/SendGridMail/Transport/REST.cs new file mode 100755 index 0000000..dd37567 --- /dev/null +++ b/SendGrid/SendGridMail/Transport/REST.cs @@ -0,0 +1,82 @@ +using System;
+using System.Collections.Generic;
+using System.Collections.Specialized;
+using System.IO;
+using System.Linq;
+using System.Net;
+using System.Text;
+using System.Web;
+using System.Xml;
+
+namespace SendGridMail.Transport
+{
+ public class REST : ITransport
+ {
+ // REST delivery settings
+ public const String Endpoint = "https://sendgrid.com/api/mail.send";
+ public const String JsonFormat = "json";
+ public const String XmlFormat = "xml";
+
+ private readonly NameValueCollection _queryParameters;
+ private readonly String _restEndpoint;
+ private readonly String _format;
+
+ public REST(NetworkCredential credentials, String url = Endpoint)
+ {
+ _queryParameters = HttpUtility.ParseQueryString(String.Empty);
+ _queryParameters["api_user"] = credentials.UserName;
+ _queryParameters["api_key"] = credentials.Password;
+
+ _format = XmlFormat;
+ _restEndpoint = url + "." + _format;
+ }
+
+ 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;
+
+ String smtpapi = message.Header.AsJson();
+
+ if (!String.IsNullOrEmpty(smtpapi))
+ _queryParameters["x-smtpapi"] = smtpapi;
+
+ var restCommand = new Uri(_restEndpoint + "?" + _queryParameters);
+
+ var request = (HttpWebRequest)WebRequest.Create(restCommand.AbsoluteUri);
+ 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))
+ {
+ while (reader.Read())
+ {
+ if (reader.IsStartElement())
+ {
+ switch (reader.Name)
+ {
+ case "result":
+ break;
+ case "message": // success
+ return;
+ case "error": // failure
+ throw new ProtocolViolationException(status);
+ default:
+ throw new ArgumentException("Unknown element: " + reader.Name);
+ }
+ }
+ }
+ }
+ }
+ }
+}
diff --git a/SendGrid/SendGridMail/Transport/SMTP.cs b/SendGrid/SendGridMail/Transport/SMTP.cs new file mode 100755 index 0000000..606afcd --- /dev/null +++ b/SendGrid/SendGridMail/Transport/SMTP.cs @@ -0,0 +1,132 @@ +using System;
+using System.Net;
+using System.Net.Mail;
+
+namespace SendGridMail.Transport
+{
+ /// <summary>
+ /// Transport class for delivering messages via SMTP
+ /// </summary>
+ public class SMTP : ITransport
+ {
+ /// <summary>
+ /// SendGrid's host name
+ /// </summary>
+ public const String SmtpServer = "smtp.sendgrid.net";
+
+ /// <summary>
+ /// Port for Simple Mail Transfer Protocol
+ /// </summary>
+ public const Int32 Port = 25;
+
+ /// <summary>
+ /// Port for Secure SMTP
+ /// </summary>
+ public const Int32 SslPort = 465;
+
+ /// <summary>
+ /// Port for TLS (currently not supported)
+ /// </summary>
+ public const Int32 TlsPort = 571;
+
+ /// <summary>
+ /// Client used to deliver SMTP message
+ /// </summary>
+ private readonly ISmtpClient _client;
+
+ /// <summary>
+ /// Transport created to deliver messages to SendGrid using SMTP
+ /// </summary>
+ /// <param name="client">SMTP client we are wrapping</param>
+ /// <param name="credentials">Sendgrid user credentials</param>
+ /// <param name="host">MTA recieving this message. By default, sent through SendGrid.</param>
+ /// <param name="port">SMTP port 25 is the default. Port 465 can be used for Secure SMTP.</param>
+ private SMTP(ISmtpClient client, NetworkCredential credentials, string host = SmtpServer, int port = Port)
+ {
+ _client = client;
+ switch (port)
+ {
+ case Port:
+ break;
+ case SslPort:
+ _client.EnableSsl = true;
+ break;
+ case TlsPort:
+ throw new NotSupportedException("TLS not supported");
+ }
+ }
+
+ /// <summary>
+ /// Deliver an email using SMTP protocol
+ /// </summary>
+ /// <param name="message"></param>
+ public void Deliver(ISendGrid message)
+ {
+ var mime = message.CreateMimeMessage();
+ _client.Send(mime);
+ }
+
+ /// <summary>
+ /// Transport created to deliver messages to SendGrid using SMTP
+ /// </summary>
+ /// <param name="credentials">Sendgrid user credentials</param>
+ /// <param name="host">MTA recieving this message. By default, sent through SendGrid.</param>
+ /// <param name="port">SMTP port 25 is the default. Port 465 can be used for Secure SMTP.</param>
+ public static SMTP GenerateInstance(NetworkCredential credentials, String host = SmtpServer, Int32 port = Port)
+ {
+ var client = new SmtpWrapper(host, port, credentials, SmtpDeliveryMethod.Network);
+ return new SMTP(client, credentials, host, port);
+ }
+
+ /// <summary>
+ /// For Unit Testing Only!
+ /// </summary>
+ /// <param name="client"></param>
+ /// <param name="credentials"></param>
+ /// <param name="host"></param>
+ /// <param name="port"></param>
+ /// <returns></returns>
+ internal static SMTP GenerateInstance(ISmtpClient client, NetworkCredential credentials, String host = SmtpServer, Int32 port = Port)
+ {
+ return new SMTP(client, credentials, host, port);
+ }
+
+ /// <summary>
+ /// Interface to allow testing
+ /// </summary>
+ internal interface ISmtpClient
+ {
+ bool EnableSsl { get; set; }
+ void Send(MailMessage mime);
+ }
+
+ /// <summary>
+ /// Implementation of SmtpClient wrapper, separated to allow dependency injection
+ /// </summary>
+ internal class SmtpWrapper : ISmtpClient
+ {
+ private readonly SmtpClient _client;
+ public bool EnableSsl
+ {
+ get
+ {
+ return _client.EnableSsl;
+ }
+ set
+ {
+ _client.EnableSsl = value;
+ }
+ }
+
+ public SmtpWrapper(String host, Int32 port, NetworkCredential credentials, SmtpDeliveryMethod deliveryMethod)
+ {
+ _client = new SmtpClient(host, port) { Credentials = credentials, DeliveryMethod = deliveryMethod };
+ }
+
+ public void Send(MailMessage mime)
+ {
+ _client.Send(mime);
+ }
+ }
+ }
+}
|