The. NET Framework 2.0 provides APIs for accessing settings in a configuration file. Here's how you access the mail settings of a Config File programmatically:
C #
using system. configuration; using system. web. configuration; using system. net. configuration; configuration configurationfile = webconfigurationmanager. openwebconfiguration ( "pathtoconfigfile" ); mailsettingssectiongroup mailsettings = configurationfile. getsectiongroup ( "system.net/mailsettings" ) as mailsettingssectiongroup; If (mailsettings! = null ) { int Port = mailsettings. SMTP. network. port; string host = mailsettings. SMTP. network. host; string Password = mailsettings. SMTP. network. password; string username = mailsettings. SMTP. network. username ;}
VB. NET
Imports System. Configuration Imports System. Web. Configuration Imports System. net. Configuration Dim Configurationfile As Configuration = webconfigurationmanager. openwebconfiguration ( "Pathtoconfigfile" ) Dim Mailsettings As Mailsettingssectiongroup = configurationfile. getsectiongroup ( "System.net/mailsettings" ) If Not Mailsettings Is Nothing Then Dim Port As Integer = Mailsettings. SMTP. Network. Port Dim Host As String = Mailsettings. SMTP. Network. Host Dim Password As String = Mailsettings. SMTP. Network. Password Dim Username As String = Mailsettings. SMTP. Network. Username End If