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

<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.

ASP.NET Email SMTP

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






About The Author

Suprotim Agarwal
Suprotim Agarwal, Developer Technologies MVP (Microsoft Most Valuable Professional) is the founder and contributor for DevCurry, DotNetCurry and SQLServerCurry. He is the Chief Editor of a Developer Magazine called DNC Magazine. He has also authored two Books - 51 Recipes using jQuery with ASP.NET Controls. and The Absolutely Awesome jQuery CookBook.

Follow him on twitter @suprotimagarwal.

10 comments:

David said...

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

Anonymous said...

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

.net application development said...

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.

Anonymous said...

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"
Else
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

MyBase.Send(mm)
End Sub

Anonymous said...

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.

Suprotim Agarwal said...

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

EugeneK said...

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.

Anonymous said...

@EugeneK: Take it easy man!

Alan J said...

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

శశి కుమార్ said...

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