Configure SmtpClient to write to a file

 

When developing a .Net application that sends out an email it is sometimes useful to write the emails to a local directory rather than send them to a mail server.  This is easily done in the SmtpClient configuration in App.Config.  The below configuration section will write .eml files to the directory C:\Email (which you will need to create):

<configuration>
   <system.net>
      <mailSettings>
         <smtp deliveryMethod="SpecifiedPickupDirectory">
            <specifiedPickupDirectory pickupDirectoryLocation="C:\Email" />
         </smtp>
      </mailSettings>
   </system.net>
</configuration>

 

 

Note: if you are using Outlook 2007 you may not be able to open .eml files without an extra step.  The file can be opened from the command prompt with the command

"%ProgramFiles%\Microsoft Office\Office12\OUTLOOK.EXE" /eml "<PathToEmlFile>"

or associate the .eml files with a batch file that contains the text

"%ProgramFiles%\Microsoft Office\Office12\OUTLOOK.EXE" /eml %1

A fuller description can be found at http://support.microsoft.com/kb/956693.