Sending E-Mail from ASP.NET - Configuring the SMTP Server

One of the big improvements in the configuration and maintenance of ASP.NET Web sites for version 2.0 is the ability to define common settings such as connection strings and mail server in a structured section of the web.config file. I previously made an entry explaining how to send an e-mail in ASP.NET and wanted to do a quick follow up on configuring the SMTP server.

In my previous entry I stated you needed to pass the SMTP server and showed example code to this effect. This is not necessarily true in ASP.NET 2.0. While you have the flexibility to define a specific SMTP server when you send an e-mail you can use a default SMTP server defined in the site's web.config file.

  <system.net>
    <mailSettings>
      <smtp>
        <network host="SMTP.MyDomain.com"/>
      </smtp>
    </mailSettings>
  </system.net>

You can add a system.net section at any top level position in the web.config file and add a mailSettings section to it. Here you can define the SMTP server as displayed in the smtp section. The SMTPClient class will use this server by default if no SMTP server is explicitly passed to the constructor or the Host property.

Share This Article With Your Friends!