SmtpClient throws InvalidOperationException when using SpecifiedPickupDirectory

I was asked today to amend the settings for a bulk email application I’ve recently written - using the element in the web.config, I was specifying an SMTP server, however the requirement now is to specify a pickup directory, and to allow IIS to handle the email sending process.

Pretty simple change I though - amend the deliveryMethod to SpecifiedPickupDirectory and provide a <specifiedPickupDirectory/> element with the path to the pickup directory that had already been configured:

smtp settings incorrect

However causes an exception to be thrown. After some head scratching and a bit of Googling, it appears that this is a bug in the .NET 4.0 framework. When the SMTP Client object is defined in a using block


you must define the <network/> attribute after the <specifiedPickupDirectory/>, even though you don’t want to use it:

smtp settings correct