Send SmtpClient Mails to Disk

When developing a system that sends mails, often the mails shouldn’t be sent for real when testing. Instead they should be made available for investigation. Fortunately, that functionality is built in with the .NET SmtpClient.

There is even no need to change the code. It’s just a matter of configuration. Add the following lines to the app.config (or web.config for web applications)

<system.net>
  <mailSettings>
    <smtp deliveryMethod="SpecifiedPickupDirectory" from="[email protected]">
      <specifiedPickupDirectory pickupDirectoryLocation="c:\temp" />
      <!-- The network host setting isn't used, but without it an exception
      occurs when disposing of the SmtpClient.-->
      <network host="localhost"/>
    </smtp>
  </mailSettings>
</system.net>

The pickup directory setting is meant to be used with a local mail server that watches a directory for new mails. I have no mail server watching my c:\temp directory. Instead, the mails are just dropped there as .eml-files that can be opened using a mail program (e.g. outlook).

  • Leave a Reply

    Your name as it will be displayed on the posted comment.
    Your e-mail address will not be published. It is only used if I want to get in touch during comment moderation.
    Your name will be a link to this address.
Software Development is a Job – Coding is a Passion

I'm Anders Abel, an independent systems architect and developer in Stockholm, Sweden.

profile for Anders Abel at Stack Overflow, Q&A for professional and enthusiast programmers

Code for most posts is available on my GitHub account.

Popular Posts

Archives

Series

Powered by WordPress with the Passion for Coding theme.