Standardised SMTP Configuration for .NET 30 December 2009

One handy feature of .NET that I keep forgetting is that you can set up default SMTP settings in your App.config/Web.config files, without any code. You could even put this in your machine.config, so that every application always has correct SMTP settings by default.

In your (web|app|machine).config file, add the following bit of XML:

<smtp deliveryMethod="Network" from="">
<network host="" port="25" />

You can also add authentication settings to the network element.

This means that in your code, when a new SmtpClient (and MailMessage for that matter) is created, it will be prepopulated with these settings, thus standardising the setup.


Copyright © Daniel Hill.