Test Emails in ASP.NET without a 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

smtp deliveryMethod="SpecifiedPickupDirectory">
specifiedPickupDirectory pickupDirectoryLocation="C:\Mails\"/>

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",
"Newsletter", "This is a test mail");

SmtpClient client = new SmtpClient("localhost");

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.

Will you give this article a +1 ? Thanks in advance

About The Author

Suprotim Agarwal, ASP.NET Architecture MVP works as an Architect Consultant and provides consultancy on how to design and develop Web applications.

Suprotim is also the founder and primary contributor to DevCurry, DotNetCurry and SQLServerCurry. He has also written an EBook 51 Recipes using jQuery with ASP.NET Controls.

Follow him on twitter @suprotimagarwal


10 Responses to "Test Emails in ASP.NET without a Mail Server"
  1. David said...
    March 18, 2011 at 5:53 AM

    Nice trick indeed. So far I used Papercut simple SMTP server tool (http://papercut.codeplex.com/) to test email sending.

  2. Anonymous said...
    March 21, 2011 at 6:48 AM

    Thanks for the tip... I'm sure this little trick will come in handy sometime in the future

  3. .net application development said...
    March 24, 2011 at 11:50 PM

    Read your article and find more ideas that I will start to work on hope to work with me...Great advice, and helpful for us.
    Logo design service.

  4. Anonymous said...
    April 11, 2011 at 2:42 PM

    I use this trick in my sites but I subclass the SmtpClient class and do it from there based on the URL being requested. The Environment part comes from a static helper method which looks at the URL via a servervariable).

    The advantages are that you never have to worry about accidentally sending an email from production or dumping production emails to a folder for a day until someone complains! Plus, it's yet another place where you don't have to manage a separate settings and/or config files between environments. It's use-it-and-forget-it.

    Public Shadows Sub Send(ByVal mm As System.Net.Mail.MailMessage)
    If Environment.Environment = EnvironmentEnum.Production Then
    Me.DeliveryMethod = Net.Mail.SmtpDeliveryMethod.Network
    Me.Host = "mysmtphost"
    Me.DeliveryMethod = Net.Mail.SmtpDeliveryMethod.SpecifiedPickupDirectory
    Me.PickupDirectoryLocation = "c:\temp\mail"
    If Not Directory.Exists("c:\temp\mail\") Then Directory.CreateDirectory("c:\temp\mail\")
    End If

    End Sub

  5. Anonymous said...
    April 11, 2011 at 2:45 PM

    Me again... and you can very easily and quickly double-click the .eml file and see your email in all it's glory (particularly HTML emails) in Outlook Express or whatever MS is calling it this week. VERY nice when trying to nail down your email visuals and making a number of visual tweaks.

  6. Suprotim Agarwal said...
    April 11, 2011 at 11:28 PM

    Nice tip anonymous. Yes the .eml files are very helpful + you can run all sort of queries on that folder like group mails by date by using a simple LINQ query http://www.devcurry.com/2010/07/list-all-files-in-net-40-based-on.html

  7. EugeneK said...
    April 11, 2011 at 11:46 PM

    What the point to view it in raw text? The point of checking mails is that you can see HTML after it been altered with some data from database.

  8. Anonymous said...
    April 27, 2011 at 2:18 PM

    @EugeneK: Take it easy man!

  9. Alan J said...
    October 2, 2011 at 12:18 AM

    How do we send a mail to a webserver like gmail.com from our developed website?

  10. శశి said...
    May 17, 2012 at 8:51 PM

    Hi Aggarwal, very nice article, good info, thanx for sharing


Copyright © 2009-2014 All Rights Reserved for DevCurry.com by Suprotim Agarwal | Terms and Conditions