Using an smtp pickup directory for ASP.NET development
I don't know why this never occurred to me before, but using
SpecifiedPickupDirectory
as the SmtpDeliveryMethod for sending email
while developing ASP.NET websites is a very good idea.
I was recently 'forced' to start using a mail pickup directory for development when I upgraded to Vista (yes I said upgraded) and found out that IIS7 doesn't ship with an SMTP Server. Many blog posts out there recommend using third party software like SmarterMail, Free SMTP Server or other cheap shareware products, but if it's for your development machine I say: don't do it!
Why not simply configure your mail settings to use a pickup directory?
- All email is written as an .eml file to the folder you specify.
- Peace of mind that your system in development isn't going to send your client an unintended email.
- No more waiting for your test emails to be delivered . Network problems and latency disappear.
- You can monitor every email your application is sending via a folder. Okay, you have log files for this in SMTP, but peeking into a folder is pretty convenient during development.
- Your inbox won't get cluttered with numerous development emails, they just sit in the pickup folder ready for you to view and/or delete.
- No more fake or catch-all email addresses to maintain. Because each email is dumped to the folder, you just open it from the file system.
- Store your mail in a separate folder for each website for easier use.
- Order the files in your pickup directory by "Date Modified" to see the latest messages that have arrived as you'll most probably want to check the last few messages as you develop.
And of course we have some cons to our pros... well, a "con" at least.
- Files use a GUID as their filename - not very helpful as you have to open the message (or wait for the summary to be extracted as a tool tip) to view the subject line, sender and recipient.
Of course, this concept really only applies to your development machine. You may want set up an SMTP server for your staging servers (and obviously your production servers ;), but your dev PC is personal - keep it that way.
Need further convincing?
Argument #1: Mass Mail
Imagine developing a newsletter application, and you need a few thousand addresses to test load (and that funky status widget you built to monitor the send progress). You don't want to have to go and download the few thousand emails to verify them. Look in your pickup folder. Count the number of emails produced. Open a few and inspect the contents. Look for differences in file size; it may give you a clue that something has gone awry.
Argument #2: Testing with a copy of the production database, or at least, some semi-real data
So you've decided to download a copy of the production database so you can test your new features with some real data. You don't want all that new email functionality sending the real recipient your half completed development emails. Look at the pickup directory for the emails of the user your testing against - exactly the way they would see them.
Argument #3: Waiting
From my experience, sending mail from within our development environment, to an outside network takes a wee bit of time. Usually, production environment is fairly instant for sending mail, and that's great. But in dev mode, you might have to hit your send & receive button a few times. Maybe you receive the email, maybe you don't. You hit send & receive a few more times, come to the conclusion something is b0rked, the mail didn't send or the outside network went down. Execute the step to send the email again, hit send & receive only to get both emails. sigh. Again, it's simple, after you've completed the step that sends the email, have a peek at the new file in the pickup folder.
If you're now convinced, you may pass go, collect $200 and move on to the configuration settings.
Mail Settings Configuration
Configuration is simple, just change the delivery method to SpecifiedPickupDirectory, and add the location to the folder you want to use to store your mail in as per the example below. You do have to enter the full path of the pickup directory, and the directory must exist, but that's really the only caveat.
<system.net>
<mailSettings>
<smtp deliveryMethod="SpecifiedPickupDirectory" from="no-reply@mydomain.com">
<specifiedPickupDirectory pickupDirectoryLocation="C:\Development\MyWebApp\Mail" />
</smtp>
</mailSettings>
</system.net>
And that's about all there is to it. Once you let go of your old ways and adopt the pickup directory, it becomes a pretty reliable way to get your development done. I hope this helps.
[UPDATE] See my follow on post Programmatically setting the SmtpClient pickup directory location at runtime, for another helpful development technique.