System.Net.Mail FAQ

Complete FAQ Listing


4.1 How do I read SMTP configuration data? Printer Friendly

Using special settings in your app's configuration file, System.Net.Mail can configure itself.

Using the configuation file, you can set the following default properties:

MailMessage.From
SmtpClient.Host
SmtpClient.Port
 

The settings will also allow you specify a Username and Password to be used for authentication. It is important to note that if DefaultCredentials are specified to true, the userName and password attributes are igorned.

Here is an example of the  node:

 

<configuration>
  <system.net>
    <mailSettings>
      <smtp from="me@mycompany.com">
        --
            The node supports the following properties, but we won't use all of them
           
        --
>
        <network host="127.0.0.1" />
       smtp>
    mailSettings>
   system.net>
configuration>

 


If you need more flexibility, you can set individual SMTP settings for each page. For example:
    <location path="SomeDirectory/Page1.aspx">
        <system.net>
            <mailSettings>
                <smtp deliveryMethod="Network" from="admin@example.com">
                    <network defaultCredentials="true" host="smtp.example.com" port="25" userName="admin@example.com" password="password"/>
                </smtp>
            </mailSettings>
        </system.net>
    </location>
    <location path="SomeDirectory/Page2.aspx">
        <system.net>
            <mailSettings>
                <smtp deliveryMethod="Network" from="sales@example.com">
                    <network defaultCredentials="true" host="smtp.example.com" port="25" userName="sales@example.com" password="password"/>
                </smtp>
            </mailSettings>
        </system.net>
    </location>
    <location path="SomeDirectory/Page3.aspx">
        <system.net>
            <mailSettings>
                <smtp deliveryMethod="Network" from="info@example.com">
                    <network defaultCredentials="true" host="smtp.example.com" port="25" userName="info@example.com" password="password"/>
                </smtp>
            </mailSettings>
        </system.net>
    </location>