Saturday, August 9, 2008

SMTP settings in config

Did you know that you can move the SMTP settings of your .NET app to the configuration file too? No, I don't mean reading out of AppSettings and writing code to read from it again - it's a .NET feature.

When you instantiate the SmtpClient class, you can call the contructor that accepts no parameters. This causes a read from the config file, which has the SMTP settings defined as follows:

<?xml version="1.0"?>
<configuration>
<system.net>
<mailSettings>
<smtp>
<network host="" port="" userName="" password=""/>
</smtp>
</mailSettings>
</system.net>
</configuration>

It's great for the system administrators too as they now have a standard way to configure SMTP, provided that developers decide to use this feature.

EDIT: I'm sorry about the missing code extract... Blogger.com removed the XML tags from the post.

No comments: