im in ur web, enriching ur code

 
 

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.


kick it on DotNetKicks.com

Posted on Wednesday, November 28, 2007 3:34 AM
Filed Under [ C#, Tips, .NET, ASP.NET ]

Comments

Gravatar
# re: Using an smtp pickup directory for ASP.NET development
Posted by David Roberts
Posted on 12/20/2007 3:59 PM
Thank you.
Gravatar
# re: Using an smtp pickup directory for ASP.NET development
Posted by Ed
Posted on 2/15/2008 5:33 PM
I always use this to eliminate #3. However, I just found out that on our production server, IIS SMTP mails (from ASP.NET email dumps) are marked as spam most especially with Yahoo recipients. What do you use in your production/staging servers?
Gravatar
# re: Using an smtp pickup directory for ASP.NET development
Posted by Yuvan
Posted on 5/14/2008 2:59 AM
Thanks man...your artilce is clean and simple and to the point.Helped me to understand the delivery method in detail.
Gravatar
# re: Using an smtp pickup directory for ASP.NET development
Posted by Kimm
Posted on 8/19/2008 11:50 PM
I've always used the pickup directory, One little drawback to the "pickupnotsend" approach - usually i send out to different clients like hotmail, yahoo mail, outlook, oulook express etc. both in HTML and plain text to see if the content is diplayed as expected.

Well - you can monitor the flow of your mails by inspecting the folder, but you cannot inspect how the clients will display the content without actually sending the mails - So back to scratch - you still need some kind of server to actually send the EMLs.
Gravatar
# re: Using an smtp pickup directory for ASP.NET development
Posted by Fluxtah
Posted on 8/23/2008 11:48 PM
Never knew this, thanks! Saves having to run IIS on my dev machine.
Gravatar
# re: Using an smtp pickup directory for ASP.NET development
Posted by Dave Transom
Posted on 8/24/2008 1:37 PM
@Fluxtah:
Using email features suddenly got a lot easier once I realised this - Thanks Vista, wish I knew this when I was back on XP Pro ;)

@Kimm:
I'd see testing the content of email in different clients (hotmail, gmail, outlook, thunderbird, etc...) as a specific task to be undertaken.

Where as during normal development, you want to make sure that, a) the email is generated, and b) it has the right data in it (order line items, personalisations etc...)

So it does the job in that respect. Testing the design is another matter, where you'd _need_ actually send it. But that needs to be controlled, so you don't send email to non-test users.

Changes that come from this kind of testing are usually design tweaks, as opposed to anything high impact - unless it's your first rich content email, and you've just realised that most clients don't support or honour a lot of html and/or styles - so I would say it's okay to push this to your _staging_ server to finalise before going live. It's perhaps a little inconvenient for that single point only, but everything else is all pluses.

Gravatar
# re: Using an smtp pickup directory for ASP.NET development
Posted by arjan
Posted on 10/10/2008 8:35 AM
I agree, but it does not work in Medium Trust, you will get a FileIOPermission exception.
Gravatar
# re: Using an smtp pickup directory for ASP.NET development
Posted by Steve
Posted on 2/2/2010 2:57 AM
I tried. I am sending to a group using code below and then sending message-email did get dumpded to local directory but only one e-mail. not each email. Any thoughts?
If objDR.HasRows = True Then
lblEmails.Text = "" ' make sure it is reset
While objDR.Read()
If IsDBNull(objDR("email")) = False Then
recipient = CType(objDR("email"), String)
If recipient <> "" And recipient <> "steve@sfcas.com" Then
recipientcount = recipientcount + 1
If recipientcount - 1 >= intLowLim And recipientcount - 1 <= intUpLim Then

message.Bcc.Add(recipient)
Gravatar
# re: Using an smtp pickup directory for ASP.NET development
Posted by Dave Transom
Posted on 2/2/2010 10:45 AM
@Steve: From the code you've given, it sounds like you're only creating one email and adding multiple recipients to it. Calling "message.Bcc.Add" simply adds more addresses. When it is 'dropped' to the pickup folder, there will be one email file, but you won't see who it's BCC'd to, because well, its a blind carbon copy.

If you view the file with a text editor (rather than outlook/outlook express, open with notepad), ALL the addresses should show up in the "X-Receiver" header at the top of the file.

However, if you want to send one separate email to each address (without the others knowing about it), you should create a new MailMessage each time, set the To address, and then call send.
Gravatar
# re: Using an smtp pickup directory for ASP.NET development
Posted by Paul Bolejack
Posted on 11/12/2011 7:09 AM
With respect to the FileIOPermission error under medium trust, you can modify the medium trust config file (web_mediumtrust.config) on your development machine to give file IO permission on your drop directory. My testing indicated that only read and write were needed. So, for example, the new permission would be:
<IPermission class="FileIOPermission" version="1" Read="C:\SmtpMailDrop;$AppDir$" Write="C:\SmtpMailDrop;$AppDir$" Append="$AppDir$" PathDiscovery="$AppDir$"/>


Gravatar
# re: Using an smtp pickup directory for ASP.NET development
Posted by Lincoln
Posted on 4/24/2012 1:47 AM
thank you, the explanation is great and the code is sweet.
Gravatar
# re: Using an smtp pickup directory for ASP.NET development
Posted by Mike
Posted on 9/26/2012 10:02 PM
You can rename your .eml files with something like:
var _emailPickUpDirectory = "d:\temp\emails\";
var _latestEmail = new DirectoryInfo(_emailPickUpDirectory).GetFiles().OrderByDescending(file => file.LastWriteTime).First();
File.Move(_latestEmail.FullName, Path.Combine(_emailPickUpDirectory, "NewDynamicName.eml"));
Gravatar
# re: Using an smtp pickup directory for ASP.NET development
Posted by Hrshad
Posted on 12/12/2012 11:48 PM
Good Article.

I want to read emails from folder and send it using schduler c# code, do you have any code for this?


Please send me on my email ID
Gravatar
# re: Using an smtp pickup directory for ASP.NET development
Posted by Dave Transom
Posted on 12/13/2012 12:20 AM
@Mike: You might run into issues where multiple files are written to the drop folder from different threads - though in a development environment, if you're doing it directly after SmtpClient.Send() it _should_ be okay, but means you have to convolute your code by adding this. So I wouldn't bother.

@Hrshad: No, I haven't had a need for this. It shouldn't be hard to write another little c# app that has real (Network) settings for the mail server. But I'm not sure if there is support out of the box for reading .eml files and resending. I'm not sure what you're intending, but I would leave this as development only, and when deploying to production send directly via network. It feels like jumping through hoops otherwise.
Gravatar
# re: Using an smtp pickup directory for ASP.NET development
Posted by developer
Posted on 2/14/2013 5:05 PM
when i implemented the code .eml file is stored into the specified path but we are not getting the email. Could you please help me.
Gravatar
# re: Using an smtp pickup directory for ASP.NET development
Posted by Dave Transom
Posted on 2/14/2013 5:17 PM
@developer:
I think you might misunderstand the purpose of this. The main reason is so email is _not_ sent over the network - it avoids sending emails in development to the wrong people, and you can review _all_ the emails to verify the content is correct in a controlled environment.

The .eml files will stay on the file system, ready for you to open them with some kind of reader (I use the built-in Windows Live Mail).
Gravatar
# re: Using an smtp pickup directory for ASP.NET development
Posted by developer
Posted on 2/14/2013 5:25 PM
yaaa but could you please help me how to send the smtp mails with queueing option.
Gravatar
# re: Using an smtp pickup directory for ASP.NET development
Posted by fdgfd
Posted on 7/2/2014 10:00 PM
gthfg

Post Comment

Title *
Name *
Email
Url
Comment *  
Please add 5 and 2 and type the answer here:

Recently on C# Vitamins...

Powered By Subtext

 

About C# Vitamins

Dave has been working in the industry for around 14 years, and has a focus on Javascript, C#, ASP.NET and SQL Server web development; not to mention being a standards driven type of guy.

C# Vitamins is the result of his findings while working in the web industry and a desire to share with the community; and if it was traced back far enough, you might say it might not have existed if he hadn't taken such an interest in id Software's original Quake.

Related Links

Below is a list of related links of Dave's other sites.