Accessing SMTP Mail Settings from Web.Config File by using c#
Just use the System.Net.Mail
classes to send your e-mails. It will automagically pick-up the Mail setting from your web.config.
You can use the WebConfigurationManager:
Configuration configurationFile = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
MailSettingsSectionGroup mailSettings = configurationFile.GetSectionGroup("system.net/mailSettings") as MailSettingsSectionGroup;
Response.Write(mailSettings.Smtp.Network.Host);
Related...If you're accessing from both a website and an application this code can come in handy.
Configuration config;
bool isWebApp = HttpRuntime.AppDomainAppId != null;
if (isWebApp)
{
config = WebConfigurationManager.OpenWebConfiguration(HttpContext.Current.Request.ApplicationPath);
}
else
{
config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
}
var mailSettings = config.GetSectionGroup("system.net/mailSettings") as MailSettingsSectionGroup;