diff options
author | Tyler Bischel <tyler.bischel@sendgrid.com> | 2012-01-09 17:28:52 -0800 |
---|---|---|
committer | Tyler Bischel <tyler.bischel@sendgrid.com> | 2012-01-09 17:28:52 -0800 |
commit | 9c33d681a996e3edffbdc42e1e3aeadafaf8624d (patch) | |
tree | b93a842c2881f99383d63cdf1076645a367c6146 | |
parent | fbbfb10cdc3c4a4266eb74c132c36c4b7c884fa3 (diff) | |
download | sendgrid-csharp-9c33d681a996e3edffbdc42e1e3aeadafaf8624d.zip sendgrid-csharp-9c33d681a996e3edffbdc42e1e3aeadafaf8624d.tar.gz sendgrid-csharp-9c33d681a996e3edffbdc42e1e3aeadafaf8624d.tar.bz2 |
allow dependency injection of client, constructor is now in a factory method
-rwxr-xr-x | SendGrid/SendGrid/Transport/SMTP.cs | 58 |
1 files changed, 54 insertions, 4 deletions
diff --git a/SendGrid/SendGrid/Transport/SMTP.cs b/SendGrid/SendGrid/Transport/SMTP.cs index ce03250..0b8b684 100755 --- a/SendGrid/SendGrid/Transport/SMTP.cs +++ b/SendGrid/SendGrid/Transport/SMTP.cs @@ -32,18 +32,18 @@ namespace SendGrid.Transport /// <summary>
/// Client used to deliver SMTP message
/// </summary>
- private readonly SmtpClient _client;
+ 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>
- public SMTP(NetworkCredential credentials, String host = SmtpServer, Int32 port = Port)
+ private SMTP(ISmtpClient client, NetworkCredential credentials, string host = SmtpServer, int port = Port)
{
- _client = new SmtpClient(host, port) {Credentials = credentials, DeliveryMethod = SmtpDeliveryMethod.Network};
-
+ _client = client;
switch (port)
{
case Port:
@@ -65,5 +65,55 @@ namespace SendGrid.Transport 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 SmtpFactory(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>
+ /// Interface to allow testing
+ /// </summary>
+ private interface ISmtpClient
+ {
+ bool EnableSsl { get; set; }
+ void Send(MailMessage mime);
+ }
+
+ /// <summary>
+ /// Implementation of SmtpClient wrapper, separated to allow dependency injection
+ /// </summary>
+ private 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);
+ }
+ }
}
}
|