Loading...

Sending test emails without mail server in asp.net

My friend has asked me about sending test emails without mail server, so here I am providing your step by step tutorial. 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:\Email\"/>
  </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 all. Test this setting using the following code:
protected void btnMail_Click(object sender, EventArgs e)
{
    MailMessage message = new MailMessage( 
        "FromEmail@somedomain.com",
        "ToEmail@abcdefgh.com",
        "Email Subject - 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.
Hope this will be useful in your development, just make sure that if you are using IIS, it should have read-write permissions to this folder.

You may download the attached file containing source code. 
I welcome your comments 


Newer Posts Older Posts

Contact Me

© Copyright Source Code World | Designed By Code Nirvana
Back To Top