summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEric Becking <eric.becking@sendgrid.com>2012-01-12 14:30:02 -0700
committerEric Becking <eric.becking@sendgrid.com>2012-01-12 14:30:02 -0700
commitd80dbc622a49ce07b7d2a8a43e275d59550202c3 (patch)
tree4a8eb84db7d2892cd5e0976fcd9c7d845e3a8b40
parentfdca704e24d310c69184902dda41fdebc18fbcb5 (diff)
parentb92852fa6c8f27b0723dbe69bcf66ad27d9b7149 (diff)
downloadsendgrid-csharp-d80dbc622a49ce07b7d2a8a43e275d59550202c3.zip
sendgrid-csharp-d80dbc622a49ce07b7d2a8a43e275d59550202c3.tar.gz
sendgrid-csharp-d80dbc622a49ce07b7d2a8a43e275d59550202c3.tar.bz2
Merge branch 'us1882' of github.com:sendgrid/sendgrid-csharp into us1882
Conflicts: SendGrid/SendGridMail/SendGrid.cs €ý5iM€kbmerge commit€kb€kb€kb€kb€kb€kb€kb€kb€kb€kb€kb€kb€kb€kb€kb€kb€kb€kbcommit merge ZZ:q :q!
-rwxr-xr-xSendGrid/Example/Example.csproj1
-rwxr-xr-xSendGrid/Example/Program.cs29
-rwxr-xr-xSendGrid/Example/SMTPAPI.cs105
-rwxr-xr-xSendGrid/SendGridMail/Header.cs11
-rwxr-xr-xSendGrid/SendGridMail/Properties/AssemblyInfo.cs7
-rwxr-xr-xSendGrid/SendGridMail/SendGrid.cs71
-rwxr-xr-xSendGrid/Tests/TestSendgrid.cs289
-rwxr-xr-xSendGrid/Tests/Tests.csproj1
-rwxr-xr-xSendGrid/Tests/Transport/TestREST.cs8
-rwxr-xr-xSendGrid/Tests/Transport/TestSMTP.cs54
10 files changed, 510 insertions, 66 deletions
diff --git a/SendGrid/Example/Example.csproj b/SendGrid/Example/Example.csproj
index 93f7660..b2558bd 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="SMTPAPI.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\SendGridMail\Mail.csproj">
diff --git a/SendGrid/Example/Program.cs b/SendGrid/Example/Program.cs
index 09eaf50..3bbc1db 100755
--- a/SendGrid/Example/Program.cs
+++ b/SendGrid/Example/Program.cs
@@ -13,16 +13,25 @@ namespace Example
{
static void Main(string[] args)
{
- var credentials = new NetworkCredential("cjbuchmann", "Gadget_15");
- var transport = SMTP.GenerateInstance(credentials);
- var header = new Header();
- var message = new SendGrid(header);
- message.AddTo("eric.becking@sendgrid.com");
- message.From = new MailAddress("eric@sendgrid.com");
- message.Text = "This is a test message.";
- message.Html = "<html><p>This is a <b>test</b> message.</p></html>";
- message.Subject = "hazaah!";
- transport.Deliver(message);
+ var username = "cjbuchmann";
+ var password = "Gadget_15";
+ var from = "cj.buchmann@sendgrid.com";
+ var to = new List<String>
+ {
+ "cj.buchmann@gmail.com"
+ };
+
+ //initialize the SMTPAPI example class
+ var smtpapi = new SMTPAPI(username, password, from, to);
+
+ //send a simple HTML encoded email.
+ //smtpapi.SimpleHTMLEmail();
+
+ //send a simple Plaintext email.
+ //smtpapi.SimplePlaintextEmail();
+
+ //send a gravatar enabled email.
+ smtpapi.EnableGravatarEmail();
}
}
}
diff --git a/SendGrid/Example/SMTPAPI.cs b/SendGrid/Example/SMTPAPI.cs
new file mode 100755
index 0000000..f4a48db
--- /dev/null
+++ b/SendGrid/Example/SMTPAPI.cs
@@ -0,0 +1,105 @@
+using System;
+using System.Collections.Generic;
+using System.Net;
+using System.Net.Mail;
+using SendGridMail;
+using SendGridMail.Transport;
+
+namespace Example
+{
+ class SMTPAPI
+ {
+ private String _username;
+ private String _password;
+ private String _from;
+ private IEnumerable<string> _to;
+
+ public SMTPAPI(String username, String password, String from, IEnumerable<string> recipients)
+ {
+ _username = username;
+ _password = password;
+ _from = from;
+ _to = recipients;
+ }
+
+ public void SimpleHTMLEmail()
+ {
+ //create a new message object
+ var message = new SendGrid(new Header());
+
+ //set the message recipients
+ message.AddTo("cj.buchmann@sendgrid.com");
+
+ //set the sender
+ message.From = new MailAddress("eric@sendgrid.com");
+
+ //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 smtpInstance = SMTP.GenerateInstance(new NetworkCredential(_username, _password));
+
+ //send the mail
+ smtpInstance.Deliver(message);
+ }
+
+ public void SimplePlaintextEmail()
+ {
+ //create a new message object
+ var message = new SendGrid(new Header());
+
+ //set the message recipients
+ message.AddTo("cj.buchmann@sendgrid.com");
+
+ //set the sender
+ message.From = new MailAddress("eric@sendgrid.com");
+
+ //set the message body
+ message.Text = "Hello World Plain Text";
+
+ //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));
+
+ //send the mail
+ smtpInstance.Deliver(message);
+ }
+
+ public void EnableGravatarEmail()
+ {
+ var header = new Header();
+ //create a new message object
+ var message = new SendGrid(header);
+
+ //set the message recipients
+ message.AddTo("cj.buchmann@sendgrid.com");
+
+ //set the sender
+ message.From = new MailAddress("cj.buchmann@sendgrid.com");
+
+ //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.EnableGravatar();
+
+ Console.WriteLine(header.AsJson());
+
+ Console.WriteLine("done");
+ //send the mail
+ //smtpInstance.Deliver(message);
+ }
+
+ }
+}
diff --git a/SendGrid/SendGridMail/Header.cs b/SendGrid/SendGridMail/Header.cs
index c1138d8..860ebed 100755
--- a/SendGrid/SendGridMail/Header.cs
+++ b/SendGrid/SendGridMail/Header.cs
@@ -7,6 +7,7 @@ namespace SendGridMail
{
public class Header : IHeader
{
+ private const string SendgridHeader = "X-Smtpapi";
private readonly HeaderSettingsNode _settings;
public Header()
@@ -16,7 +17,7 @@ namespace SendGridMail
public void AddSubVal(string tag, IEnumerable<string> substitutions)
{
- var keys = new List<String> {"data", "sub", tag};
+ var keys = new List<String> {"sub", tag};
_settings.AddArray(keys, substitutions);
}
@@ -24,7 +25,7 @@ namespace SendGridMail
{
foreach (var key in identifiers.Keys)
{
- var keys = new List<String> {"data", "unique_args", key};
+ var keys = new List<String> {"unique_args", key};
var value = identifiers[key];
_settings.AddSetting(keys, value);
}
@@ -32,7 +33,7 @@ namespace SendGridMail
public void SetCategory(string category)
{
- var keys = new List<String> {"data", "category"};
+ var keys = new List<String> {"category"};
_settings.AddSetting(keys, category);
}
@@ -48,13 +49,13 @@ namespace SendGridMail
public void AddFilterSetting(string filter, IEnumerable<string> settings, string value)
{
- var keys = new List<string>() { "data", "filters", filter, "settings" }.Concat(settings).ToList();
+ var keys = new List<string>() {"filters", filter, "settings" }.Concat(settings).ToList();
_settings.AddSetting(keys, value);
}
public void AddHeader(MailMessage mime)
{
- mime.Headers.Add("x-smtpapi", AsJson());
+ mime.Headers.Add(SendgridHeader, AsJson());
}
public String AsJson()
diff --git a/SendGrid/SendGridMail/Properties/AssemblyInfo.cs b/SendGrid/SendGridMail/Properties/AssemblyInfo.cs
index 0cefaba..0108aa9 100755
--- a/SendGrid/SendGridMail/Properties/AssemblyInfo.cs
+++ b/SendGrid/SendGridMail/Properties/AssemblyInfo.cs
@@ -23,6 +23,13 @@ using System.Runtime.InteropServices;
[assembly: Guid("193fa200-8430-4206-aacd-2d2bb2dfa6cf")]
[assembly: InternalsVisibleTo("Tests")]
+[assembly:InternalsVisibleTo("DynamicProxyGenAssembly2," +
+"1310004000001000100c547cac37abd99c8db225ef2f6c8a3602f3b" +
+"PublicKey=002400000480000094000000060200000024000052534" +
+"3606cc9891605d02baa56104f4cfc0734aa39b93bf7852f7d926665" +
+"4753cc297e7d2edfe0bac1cdcf9f717241550e0a7b191195b7667bb" +
+"4f64bcb8e2121380fd1d9d46ad2d92d2d15605093924cceaf74c486" +
+"1eff62abf69b9291ed0a340e113be11e6a7d3113e92484cf7045cc7")]
// Version information for an assembly consists of the following four values:
//
diff --git a/SendGrid/SendGridMail/SendGrid.cs b/SendGrid/SendGridMail/SendGrid.cs
index 2f44c38..2e238f5 100755
--- a/SendGrid/SendGridMail/SendGrid.cs
+++ b/SendGrid/SendGridMail/SendGrid.cs
@@ -236,6 +236,12 @@ namespace SendGridMail
}
public Attachment[] Attachments { get; set; }
+ private List<Attachment> _attachments = new List<Attachment>();
+ public Attachment[] Attachments
+ {
+ get { return _attachments.ToArray(); }
+ set { _attachments = value.ToList(); }
+ }
public void AddSubVal(String tag, params String[] value)
{
@@ -246,55 +252,18 @@ namespace SendGridMail
public void AddAttachment(String filePath)
{
var data = new Attachment(filePath, MediaTypeNames.Application.Octet);
-
- if (Attachments == null)
- {
- Attachments = new Attachment[1];
- Attachments[0] = data;
- }
- else
- {
- var i = Attachments.Count();
- var tmp = new Attachment[i + 1];
- Attachments.CopyTo(tmp, 0);
- Attachments = tmp;
- Attachments[i] = data;
- }
+ _attachments.Add(data);
}
public void AddAttachment(Attachment attachment)
{
- if (Attachments == null)
- {
- Attachments = new Attachment[1];
- Attachments[0] = attachment;
- }
- else
- {
- var i = Attachments.Count();
- var tmp = new Attachment[i + 1];
- Attachments.CopyTo(tmp, 0);
- Attachments = tmp;
- Attachments[i] = attachment;
- }
+ _attachments.Add(attachment);
}
public void AddAttachment(Stream attachment, ContentType type)
{
- var data = new Attachment(attachment, type);
- if (Attachments == null)
- {
- Attachments = new Attachment[1];
- Attachments[0] = data;
- }
- else
- {
- var i = Attachments.Count();
- var tmp = new Attachment[i + 1];
- Attachments.CopyTo(tmp, 0);
- Attachments = tmp;
- Attachments[i] = data;
- }
+ var data = new Attachment(attachment, type);
+ _attachments.Add(data);
}
public IEnumerable<String> GetRecipients()
@@ -404,6 +373,7 @@ namespace SendGridMail
Header.AddFilterSetting(filter, new List<string>(){ "text" }, text);
Header.AddFilterSetting(filter, new List<string>(){ "html" }, html);
Header.AddFilterSetting(filter, new List<string>(){ "replace"}, replace);
+ Header.AddFilterSetting(filter, new List<string>(){ "url"}, url);
Header.AddFilterSetting(filter, new List<string>(){ "landing" }, landing);
}
@@ -421,11 +391,11 @@ namespace SendGridMail
var filter = this._filters["GoogleAnalytics"];
Header.Enable(filter);
- Header.AddFilterSetting(filter, new List<string>(){ "source " }, source);
+ Header.AddFilterSetting(filter, new List<string>(){ "source" }, source);
Header.AddFilterSetting(filter, new List<string>(){ "medium" }, medium);
Header.AddFilterSetting(filter, new List<string>(){ "term" }, term);
Header.AddFilterSetting(filter, new List<string>(){ "content" }, content);
- Header.AddFilterSetting(filter, new List<string>(){ "compaign" }, campaign);
+ Header.AddFilterSetting(filter, new List<string>(){ "campaign" }, campaign);
}
public void EnableTemplate(string html)
@@ -460,7 +430,15 @@ namespace SendGridMail
String smtpapi = Header.AsJson();
if (!String.IsNullOrEmpty(smtpapi))
- message.Headers.Add("X-SmtpApi", "{" + smtpapi + "}");
+ message.Headers.Add("X-Smtpapi", smtpapi);
+
+ if(Attachments != null)
+ {
+ foreach (Attachment attachment in Attachments)
+ {
+ message.Attachments.Add(attachment);
+ }
+ }
if(Attachments != null)
{
@@ -489,7 +467,10 @@ namespace SendGridMail
public void Mail()
{
- throw new NotImplementedException();
+ SmtpClient client = new SmtpClient("localhost");
+ client.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory;
+ client.PickupDirectoryLocation = @"C:\temp";
+ client.Send(message);
}
}
}
diff --git a/SendGrid/Tests/TestSendgrid.cs b/SendGrid/Tests/TestSendgrid.cs
new file mode 100755
index 0000000..97bb7a0
--- /dev/null
+++ b/SendGrid/Tests/TestSendgrid.cs
@@ -0,0 +1,289 @@
+using System;
+using System.Collections.Generic;
+using System.Globalization;
+using System.Linq;
+using System.Text;
+using NUnit.Framework;
+using SendGridMail;
+
+namespace Tests
+{
+ [TestFixture]
+ class TestSendgrid
+ {
+ [Test]
+ public void TestDisableGravatar()
+ {
+ var header = new Header();
+ var sendgrid = new SendGrid(header);
+
+ sendgrid.DisableGravatar();
+
+ String json = header.AsJson();
+ Assert.AreEqual("{\"data\" : {\"filters\" : {\"gravatar\" : {\"settings\" : {\"enable\" : \"0\"}}}}}", json);
+ }
+
+ [Test]
+ public void TestDisableOpenTracking()
+ {
+ var header = new Header();
+ var sendgrid = new SendGrid(header);
+
+ sendgrid.DisableOpenTracking();
+
+ String json = header.AsJson();
+ Assert.AreEqual("{\"data\" : {\"filters\" : {\"opentrack\" : {\"settings\" : {\"enable\" : \"0\"}}}}}", json);
+ }
+
+ [Test]
+ public void DisableClickTracking()
+ {
+ var header = new Header();
+ var sendgrid = new SendGrid(header);
+
+ sendgrid.DisableClickTracking();
+
+ String json = header.AsJson();
+ Assert.AreEqual("{\"data\" : {\"filters\" : {\"clicktrack\" : {\"settings\" : {\"enable\" : \"0\"}}}}}", json);
+ }
+
+ [Test]
+ public void DisableSpamCheck()
+ {
+ var header = new Header();
+ var sendgrid = new SendGrid(header);
+
+ sendgrid.DisableSpamCheck();
+
+ String json = header.AsJson();
+ Assert.AreEqual("{\"data\" : {\"filters\" : {\"spamcheck\" : {\"settings\" : {\"enable\" : \"0\"}}}}}", json);
+ }
+
+ [Test]
+ public void DisableUnsubscribe()
+ {
+ var header = new Header();
+ var sendgrid = new SendGrid(header);
+
+ sendgrid.DisableUnsubscribe();
+
+ String json = header.AsJson();
+ Assert.AreEqual("{\"data\" : {\"filters\" : {\"subscriptiontrack\" : {\"settings\" : {\"enable\" : \"0\"}}}}}", json);
+ }
+
+ [Test]
+ public void DisableFooter()
+ {
+ var header = new Header();
+ var sendgrid = new SendGrid(header);
+
+ sendgrid.DisableFooter();
+
+ String json = header.AsJson();
+ Assert.AreEqual("{\"data\" : {\"filters\" : {\"footer\" : {\"settings\" : {\"enable\" : \"0\"}}}}}", json);
+ }
+
+ [Test]
+ public void DisableGoogleAnalytics()
+ {
+ var header = new Header();
+ var sendgrid = new SendGrid(header);
+
+ sendgrid.DisableGoogleAnalytics();
+
+ String json = header.AsJson();
+ Assert.AreEqual("{\"data\" : {\"filters\" : {\"ganalytics\" : {\"settings\" : {\"enable\" : \"0\"}}}}}", json);
+ }
+
+ [Test]
+ public void DisableTemplate()
+ {
+ var header = new Header();
+ var sendgrid = new SendGrid(header);
+
+ sendgrid.DisableTemplate();
+
+ String json = header.AsJson();
+ Assert.AreEqual("{\"data\" : {\"filters\" : {\"template\" : {\"settings\" : {\"enable\" : \"0\"}}}}}", json);
+ }
+
+ [Test]
+ public void DisableBcc()
+ {
+ var header = new Header();
+ var sendgrid = new SendGrid(header);
+
+ sendgrid.DisableBcc();
+
+ String json = header.AsJson();
+ Assert.AreEqual("{\"data\" : {\"filters\" : {\"bcc\" : {\"settings\" : {\"enable\" : \"0\"}}}}}", json);
+ }
+
+ [Test]
+ public void DisableBypassListManagement()
+ {
+ var header = new Header();
+ var sendgrid = new SendGrid(header);
+
+ sendgrid.DisableBypassListManagement();
+
+ String json = header.AsJson();
+ Assert.AreEqual("{\"data\" : {\"filters\" : {\"bypass_list_management\" : {\"settings\" : {\"enable\" : \"0\"}}}}}", json);
+ }
+
+ [Test]
+ public void EnableGravatar()
+ {
+ var header = new Header();
+ var sendgrid = new SendGrid(header);
+
+ sendgrid.EnableGravatar();
+
+ String json = header.AsJson();
+ Assert.AreEqual("{\"data\" : {\"filters\" : {\"gravatar\" : {\"settings\" : {\"enable\" : \"1\"}}}}}", json);
+ }
+
+ [Test]
+ public void EnableOpenTracking()
+ {
+ var header = new Header();
+ var sendgrid = new SendGrid(header);
+
+ sendgrid.EnableOpenTracking();
+
+ String json = header.AsJson();
+ Assert.AreEqual("{\"data\" : {\"filters\" : {\"opentrack\" : {\"settings\" : {\"enable\" : \"1\"}}}}}", json);
+ }
+
+ [Test]
+ public void EnableClickTracking()
+ {
+ var header = new Header();
+ var sendgrid = new SendGrid(header);
+ var text = "hello world";
+ sendgrid.EnableClickTracking(text);
+
+ String json = header.AsJson();
+ Assert.AreEqual("{\"data\" : {\"filters\" : {\"clicktrack\" : {\"settings\" : {\"enable\" : \"1\",\"text\" : \"hello world\"}}}}}", json);
+ }
+
+ [Test]
+ public void EnableSpamCheck()
+ {
+ var header = new Header();
+ var sendgrid = new SendGrid(header);
+
+ var score = 5;
+ var url = "http://www.example.com";
+ sendgrid.EnableSpamCheck(score, url);
+
+ String json = header.AsJson();
+ Assert.AreEqual("{\"data\" : {\"filters\" : {\"spamcheck\" : {\"settings\" : {\"enable\" : \"1\",\"score\" : \"5\",\"url\" : \"http:\\/\\/www.example.com\"}}}}}", json);
+ }
+
+ [Test]
+ public void EnableUnsubscribe()
+ {
+ var header = new Header();
+ var sendgrid = new SendGrid(header);
+
+ var text = "<% %>";
+ var html = "<% name %>";
+ var replace = "John";
+ var url = "http://www.example.com";
+ var landing = "this_landing";
+ sendgrid.EnableUnsubscribe(text, html, replace, url, landing);
+
+ var jsonText = "\"text\" : \""+text+"\"";
+ var jsonHtml = "\"html\" : \""+html+"\"";
+ var jsonReplace = "\"replace\" : \""+replace+"\"";
+ var jsonUrl = "\"url\" : \"http:\\/\\/www.example.com\"";
+ var jsonLanding = "\"landing\" : \""+landing+"\"";
+
+ String json = header.AsJson();
+ Assert.AreEqual("{\"data\" : {\"filters\" : {\"subscriptiontrack\" : {\"settings\" : {\"enable\" : \"1\","+
+ jsonText+","+jsonHtml+","+jsonReplace+","+jsonUrl+","+jsonLanding+"}}}}}", json);
+
+ }
+
+ [Test]
+ public void EnableFooter()
+ {
+ var header = new Header();
+ var sendgrid = new SendGrid(header);
+
+ var text = "My Text";
+ var html = "<body><p>hello, <% name %></p></body>";
+ var escHtml = "<body><p>hello, <% name %><\\/p><\\/body>";
+
+ sendgrid.EnableFooter(text, html);
+
+ String json = header.AsJson();
+ Assert.AreEqual("{\"data\" : {\"filters\" : {\"footer\" : {\"settings\" : {\"enable\" : \"1\",\"text\" : \""+text+"\",\"html\" : \""+escHtml+"\"}}}}}", json);
+ }
+
+ [Test]
+ public void EnableGoogleAnalytics()
+ {
+ var header = new Header();
+ var sendgrid = new SendGrid(header);
+
+ var source = "SomeDomain.com";
+ var medium = "Email";
+ var term = "keyword1, keyword2, keyword3";
+ var content = "PG, PG13";
+ var campaign = "my_campaign";
+
+ sendgrid.EnableGoogleAnalytics(source, medium, term, content, campaign);
+
+ var jsonSource = "\"source\" : \"SomeDomain.com\"";
+ var jsonMedium = "\"medium\" : \""+medium+"\"";
+ var jsonTerm = "\"term\" : \""+term+"\"";
+ var jsonContent = "\"content\" : \""+content+"\"";
+ var jsonCampaign = "\"campaign\" : \""+campaign+"\"";
+
+ String json = header.AsJson();
+ Assert.AreEqual("{\"data\" : {\"filters\" : {\"ganalytics\" : {\"settings\" : {\"enable\" : \"1\","+
+ jsonSource+","+jsonMedium+","+jsonTerm+","+jsonContent+","+jsonCampaign+"}}}}}", json);
+ }
+
+ [Test]
+ public void EnableTemplate()
+ {
+ var header = new Header();
+ var sendgrid = new SendGrid(header);
+ var html = "<% hadhdhd %>";
+
+ var escHtml = "<% hadhdhd %>";
+ sendgrid.EnableTemplate(html);
+
+ String json = header.AsJson();
+ Assert.AreEqual("{\"data\" : {\"filters\" : {\"template\" : {\"settings\" : {\"enable\" : \"1\",\"html\" : \""+escHtml+"\"}}}}}", json);
+ }
+
+ [Test]
+ public void EnableBcc()
+ {
+ var header = new Header();
+ var sendgrid = new SendGrid(header);
+
+ var email = "somebody@someplace.com";
+ sendgrid.EnableBcc(email);
+
+ String json = header.AsJson();
+ Assert.AreEqual("{\"data\" : {\"filters\" : {\"bcc\" : {\"settings\" : {\"enable\" : \"1\",\"email\" : \"" + email + "\"}}}}}", json);
+ }
+
+ [Test]
+ public void EnableBypassListManagement()
+ {
+ var header = new Header();
+ var sendgrid = new SendGrid(header);
+
+ sendgrid.EnableBypassListManagement();
+
+ String json = header.AsJson();
+ Assert.AreEqual("{\"data\" : {\"filters\" : {\"bypass_list_management\" : {\"settings\" : {\"enable\" : \"1\"}}}}}", json);
+ }
+ }
+}
diff --git a/SendGrid/Tests/Tests.csproj b/SendGrid/Tests/Tests.csproj
index 71b0db4..8009274 100755
--- a/SendGrid/Tests/Tests.csproj
+++ b/SendGrid/Tests/Tests.csproj
@@ -55,6 +55,7 @@
<Compile Include="TestHeader.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="TestJsonUtils.cs" />
+ <Compile Include="TestSendgrid.cs" />
<Compile Include="TestSendgridMessageSetup.cs" />
<Compile Include="TestTreeNode.cs" />
<Compile Include="Transport\TestREST.cs" />
diff --git a/SendGrid/Tests/Transport/TestREST.cs b/SendGrid/Tests/Transport/TestREST.cs
index 747e540..ccbab48 100755
--- a/SendGrid/Tests/Transport/TestREST.cs
+++ b/SendGrid/Tests/Transport/TestREST.cs
@@ -10,7 +10,13 @@ namespace Tests.Transport
class TestREST
{
[Test]
- public void Deliver()
+ public void TestDeliver()
+ {
+
+ }
+
+ [Test]
+ public void TestConstructor()
{
}
diff --git a/SendGrid/Tests/Transport/TestSMTP.cs b/SendGrid/Tests/Transport/TestSMTP.cs
index fcfb050..e0e4fe5 100755
--- a/SendGrid/Tests/Transport/TestSMTP.cs
+++ b/SendGrid/Tests/Transport/TestSMTP.cs
@@ -1,10 +1,9 @@
using System;
-using System.Collections.Generic;
-using System.Linq;
using System.Net;
-using System.Text;
+using System.Net.Mail;
using Moq;
using NUnit.Framework;
+using SendGridMail;
using SendGridMail.Transport;
namespace Tests.Transport
@@ -13,12 +12,57 @@ namespace Tests.Transport
public class TestSMTP
{
[Test]
- public void Deliver()
+ public void TestDeliver()
{
+ var mockMessage = new Mock<ISendGrid>();
+ var mime = new MailMessage("test-from@sendgrid.com", "test-to@sendgrid.com", "this is a test", "it is only a test");
+ mockMessage.Setup(foo => foo.CreateMimeMessage()).Returns(mime);
+ var message = mockMessage.Object;
+
+ var mockClient = new Mock<SMTP.ISmtpClient>();
+ mockClient.Setup(foo => foo.Send(mime));
+ var client = mockClient.Object;
+ var credentials = new NetworkCredential("username", "password");
+ var test = SMTP.GenerateInstance(client, credentials);
+ test.Deliver(message);
+
+ mockClient.Verify(foo => foo.Send(mime), Times.Once());
+ mockMessage.Verify(foo => foo.CreateMimeMessage(), Times.Once());
+ }
+
+ [Test]
+ public void TestConstructor()
+ {
+ //Test on defaults of port 25 and
var mock = new Mock<SMTP.ISmtpClient>();
+ mock.SetupProperty(foo => foo.EnableSsl);
var client = mock.Object;
var credentials = new NetworkCredential("username", "password");
- SMTP.GenerateInstance(client, credentials);
+ var test = SMTP.GenerateInstance(client, credentials);
+ mock.Verify(foo => foo.EnableSsl, Times.Never());
+
+ mock = new Mock<SMTP.ISmtpClient>();
+ mock.SetupProperty(foo => foo.EnableSsl);
+ client = mock.Object;
+ credentials = new NetworkCredential("username", "password");
+ test = SMTP.GenerateInstance(client, credentials, port:SMTP.SslPort);
+ mock.VerifySet(foo => foo.EnableSsl = true);
+
+ mock = new Mock<SMTP.ISmtpClient>();
+ mock.SetupProperty(foo => foo.EnableSsl);
+ client = mock.Object;
+ credentials = new NetworkCredential("username", "password");
+ try
+ {
+ test = SMTP.GenerateInstance(client, credentials, port: SMTP.TlsPort);
+ Assert.Fail("should have thrown an unsupported port exception");
+ }
+ catch (NotSupportedException ex)
+ {
+ Assert.AreEqual("TLS not supported", ex.Message);
+ }
+
+
}
}
}