Wednesday, May 30, 2012

Make Windows 7 and IIS 7.5 Think You Have an SMTP Server When You Don’t

I am coming up to speed on all the features available in my employer’s main software product. One of those features is the ability to send email to users for various reasons. The application is ASP.Net based and uses the System.Net.Mail.SmtpClient object. The code is setting the DeliveryMethod value to Net.Mail.SmtpDeliveryMethod.PickupDirectoryFromIis. This works fine on Windows 2008 because there is a way to install the SMTP server, but is a problem for me.  I’m running Windows 7 64 bit with IIS 7.5, which does not have an SMTP server available. Using IIS Manager you can set the SMTP E-Mail settings to “Store e-mail in pickup directory” and provide a directory where you would like to have your email files dropped. That all sounds fine, but then you discover that you get an error when you try to run your code, “Cannot get IIS Pickup Directory.” The reason is that the Metabase doesn’t contain the SmtpSvc key, nor any of its children.

In order to fix this you will need “Metabase Explorer” which is part of the IIS 6.0 Resource Kit. Once you have Metabase Explorer running you can add the missing keys and grant permissions. In Metabase Explorer (you will have to right click it and run it as administrator) you will see something similar to this screen shot;



You should not see the two keys that are indicated by red arrows. You will be adding these, but first let's make a backup of the LM key and its children;

  1. Right click LM->Export to file
  2. Select a folder where you want your backup
  3. Enter a backup filename
  4. Click Save
  5. Enter a password and confirm.
Now we can add the new keys;
  1. Right click LM->New->Key
  2. Give it the name “SmtpSvc” and hit enter.
  3. Right click SmtpSvc->New->Key
  4. Give it the name “1” and hit enter.
  5. Right click 1->New->String Record
  6. In Record Name or Identifier, type “PickupDirectory” and click OK.
  7. Right click PickupDirectory->Properties.
  8. In Value Data, enter the folder where you want your emails dropped. In my case that is “c:\inetpub\mailroot\pickup”
  9. Click OK.
Now you are ready to grant permissions. First you will need to determine which user the application pool is using. You will need to grant that user permission to read the new keys you just created.
  1. Right click SmtpSvc->Permissions...
  2. You will be prompted to indicate the way you want to set permissions. Clicking “No” will copy the parent permissions down to this key and let you edit them. Click “No”.
  3. Select or Add the correct user.
  4. Make sure the “Allow” check box is checked for “Read” permission.
  5. Click OK.
  6. Repeat steps 1 through 4 for the “SmtpSvc\1” key.
The only thing left is to make sure you have created the pickup directory you indicated above in step 8. Once the directory is created, you will need to make sure your application pool user also has read/write permissions on this directory.

Now you should be able to run your code and have email delivered as .eml files directly to your pickup folder, rather than being sent to an SMTP server. For my purposes this is exactly what I need, since I would rather have the email dropped locally where I can look at it, instead of being sent as a real email. For testing purposes it’s perfect because you don’t run the risk of having messages blasted out to customers when you are only testing something.