Tuesday, May 11, 2010

Accessing Config File Mail Settings Programmatically

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:

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;
}
===============================================================

object path = "~/WebRegAdmin";
Configuration config = WebConfigurationManager.OpenWebConfiguration(HttpContext.Current.Request.ApplicationPath);

ConfigurationSection appSettings = config.GetSection("connectionStrings");
if (bEncrypt)
{
appSettings.SectionInformation.ProtectSection "DataProtectionConfigurationProvider");
}
else
{
appSettings.SectionInformation.UnprotectSection();
}
config.Save();

No comments:

Post a Comment