Sunday 16 December 2012

Test Email without Mail Server

Test Email without Mail Server


The tip I am going to share today is an old one but many developers do not know about it. Assume you are creating an application and need to test a module that sends out bulk mails, like a newsletter. Your first thought would be to configure an SMTP server to test the email module. However there is a trick that you can make use of while testing your code. Use the following setting in your web.config:
<system.net>
  <
mailSettings>
      <
smtp deliveryMethod="SpecifiedPickupDirectory">
     <specifiedPickupDirectory pickupDirectoryLocation="C:\Mails\"/>
      </smtp>
  </mailSettings>
</system.net>

The specifiedPickupDirectory element configures the local directory for a Simple Mail Transport Protocol (SMTP) server. The pickupDirectoryLocation is a directory where the application saves e-mail for later processing by the SMTP server. Make sure the directory exists before using it.
That’s it. Test this setting using the following code:
protected void btnMail_Click(object sender, EventArgs e)
{
    MailMessage message = new MailMessage("abc@somedomain.com",
        "abc@abcdefgh.com",
        "Newsletter", "This is a test mail");

    SmtpClient client = new SmtpClient("localhost");
    client.Send(message);
}
Run the application and click on the Send button. Now open the Mails folder in your C Drive and you should see your mail there. Similarly you can test your entire newsletter module without sending out mails to real email addresses.



Cool ain’t it! Just make sure that if you are using IIS, it should have read-write permissions to this folder.

No comments:

Post a Comment

Thank You for Your Comments. We will get back to you soon.

back to top