Read SMTP Settings From Configuration File

sourceSource Code ondemand_videoVideo

Reading SMTP settings from a configuration file (e.g. app.config, web.config, etc.) is a simple process. This enables you to use the same settings across your entire project and only have to apply updates in one place.

Overview

There are just two basic steps in the process:

  1. Gather the required SMTP server information for your system. This information includes the server address, port number, SSL requirements, and username/password (if needed).
  2. Add the SMTP settings to the app.config or web.config as a child of the configuration node.

Basic Example

Assuming that I have already gathered the required SMTP server information (I’ll use dummy values in this example), I am now going to add these setting as children of the configuration node in the configuration file:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>

  <!-- Below are the SMTP settings -->
  <system.net>
    <mailSettings>
      <smtp from="fromAddress@domain.com">
        <network host="smtpEmailServerAddress" port="25" enableSsl="true"
            userName="username" password="password"/>
      </smtp>
    </mailSettings>
  </system.net>

</configuration>

Now that we have these setting is place, when I debug the code below we should see that the values from the coniguration file are being used to populate the properties in the SmtpClient and MailMessage objects:

using (SmtpClient client = new SmtpClient())
using (MailMessage message = new MailMessage())
{
    message.To.Add("toAddress@domain.com");
    message.IsBodyHtml = true;
    message.Subject = "Subject";
    message.Body = "Body";

    try
    {
        // send the email
        client.Send(message);
    }
    catch (SmtpException ex)
    {
        // log exception
    }
}

Below is a screenshot of the Locals window in Visual Studio while debugging and hitting a breakpoint at the message.To.Add("toAddress@domain.com"); line above (see highlighted items below):

SMTP debug session

Final Thoughts