E-Mail testing without mail server.

October 17, 2008


If one of below question is your, you might found some benefit from reading further:-

  1. Implement the e-mail wrapper class. Can it send an e-mail?
  2. Unit test my code. Where is the mail server which I could use?
  3. Ureka I could test my code !! What about memory usage or Load? Load testing on my code may flooding the email system.

Still reading??? Hmm …

Beginning

I am working on the product which will have a tiny little mail wrapper which will forward the e-mail to the SMTP gateway for e-mailing to subscriber purpose. However it could not be launch to production without testing on pre-pre-pre-production environment (sounds funny?). How could we ensure that this functionality is working as we expected? Mock it – which class to mock, the mail server? Unit test – what about connectivity? using the existing production mail server – Operation guy might give us a call and yell out.

Trial & Error

After I did some google, I found lots of interesting post which might help me solving all 3 questions. The first one I try is nDumbSter for .NET fake email testing. Reading and get going with interesting unit test code in their web site is fascinating me. However the mail sender they use in their sample test code is based on System.Web.Mail which is not only marked as obsoleted from Microsoft but also not the one we used in my project. Since we use System.Net.Mail instead. What is the different? you might considered reading this.

Try sending the mail from System.Net.Mail to nDumbster will result the nDumbSter to be DUMB!!! response for 1 email in 100 seconds !!! (right, One hundred Second) and will throw the exception for the next email. What’s next??

Solution

Let’s try another google, I come along with another interesting solution from Lumisoft Mail Server, after rough look this should be the right one. So I download and extract it – and found below subfolder:-

  • MailServer
  • Net
  • RFC

Quick overview – Net?? Might be the one, with curious mind I did open the solution and compile it. Gotcha the DLL is in place – so what’s next? Another google on usage sample for this brought me to this Wrapper class. Copy&Paste development has across my mind, however the Wrapper itself could not be compiled properly. Below is some list of change which might help you get through it.

  • IPBindInfo ?? – Try BindInfo instead.
  • still fail? – Try using all necessary reference.

After that – I wrote some unit test based on simple System.Net.Mail library as below Sample section. Not only wrapper class which I could use for Unit testing but also it could be extend and serve as a temporary mail server for Load testing as well.

Sample Test Code

[TestFixture]
public class Class1
{
private SMTPSimulator m_SMTPSim;
private ServerInfo m_serverInfo;
[SetUp]
public void SetUp()
{
m_SMTPSim = new SMTPSimulator();
m_serverInfo = new ServerInfo();
m_SMTPSim.Start(“127.0.0.1″, 25);
}

[TearDown]
public void TearDown()
{
m_SMTPSim.ClearReceivedEmails();
m_SMTPSim.Stop();
m_SMTPSim.Dispose();
}
[Test]
public void TestSimpleMail()
{
string fromAdd = “From@foo.com”;
string toAdd = “To@bar.com”;
string mailSub = “mail Subject”;
string mailBody = “mail Body”;
//sending Email
MailMessage mailMessage = new MailMessage(fromAdd,toAdd,mailSub,mailBody);
SendEmailWithSystemNetMail(m_serverInfo, mailMessage);

//validation
Assert.Greater(m_SMTPSim.ReceivedEmailsCount, 0);

//Retrive e-mail from SMTP sim
ReceivedEmail[] receivedEmailS = m_SMTPSim.ReceivedEmails;

Assert.AreEqual(fromAdd, receivedEmailS[0].From.Replace(‘<‘,’ ‘).Replace(‘>’,’ ‘).Trim());
Assert.AreEqual(toAdd, receivedEmailS[0].To.Replace(‘<‘, ‘ ‘).Replace(‘>’, ‘ ‘).Trim());
Assert.AreEqual(mailSub, receivedEmailS[0].Subject);
Assert.AreEqual(mailBody, receivedEmailS[0].Body.Replace(“\r”,” “).Replace(“\n”,” “).Trim());

}

[Test]
public void TestTimeForSimpleEmailSend100Times()
{
string fromAdd = “From@foo.com”;
string toAdd = “To@bar.com”;
string mailSub = “mail Subject”;
string mailBody = “mail Body”;

//sending Email
MailMessage mailMessage = new MailMessage(fromAdd, toAdd, mailSub, mailBody);

Console.WriteLine(“Start : ” + DateTime.Now);
for (int i = 0; i < 100; i++)
{
SendEmailWithSystemNetMail(m_serverInfo, mailMessage);
}
Console.WriteLine(“Stop : ” + DateTime.Now);

}

public void SendEmailWithSystemNetMail(ServerInfo serverInfo, MailMessage mailMessage)
{
SmtpClient smtpClient = new SmtpClient(serverInfo.IP, serverInfo.Port);
smtpClient.Send(mailMessage);
}

}

Add to Technorati Favorites
add to del.icio.us :: Add to Blinkslist :: add to furl :: Digg it :: add to ma.gnolia :: Stumble It! :: add to simpy :: seed the vine :: :: :: TailRank :: post to facebook


Hello world!

October 6, 2008

Welcome to WordPress.com. This is your first post. Edit or delete it and start blogging!


Follow

Get every new post delivered to your Inbox.