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:

<configuration>
<system.net>
<mailSettings>
<smtp deliveryMethod="Network" from="fromaddress@example.com">
<network host="mailhost.example.com" port="25" />
</smtp>
</mailSettings>
</system.net>

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.

Older

Copyright © Daniel Hill.