System.Net.Mail FAQ

Complete FAQ Listing

3.5 Accessing Config File Mail Settings Programmatically Printer Friendly

Some people may not know that .NET 2.0 provides APIs for accessing everything in a configuration file. The most common question I see relates to accessing the mail settings in the node programmatically.

Special thanks to Ryan Olshan for this one.

[ C# ]

System.Net.Configuration.MailSettingsSectionGroup mMailSettings;

int mPort = mMailSettings.Smtp.Network.Port;
string mHost = mMailSettings.Smtp.Network.Host;
string mPassword = mMailSettings.Smtp.Network.Password;
string mUsername = mMailSettings.Smtp.Network.Username;

[ VB.NET ]

Dim mMailSettings As System.Net.Configuration.MailSettingsSectionGroup

Dim mPort As Integer = mMailSettings.Smtp.Network.Port
Dim mHost As String = mMailSettings.Smtp.Network.Host
Dim mPassword As String = mMailSettings.Smtp.Network.Password
Dim mUsername As String = mMailSettings.Smtp.Network.Username