Permalink
Browse files

removed dependency from SmtpClient by introducing ISmtpClient and now…

… its testable
  • Loading branch information...
1 parent bf38b99 commit 0cb9caa01382e367f15c5cb309b2855fb6b91b7d @smsohan committed Jan 26, 2011
Showing with 452 additions and 63 deletions.
  1. +24 −6 Mvc.Mailer.Test/MailMessageExtensionsTest.cs
  2. +2 −0 Mvc.Mailer.Test/Mvc.Mailer.Test.csproj
  3. +44 −0 Mvc.Mailer.Test/SmtpClientWrapperTest.cs
  4. +102 −0 Mvc.Mailer.Test/TestSmtpClientTest.cs
  5. BIN Mvc.Mailer.Test/bin/Release/Mvc.Mailer.Test.dll
  6. +12 −9 Mvc.Mailer.Test/bin/Release/Mvc.Mailer.Test.dll.VisualState.xml
  7. BIN Mvc.Mailer.Test/bin/Release/Mvc.Mailer.Test.pdb
  8. BIN Mvc.Mailer.Test/bin/Release/Mvc.Mailer.dll
  9. BIN Mvc.Mailer.Test/bin/Release/Mvc.Mailer.pdb
  10. +57 −23 Mvc.Mailer.Test/bin/Release/TestResult.xml
  11. +1 −1 Mvc.Mailer.Test/bin/Release/content/Views/Notifier/WelcomeMessage.cshtml
  12. +10 −0 Mvc.Mailer.Test/bin/Release/content/Views/web.config.transform
  13. +1 −0 Mvc.Mailer.Test/obj/Release/Mvc.Mailer.Test.csproj.FileListAbsolute.txt
  14. BIN Mvc.Mailer.Test/obj/Release/Mvc.Mailer.Test.dll
  15. BIN Mvc.Mailer.Test/obj/Release/Mvc.Mailer.Test.pdb
  16. BIN Mvc.Mailer.Test/obj/Release/ResolveAssemblyReference.cache
  17. +16 −0 Mvc.Mailer/ISmtpClient.cs
  18. +24 −7 Mvc.Mailer/MailMessageExtensions.cs
  19. +8 −0 Mvc.Mailer/MailerBase.cs
  20. +4 −0 Mvc.Mailer/Mvc.Mailer.csproj
  21. +3 −17 Mvc.Mailer/NuGet/MvcMailer.nuspec
  22. BIN Mvc.Mailer/NuGet/input/lib/40/Mvc.Mailer.dll
  23. BIN Mvc.Mailer/NuGet/output/MvcMailer.0.6.nupkg
  24. +33 −0 Mvc.Mailer/SmtpClientBase.cs
  25. +60 −0 Mvc.Mailer/SmtpClientWrapper.cs
  26. +51 −0 Mvc.Mailer/TestSmtpClient.cs
  27. BIN Mvc.Mailer/bin/Release/Mvc.Mailer.dll
  28. BIN Mvc.Mailer/bin/Release/Mvc.Mailer.pdb
  29. BIN Mvc.Mailer/obj/Release/Mvc.Mailer.dll
  30. BIN Mvc.Mailer/obj/Release/Mvc.Mailer.pdb
  31. BIN MvcMailer.suo
View
30 Mvc.Mailer.Test/MailMessageExtensionsTest.cs
@@ -13,18 +13,21 @@ namespace Mvc.Mailer.Test
public class MailMessageExtensionsTest
{
- private SmtpClient _smtpClient;
+ private SmtpClientWrapper _smtpClient;
private MailMessage _mailMessage;
private DirectoryInfo _mailDirectory;
[SetUp]
public void SetUp()
{
- _smtpClient = new SmtpClient();
- _smtpClient.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory;
+ var smtpClient = new SmtpClient();
+ smtpClient.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory;
_mailDirectory = Directory.CreateDirectory(Path.Combine(Environment.CurrentDirectory, "Mails"));
- _smtpClient.PickupDirectoryLocation = _mailDirectory.FullName;
+ smtpClient.PickupDirectoryLocation = _mailDirectory.FullName;
+ smtpClient.Host = "smtp.gmail.com";
+ smtpClient.Port = 597;
+ _smtpClient = new SmtpClientWrapper{InnerSmtpClient = smtpClient};
_mailMessage = new MailMessage { From = new MailAddress("gaga@gaga.com") };
_mailMessage.To.Add("gigi@gigi.com");
_mailMessage.Subject = "Hello!";
@@ -34,18 +37,33 @@ public void SetUp()
[Test]
public void TestSend()
{
- _mailMessage.Send(_smtpClient);
+ _mailMessage.Send(_smtpClient);
+ Assert.Pass("Mail Send working since no exception wast thrown");
}
[Test]
public void TestSendAsync()
{
- _mailMessage.Send(_smtpClient);
+ _mailMessage.SendAsync(_smtpClient);
+ Assert.Pass("Mail Send Async working since no exception wast thrown");
+
+ }
+
+ [Test]
+ public void In_Test_Mode_should_use_TestSmtpClient()
+ {
+ TestSmtpClient.SentMails.Clear();
+ MailerBase.IsTestModeEnabled = true;
+ _mailMessage.Send();
+ Assert.AreEqual(1, TestSmtpClient.SentMails.Count);
+ Assert.AreSame(_mailMessage, TestSmtpClient.SentMails[0]);
}
[TearDown]
public void TearDown()
{
+ MailerBase.IsTestModeEnabled = false;
+ TestSmtpClient.SentMails.Clear();
_mailDirectory.Delete(true);
}
View
2 Mvc.Mailer.Test/Mvc.Mailer.Test.csproj
@@ -50,7 +50,9 @@
<Compile Include="MailMessageExtensionsTest.cs" />
<Compile Include="Mocks.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
+ <Compile Include="SmtpClientWrapperTest.cs" />
<Compile Include="StringResultTest.cs" />
+ <Compile Include="TestSmtpClientTest.cs" />
<Compile Include="UrlHelperExtensionTest.cs" />
</ItemGroup>
<ItemGroup>
View
44 Mvc.Mailer.Test/SmtpClientWrapperTest.cs
@@ -0,0 +1,44 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using NUnit.Framework;
+using System.Net.Mail;
+
+namespace Mvc.Mailer.Test
+{
+ [TestFixture]
+ public class SmtpClientWrapperTest
+ {
+ [Test]
+ public void SmtpClientWrapper_should_be_implicitly_created_from_smtp_client()
+ {
+ SmtpClientWrapper smtpClientWrapper = new SmtpClient();
+ }
+
+ [Test]
+ public void Empty_constructor_should_initiaize_InnerSmtpClient()
+ {
+ Assert.IsNotNull(new SmtpClientWrapper().InnerSmtpClient);
+ }
+
+
+ [Test]
+ public void Constructor_with_SmtpClient_should_initiaize_InnerSmtpClient()
+ {
+ SmtpClient client = new SmtpClient();
+ Assert.AreSame(client, new SmtpClientWrapper(client).InnerSmtpClient);
+ }
+
+ [Test]
+ public void Test_InnerSmtpClient()
+ {
+ var wrapper = new SmtpClientWrapper();
+ SmtpClient client = new SmtpClient();
+ wrapper.InnerSmtpClient = client;
+ Assert.AreSame(client, wrapper.InnerSmtpClient);
+ }
+
+
+ }
+}
View
102 Mvc.Mailer.Test/TestSmtpClientTest.cs
@@ -0,0 +1,102 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using NUnit.Framework;
+using System.Net.Mail;
+
+namespace Mvc.Mailer.Test
+{
+ [TestFixture]
+ public class TestSmtpClientTest
+ {
+ TestSmtpClient _testSmtpClient;
+
+ [SetUp]
+ public void Init()
+ {
+ _testSmtpClient = new TestSmtpClient();
+ }
+
+ [Test]
+ public void SentMails_should_be_not_null()
+ {
+ Assert.IsNotNull(TestSmtpClient.SentMails);
+ }
+
+ [Test]
+ public void Send_should_add_to_sent_mails()
+ {
+ var messageA = new MailMessage { From = new MailAddress("hello@example.com"), Subject="Hello", Body="There" };
+ messageA.To.Add("hi@example.com");
+ var messageB = new MailMessage { From = new MailAddress("hi@example.com"), Subject = "There", Body = "Hello" };
+ messageB.To.Add("hello@example.com");
+
+ _testSmtpClient.Send(messageA);
+ _testSmtpClient.Send(messageB);
+
+ Assert.AreEqual(2, TestSmtpClient.SentMails.Count);
+ Assert.AreSame(messageA, TestSmtpClient.SentMails[0]);
+ Assert.AreSame(messageB, TestSmtpClient.SentMails[1]);
+ }
+
+ [Test]
+ public void Send_should_set_async_to_false()
+ {
+ var messageA = new MailMessage { From = new MailAddress("hello@example.com"), Subject = "Hello", Body = "There" };
+ messageA.To.Add("hi@example.com");
+
+ _testSmtpClient.Send(messageA);
+
+ Assert.AreEqual(1, TestSmtpClient.SentMails.Count);
+ Assert.IsFalse(_testSmtpClient.WasLastCallAsync);
+ }
+
+ [Test]
+ public void SendAsync_should_add_to_sent_mails()
+ {
+ var messageA = new MailMessage { From = new MailAddress("hello@example.com"), Subject = "Hello", Body = "There" };
+ messageA.To.Add("hi@example.com");
+ var messageB = new MailMessage { From = new MailAddress("hi@example.com"), Subject = "There", Body = "Hello" };
+ messageB.To.Add("hello@example.com");
+
+ _testSmtpClient.SendAsync(messageA);
+ _testSmtpClient.SendAsync(messageB, "hi");
+
+ Assert.AreEqual(2, TestSmtpClient.SentMails.Count);
+ Assert.AreSame(messageA, TestSmtpClient.SentMails[0]);
+ Assert.AreSame(messageB, TestSmtpClient.SentMails[1]);
+ }
+
+ [Test]
+ public void SendAsync_should_set_async_to_false()
+ {
+ var messageA = new MailMessage { From = new MailAddress("hello@example.com"), Subject = "Hello", Body = "There" };
+ messageA.To.Add("hi@example.com");
+
+ _testSmtpClient.SendAsync(messageA);
+
+ Assert.AreEqual(1, TestSmtpClient.SentMails.Count);
+ Assert.IsTrue(_testSmtpClient.WasLastCallAsync);
+ }
+
+ [Test]
+ public void SendAsync_should_fire_call_back_if_registered()
+ {
+ var eventFired = false;
+ _testSmtpClient.SendCompleted += (sender, e) => eventFired = true;
+ var messageA = new MailMessage { From = new MailAddress("hello@example.com"), Subject = "Hello", Body = "There" };
+ messageA.To.Add("hi@example.com");
+ _testSmtpClient.SendAsync(messageA);
+ Assert.IsTrue(eventFired);
+ }
+
+ [TearDown]
+ public void TearDown()
+ {
+ MailerBase.IsTestModeEnabled = false;
+ TestSmtpClient.SentMails.Clear();
+ }
+
+ }
+}
View
BIN Mvc.Mailer.Test/bin/Release/Mvc.Mailer.Test.dll
Binary file not shown.
View
21 Mvc.Mailer.Test/bin/Release/Mvc.Mailer.Test.dll.VisualState.xml
@@ -1,18 +1,21 @@
<?xml version="1.0" encoding="utf-8"?>
<VisualState xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" ShowCheckBoxes="false">
- <TopNode>[0-1020]C:\Users\S M Sohan\MvcMailer\Mvc.Mailer.Test\bin\Release\Mvc.Mailer.Test.dll</TopNode>
- <SelectedNode>[0-1020]C:\Users\S M Sohan\MvcMailer\Mvc.Mailer.Test\bin\Release\Mvc.Mailer.Test.dll</SelectedNode>
+ <TopNode>[0-1037]C:\Users\S M Sohan\MvcMailer\Mvc.Mailer.Test\bin\Release\Mvc.Mailer.Test.dll</TopNode>
+ <SelectedNode>[0-1010]Mvc.Mailer.Test.MailMessageExtensionsTest.In_Test_Mode_should_use_TestSmtpClient</SelectedNode>
<ExcludeCategories>false</ExcludeCategories>
<Nodes>
- <Node UniqueName="[0-1020]C:\Users\S M Sohan\MvcMailer\Mvc.Mailer.Test\bin\Release\Mvc.Mailer.Test.dll" Expanded="true" />
- <Node UniqueName="[0-1021]Mvc" Expanded="true" />
- <Node UniqueName="[0-1022]Mvc.Mailer" Expanded="true" />
- <Node UniqueName="[0-1023]Mvc.Mailer.Test" Expanded="true" />
- <Node UniqueName="[0-1010]Mvc.Mailer.Test.MailerBaseTest" Expanded="true" />
- <Node UniqueName="[0-1015]Mvc.Mailer.Test.MailerBaseTest.TestIsBodyHtml" Expanded="true" />
- <Node UniqueName="[0-1011]Mvc.Mailer.Test.MailerBaseTest.TestMasterName" Expanded="true" />
+ <Node UniqueName="[0-1037]C:\Users\S M Sohan\MvcMailer\Mvc.Mailer.Test\bin\Release\Mvc.Mailer.Test.dll" Expanded="true" />
+ <Node UniqueName="[0-1038]Mvc" Expanded="true" />
+ <Node UniqueName="[0-1039]Mvc.Mailer" Expanded="true" />
+ <Node UniqueName="[0-1040]Mvc.Mailer.Test" Expanded="true" />
+ <Node UniqueName="[0-1015]Mvc.Mailer.Test.MailerBaseTest" Expanded="true" />
+ <Node UniqueName="[0-1020]Mvc.Mailer.Test.MailerBaseTest.TestIsBodyHtml" Expanded="true" />
+ <Node UniqueName="[0-1016]Mvc.Mailer.Test.MailerBaseTest.TestMasterName" Expanded="true" />
<Node UniqueName="[0-1007]Mvc.Mailer.Test.MailMessageExtensionsTest" Expanded="true" />
+ <Node UniqueName="[0-1032]Mvc.Mailer.Test.SmtpClientWrapperTest" Expanded="true" />
<Node UniqueName="[0-1000]Mvc.Mailer.Test.StringResultTest" Expanded="true" />
<Node UniqueName="[0-1002]Mvc.Mailer.Test.StringResultTest.ExecuteResultWithBlankViewNameShouldThrowArgumentNullException" Expanded="true" />
+ <Node UniqueName="[0-1025]Mvc.Mailer.Test.TestSmtpClientTest" Expanded="true" />
+ <Node UniqueName="[0-1011]Mvc.Mailer.Test.UrlHelperExtensionTest" Expanded="true" />
</Nodes>
</VisualState>
View
BIN Mvc.Mailer.Test/bin/Release/Mvc.Mailer.Test.pdb
Binary file not shown.
View
BIN Mvc.Mailer.Test/bin/Release/Mvc.Mailer.dll
Binary file not shown.
View
BIN Mvc.Mailer.Test/bin/Release/Mvc.Mailer.pdb
Binary file not shown.
View
80 Mvc.Mailer.Test/bin/Release/TestResult.xml
@@ -1,52 +1,86 @@
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<!--This file represents the results of running a test suite-->
-<test-results name="C:\Users\S M Sohan\MvcMailer\Mvc.Mailer.Test\bin\Release\Mvc.Mailer.Test.dll" total="14" errors="0" failures="0" not-run="0" inconclusive="0" ignored="0" skipped="0" invalid="0" date="2011-01-24" time="10:39:59">
+<test-results name="C:\Users\S M Sohan\MvcMailer\Mvc.Mailer.Test\bin\Release\Mvc.Mailer.Test.dll" total="28" errors="0" failures="0" not-run="0" inconclusive="0" ignored="0" skipped="0" invalid="0" date="2011-01-26" time="15:24:29">
<environment nunit-version="2.5.9.10348" clr-version="2.0.50727.4952" os-version="Microsoft Windows NT 6.1.7600.0" platform="Win32NT" cwd="C:\Users\S M Sohan\MvcMailer\Mvc.Mailer.Test\bin\Release" machine-name="SMSOHAN-PC" user="S M Sohan" user-domain="SMSohan-PC" />
<culture-info current-culture="en-US" current-uiculture="en-US" />
- <test-suite type="Assembly" name="C:\Users\S M Sohan\MvcMailer\Mvc.Mailer.Test\bin\Release\Mvc.Mailer.Test.dll" executed="True" result="Success" success="True" time="0.304" asserts="0">
+ <test-suite type="Assembly" name="C:\Users\S M Sohan\MvcMailer\Mvc.Mailer.Test\bin\Release\Mvc.Mailer.Test.dll" executed="True" result="Success" success="True" time="0.672" asserts="0">
<results>
- <test-suite type="Namespace" name="Mvc" executed="True" result="Success" success="True" time="0.292" asserts="0">
+ <test-suite type="Namespace" name="Mvc" executed="True" result="Success" success="True" time="0.659" asserts="0">
<results>
- <test-suite type="Namespace" name="Mailer" executed="True" result="Success" success="True" time="0.289" asserts="0">
+ <test-suite type="Namespace" name="Mailer" executed="True" result="Success" success="True" time="0.644" asserts="0">
<results>
- <test-suite type="Namespace" name="Test" executed="True" result="Success" success="True" time="0.287" asserts="0">
+ <test-suite type="Namespace" name="Test" executed="True" result="Success" success="True" time="0.642" asserts="0">
<results>
- <test-suite type="TestFixture" name="MailerBaseTest" executed="True" result="Success" success="True" time="0.082" asserts="0">
+ <test-suite type="TestFixture" name="MailerBaseTest" executed="True" result="Success" success="True" time="0.125" asserts="0">
<results>
- <test-case name="Mvc.Mailer.Test.MailerBaseTest.PopulateBodyWithNullMailMessage" executed="True" result="Success" success="True" time="0.034" asserts="0" />
- <test-case name="Mvc.Mailer.Test.MailerBaseTest.PopulateBodyWithShouldPutTheViewText" executed="True" result="Success" success="True" time="0.011" asserts="0" />
- <test-suite type="ParameterizedTest" name="TestIsBodyHtml" executed="True" result="Success" success="True" time="0.009" asserts="0">
+ <test-case name="Mvc.Mailer.Test.MailerBaseTest.PopulateBodyWithNullMailMessage" executed="True" result="Success" success="True" time="0.070" asserts="0" />
+ <test-case name="Mvc.Mailer.Test.MailerBaseTest.PopulateBodyWithShouldPutTheViewText" executed="True" result="Success" success="True" time="0.018" asserts="0" />
+ <test-suite type="ParameterizedTest" name="TestIsBodyHtml" executed="True" result="Success" success="True" time="0.012" asserts="0">
<results>
- <test-case name="Mvc.Mailer.Test.MailerBaseTest.TestIsBodyHtml(True)" executed="True" result="Success" success="True" time="0.005" asserts="1" />
+ <test-case name="Mvc.Mailer.Test.MailerBaseTest.TestIsBodyHtml(True)" executed="True" result="Success" success="True" time="0.009" asserts="1" />
<test-case name="Mvc.Mailer.Test.MailerBaseTest.TestIsBodyHtml(False)" executed="True" result="Success" success="True" time="0.000" asserts="1" />
</results>
</test-suite>
- <test-suite type="ParameterizedTest" name="TestMasterName" executed="True" result="Success" success="True" time="0.009" asserts="0">
+ <test-suite type="ParameterizedTest" name="TestMasterName" executed="True" result="Success" success="True" time="0.001" asserts="0">
<results>
- <test-case name="Mvc.Mailer.Test.MailerBaseTest.TestMasterName(null)" executed="True" result="Success" success="True" time="0.000" asserts="1" />
- <test-case name="Mvc.Mailer.Test.MailerBaseTest.TestMasterName(&quot;&quot;)" executed="True" result="Success" success="True" time="0.001" asserts="1" />
+ <test-case name="Mvc.Mailer.Test.MailerBaseTest.TestMasterName(null)" executed="True" result="Success" success="True" time="0.001" asserts="1" />
+ <test-case name="Mvc.Mailer.Test.MailerBaseTest.TestMasterName(&quot;&quot;)" executed="True" result="Success" success="True" time="0.000" asserts="1" />
<test-case name="Mvc.Mailer.Test.MailerBaseTest.TestMasterName(&quot;_Layout&quot;)" executed="True" result="Success" success="True" time="0.000" asserts="1" />
</results>
</test-suite>
</results>
</test-suite>
- <test-suite type="TestFixture" name="MailMessageExtensionsTest" executed="True" result="Success" success="True" time="0.114" asserts="0">
+ <test-suite type="TestFixture" name="MailMessageExtensionsTest" executed="True" result="Success" success="True" time="0.266" asserts="0">
<results>
- <test-case name="Mvc.Mailer.Test.MailMessageExtensionsTest.TestSend" executed="True" result="Success" success="True" time="0.106" asserts="0" />
- <test-case name="Mvc.Mailer.Test.MailMessageExtensionsTest.TestSendAsync" executed="True" result="Success" success="True" time="0.004" asserts="0" />
+ <test-case name="Mvc.Mailer.Test.MailMessageExtensionsTest.In_Test_Mode_should_use_TestSmtpClient" executed="True" result="Success" success="True" time="0.079" asserts="2" />
+ <test-case name="Mvc.Mailer.Test.MailMessageExtensionsTest.TestSend" executed="True" result="Success" success="True" time="0.153" asserts="0">
+ <reason>
+ <message><![CDATA[Mail Send working since no exception wast thrown]]></message>
+ </reason>
+ </test-case>
+ <test-case name="Mvc.Mailer.Test.MailMessageExtensionsTest.TestSendAsync" executed="True" result="Success" success="True" time="0.025" asserts="0">
+ <reason>
+ <message><![CDATA[Mail Send Async working since no exception wast thrown]]></message>
+ </reason>
+ </test-case>
</results>
</test-suite>
- <test-suite type="TestFixture" name="StringResultTest" executed="True" result="Success" success="True" time="0.082" asserts="0">
+ <test-suite type="TestFixture" name="SmtpClientWrapperTest" executed="True" result="Success" success="True" time="0.032" asserts="0">
<results>
- <test-suite type="ParameterizedTest" name="ExecuteResultWithBlankViewNameShouldThrowArgumentNullException" executed="True" result="Success" success="True" time="0.021" asserts="0">
+ <test-case name="Mvc.Mailer.Test.SmtpClientWrapperTest.Constructor_with_SmtpClient_should_initiaize_InnerSmtpClient" executed="True" result="Success" success="True" time="0.002" asserts="1" />
+ <test-case name="Mvc.Mailer.Test.SmtpClientWrapperTest.Empty_constructor_should_initiaize_InnerSmtpClient" executed="True" result="Success" success="True" time="0.015" asserts="1" />
+ <test-case name="Mvc.Mailer.Test.SmtpClientWrapperTest.SmtpClientWrapper_should_be_implicitly_created_from_smtp_client" executed="True" result="Success" success="True" time="0.001" asserts="0" />
+ <test-case name="Mvc.Mailer.Test.SmtpClientWrapperTest.Test_InnerSmtpClient" executed="True" result="Success" success="True" time="0.001" asserts="1" />
+ </results>
+ </test-suite>
+ <test-suite type="TestFixture" name="StringResultTest" executed="True" result="Success" success="True" time="0.090" asserts="0">
+ <results>
+ <test-suite type="ParameterizedTest" name="ExecuteResultWithBlankViewNameShouldThrowArgumentNullException" executed="True" result="Success" success="True" time="0.028" asserts="0">
<results>
- <test-case name="Mvc.Mailer.Test.StringResultTest.ExecuteResultWithBlankViewNameShouldThrowArgumentNullException(null)" executed="True" result="Success" success="True" time="0.017" asserts="0" />
- <test-case name="Mvc.Mailer.Test.StringResultTest.ExecuteResultWithBlankViewNameShouldThrowArgumentNullException(&quot;&quot;)" executed="True" result="Success" success="True" time="0.000" asserts="0" />
+ <test-case name="Mvc.Mailer.Test.StringResultTest.ExecuteResultWithBlankViewNameShouldThrowArgumentNullException(null)" executed="True" result="Success" success="True" time="0.022" asserts="0" />
+ <test-case name="Mvc.Mailer.Test.StringResultTest.ExecuteResultWithBlankViewNameShouldThrowArgumentNullException(&quot;&quot;)" executed="True" result="Success" success="True" time="0.001" asserts="0" />
</results>
</test-suite>
- <test-case name="Mvc.Mailer.Test.StringResultTest.ExecuteResultWithNullContextShouldThrowArgumentNullException" executed="True" result="Success" success="True" time="0.001" asserts="0" />
- <test-case name="Mvc.Mailer.Test.StringResultTest.ShouldRenderViewIntoOutputProperty" executed="True" result="Success" success="True" time="0.051" asserts="1" />
- <test-case name="Mvc.Mailer.Test.StringResultTest.ShouldRenderViewIntoOutputPropertyWhenViewIsSet" executed="True" result="Success" success="True" time="0.001" asserts="1" />
+ <test-case name="Mvc.Mailer.Test.StringResultTest.ExecuteResultWithNullContextShouldThrowArgumentNullException" executed="True" result="Success" success="True" time="0.003" asserts="0" />
+ <test-case name="Mvc.Mailer.Test.StringResultTest.ShouldRenderViewIntoOutputProperty" executed="True" result="Success" success="True" time="0.034" asserts="1" />
+ <test-case name="Mvc.Mailer.Test.StringResultTest.ShouldRenderViewIntoOutputPropertyWhenViewIsSet" executed="True" result="Success" success="True" time="0.000" asserts="1" />
+ </results>
+ </test-suite>
+ <test-suite type="TestFixture" name="TestSmtpClientTest" executed="True" result="Success" success="True" time="0.035" asserts="0">
+ <results>
+ <test-case name="Mvc.Mailer.Test.TestSmtpClientTest.Send_should_add_to_sent_mails" executed="True" result="Success" success="True" time="0.003" asserts="3" />
+ <test-case name="Mvc.Mailer.Test.TestSmtpClientTest.Send_should_set_async_to_false" executed="True" result="Success" success="True" time="0.002" asserts="2" />
+ <test-case name="Mvc.Mailer.Test.TestSmtpClientTest.SendAsync_should_add_to_sent_mails" executed="True" result="Success" success="True" time="0.004" asserts="3" />
+ <test-case name="Mvc.Mailer.Test.TestSmtpClientTest.SendAsync_should_fire_call_back_if_registered" executed="True" result="Success" success="True" time="0.002" asserts="1" />
+ <test-case name="Mvc.Mailer.Test.TestSmtpClientTest.SendAsync_should_set_async_to_false" executed="True" result="Success" success="True" time="0.001" asserts="2" />
+ <test-case name="Mvc.Mailer.Test.TestSmtpClientTest.SentMails_should_be_not_null" executed="True" result="Success" success="True" time="0.001" asserts="1" />
+ </results>
+ </test-suite>
+ <test-suite type="TestFixture" name="UrlHelperExtensionTest" executed="True" result="Success" success="True" time="0.063" asserts="0">
+ <results>
+ <test-case name="Mvc.Mailer.Test.UrlHelperExtensionTest.Abs_with_abs_should_return_itself" executed="True" result="Success" success="True" time="0.047" asserts="1" />
+ <test-case name="Mvc.Mailer.Test.UrlHelperExtensionTest.Abs_with_relative_should_absolutilze" executed="True" result="Success" success="True" time="0.008" asserts="1" />
+ <test-case name="Mvc.Mailer.Test.UrlHelperExtensionTest.Abs_with_root_should_absolutilze" executed="True" result="Success" success="True" time="0.001" asserts="1" />
</results>
</test-suite>
</results>
View
2 Mvc.Mailer.Test/bin/Release/content/Views/Notifier/WelcomeMessage.cshtml
@@ -4,4 +4,4 @@ Welcome to The BestSiteEver!<br />
Your subscription begins at @DateTime.Now.ToShortTimeString()!
<br/>
<!-- Use UrlHelper with protocol to generate absolute URL in your email views -->
-Please visit <a href="@Url.Action("About", "Home", null, "http")">The About Us Page</a> to Learn more about us.
+Please visit <a href="@Url.Abs(Url.Action("About", "Home"))">The About Us Page</a> to Learn more about us.
View
10 Mvc.Mailer.Test/bin/Release/content/Views/web.config.transform
@@ -0,0 +1,10 @@
+<configuration>
+ <system.web.webPages.razor>
+ <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
+ <pages pageBaseType="System.Web.Mvc.WebViewPage">
+ <namespaces>
+ <add namespace="Mvc.Mailer" />
+ </namespaces>
+ </pages>
+ </system.web.webPages.razor>
+</configuration>
View
1 Mvc.Mailer.Test/obj/Release/Mvc.Mailer.Test.csproj.FileListAbsolute.txt
@@ -14,3 +14,4 @@ C:\Users\S M Sohan\MvcMailer\Mvc.Mailer.Test\obj\Release\Mvc.Mailer.Test.pdb
C:\Users\S M Sohan\MvcMailer\Mvc.Mailer.Test\bin\Release\Moq.dll
C:\Users\S M Sohan\MvcMailer\Mvc.Mailer.Test\bin\Release\Moq.pdb
C:\Users\S M Sohan\MvcMailer\Mvc.Mailer.Test\bin\Release\Moq.xml
+C:\Users\S M Sohan\MvcMailer\Mvc.Mailer.Test\bin\Release\content\Views\web.config.transform
View
BIN Mvc.Mailer.Test/obj/Release/Mvc.Mailer.Test.dll
Binary file not shown.
View
BIN Mvc.Mailer.Test/obj/Release/Mvc.Mailer.Test.pdb
Binary file not shown.
View
BIN Mvc.Mailer.Test/obj/Release/ResolveAssemblyReference.cache
Binary file not shown.
View
16 Mvc.Mailer/ISmtpClient.cs
@@ -0,0 +1,16 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Net.Mail;
+
+namespace Mvc.Mailer
+{
+ public interface ISmtpClient : IDisposable
+ {
+ event SendCompletedEventHandler SendCompleted;
+ void Send(MailMessage mailMessage);
+ void SendAsync(MailMessage mailMessage);
+ void SendAsync(MailMessage mailMessage, object userState);
+ }
+}
View
31 Mvc.Mailer/MailMessageExtensions.cs
@@ -20,22 +20,39 @@ public static class MailMessageExtensions
/// </summary>
/// <param name="message">The mailMessage Object</param>
/// <param name="smtpClient">leave null to use default System.Net.Mail.SmtpClient</param>
- public static void Send(this MailMessage message, SmtpClient smtpClient = null)
+ public static void Send(this MailMessage message, ISmtpClient smtpClient = null)
{
- smtpClient = smtpClient ?? new SmtpClient();
- smtpClient.Send(message);
+ smtpClient = smtpClient ?? GetSmtpClient();
+ using (smtpClient)
+ {
+ smtpClient.Send(message);
+ }
}
/// <summary>
/// Asynchronously Sends a MailMessage using smtpClient
/// </summary>
/// <param name="message">The mailMessage Object</param>
/// <param name="smtpClient">leave null to use default System.Net.Mail.SmtpClient</param>
- public static void SendAsync(this MailMessage message, SmtpClient smtpClient = null)
+ public static void SendAsync(this MailMessage message, ISmtpClient smtpClient = null)
{
- smtpClient = smtpClient ?? new SmtpClient();
- var userState = "nothing";
- smtpClient.SendAsync(message, userState);
+ smtpClient = smtpClient ?? GetSmtpClient();
+ var userState = "userState";
+
+ using (smtpClient)
+ {
+ smtpClient.SendAsync(message, userState);
+ }
+ }
+
+ public static ISmtpClient GetSmtpClient()
+ {
+ if (MailerBase.IsTestModeEnabled)
+ {
+ return new TestSmtpClient();
+ }
+ return new SmtpClientWrapper();
+
}
}
View
8 Mvc.Mailer/MailerBase.cs
@@ -36,6 +36,7 @@ public MailerBase()
{
CurrentHttpContext = new HttpContextWrapper(HttpContext.Current);
}
+ IsTestModeEnabled = false;
}
/// <summary>
@@ -122,6 +123,13 @@ public HttpContextBase CurrentHttpContext
internal set;
}
+
+ public static bool IsTestModeEnabled
+ {
+ get;
+ set;
+ }
+
}
}
View
4 Mvc.Mailer/Mvc.Mailer.csproj
@@ -43,6 +43,10 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
+ <Compile Include="SmtpClientBase.cs" />
+ <Compile Include="SmtpClientWrapper.cs" />
+ <Compile Include="TestSmtpClient.cs" />
+ <Compile Include="ISmtpClient.cs" />
<Compile Include="UrlHelperExtension.cs" />
<Compile Include="MailerBase.cs" />
<Compile Include="MailMessageExtensions.cs" />
View
20 Mvc.Mailer/NuGet/MvcMailer.nuspec
@@ -5,23 +5,9 @@
<version>0.6</version>
<authors>S. M. SOHAN</authors>
<description>
- MvcMailer sends emails using the MVC views as Email Body with no effort. See an example at the project site.
- This is email done eight for ASP.Net MVC - no more spaghetti code for emailing :) See the code project article
- at http://www.codeproject.com/KB/aspnet/MvcMailerNuGet.aspx to learn more about MvcMailer!
-
- MvcMailer has a MailerBase class that extends ControllerBase class. So, Mailers are just like your Controllers.
- As a result, you can use all the controller goodies without any learning curve, period. The end result
- is a professional looking HTML email body ready for your to send to your website users.
-
- Here's a quick list of features:
- a) Use Razor/WebForms views
- b) Use Master pages
- c) Write Testable Code
- d) Pass values to your view using ViewBag or ViewModel and
- e) Generate Absolute URL using Url.Abs method [New in V0.5].
-
- This package has a very good amount of test coverage and if you want, take a look at the project site.
-
+MvcMailer sends emails using the MVC views as Email Body with no effort. Here's a quick list of features: a) Use Razor/WebForms views b) Use Master pages c) Write Testable Code d) Pass values to your view using ViewBag or ViewModel and e) Generate Absolute URL using Url.Abs method [New in V0.5].
+MvcMailer has a MailerBase class that extends ControllerBase class. So, Mailers are just like your Controllers. As a result, you can use all the controller goodies without any learning curve, period. The end result is a professional looking HTML email body ready for your to send to your website users.
+This package has a very good amount of test coverage and if you want, take a look at the project site.
</description>
<language>en-US</language>
<projectUrl>https://github.com/smsohan/MvcMailer</projectUrl>
View
BIN Mvc.Mailer/NuGet/input/lib/40/Mvc.Mailer.dll
Binary file not shown.
View
BIN Mvc.Mailer/NuGet/output/MvcMailer.0.6.nupkg
Binary file not shown.
View
33 Mvc.Mailer/SmtpClientBase.cs
@@ -0,0 +1,33 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Net.Mail;
+using System.ComponentModel;
+
+namespace Mvc.Mailer
+{
+ public abstract class SmtpClientBase : ISmtpClient
+ {
+ public event SendCompletedEventHandler SendCompleted;
+
+ protected void OnSendCompleted(object sender, AsyncCompletedEventArgs e)
+ {
+ if (SendCompleted != null)
+ {
+ SendCompleted(sender, e);
+ }
+ }
+
+ public abstract void Send(MailMessage mailMessage);
+
+ public virtual void SendAsync(MailMessage mailMessage)
+ {
+ var userState = "userState";
+ SendAsync(mailMessage, userState);
+ }
+
+ public abstract void SendAsync(MailMessage mailMessage, object userState);
+ public abstract void Dispose();
+ }
+}
View
60 Mvc.Mailer/SmtpClientWrapper.cs
@@ -0,0 +1,60 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Net.Mail;
+using System.ComponentModel;
+
+namespace Mvc.Mailer
+{
+ public class SmtpClientWrapper : SmtpClientBase
+ {
+ public SmtpClientWrapper()
+ {
+ InnerSmtpClient = new SmtpClient();
+ }
+
+ public SmtpClientWrapper(SmtpClient smtpClient)
+ {
+ InnerSmtpClient = smtpClient;
+ }
+
+ public SmtpClient InnerSmtpClient
+ {
+ get;
+ set;
+ }
+
+ public override void Send(MailMessage mailMessage)
+ {
+ using (InnerSmtpClient)
+ {
+ InnerSmtpClient.Send(mailMessage);
+ }
+ }
+
+ public override void SendAsync(MailMessage mailMessage, object userState)
+ {
+ using (InnerSmtpClient)
+ {
+ InnerSmtpClient.SendCompleted += new SendCompletedEventHandler(InnerSmtpClient_SendCompleted);
+ InnerSmtpClient.SendAsync(mailMessage, userState);
+ }
+ }
+
+ void InnerSmtpClient_SendCompleted(object sender, AsyncCompletedEventArgs e)
+ {
+ OnSendCompleted(sender, e);
+ }
+
+ public override void Dispose()
+ {
+ InnerSmtpClient.Dispose();
+ }
+
+ public static implicit operator SmtpClientWrapper(SmtpClient innerSmtpClient)
+ {
+ return new SmtpClientWrapper(innerSmtpClient);
+ }
+ }
+}
View
51 Mvc.Mailer/TestSmtpClient.cs
@@ -0,0 +1,51 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Net.Mail;
+using System.ComponentModel;
+
+namespace Mvc.Mailer
+{
+ public class TestSmtpClient : SmtpClientBase
+ {
+ [ThreadStatic]
+ private static List<MailMessage> _sentMails;
+ public static List<MailMessage> SentMails
+ {
+ get{
+ _sentMails = _sentMails ?? new List<MailMessage>();
+ return _sentMails;
+ }
+ }
+
+ public bool WasLastCallAsync
+ {
+ get;
+ set;
+ }
+
+ public TestSmtpClient()
+ {
+
+ }
+
+ public override void Send(MailMessage mailMessage)
+ {
+ WasLastCallAsync = false;
+ SentMails.Add(mailMessage);
+ }
+
+ public override void SendAsync(MailMessage mailMessage, object userState)
+ {
+ WasLastCallAsync = true;
+ SentMails.Add(mailMessage);
+ OnSendCompleted(this, new AsyncCompletedEventArgs(null, false, userState));
+ }
+
+ public override void Dispose()
+ {
+
+ }
+ }
+}
View
BIN Mvc.Mailer/bin/Release/Mvc.Mailer.dll
Binary file not shown.
View
BIN Mvc.Mailer/bin/Release/Mvc.Mailer.pdb
Binary file not shown.
View
BIN Mvc.Mailer/obj/Release/Mvc.Mailer.dll
Binary file not shown.
View
BIN Mvc.Mailer/obj/Release/Mvc.Mailer.pdb
Binary file not shown.
View
BIN MvcMailer.suo
Binary file not shown.

0 comments on commit 0cb9caa

Please sign in to comment.