Friday, August 28, 2009

Read - Write SMTP settings Web.Config with Asp.net runtime

//Read SMTP Settings

System.Configuration.Configuration config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(HttpContext.Current.Request.ApplicationPath);
System.Net.Configuration.MailSettingsSectionGroup settings = (System.Net.Configuration.MailSettingsSectionGroup)config.GetSectionGroup("system.net/mailSettings");
txtSmtpHostAddr.Text = settings.Smtp.Network.Host;
txtSmtpUserName.Text = settings.Smtp.Network.UserName;
txtSmtpPortNo.Text = settings.Smtp.Network.Port.ToString();
txtSmtpEmail.Text = settings.Smtp.From;

//Write SMTP Settings

System.Configuration.Configuration config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(HttpContext.Current.Request.ApplicationPath);
System.Net.Configuration.MailSettingsSectionGroup settings = (System.Net.Configuration.MailSettingsSectionGroup)config.GetSectionGroup("system.net/mailSettings");
settings.Smtp.Network.Host = txtSmtpHostAddr.Text.Trim();
settings.Smtp.Network.UserName = txtSmtpUserName.Text.Trim();
settings.Smtp.Network.Password = txtSmtpPassword.Text.Trim();
settings.Smtp.Network.Port = txtSmtpPortNo.Text == "" ? 25 : Convert.ToInt32(txtSmtpPortNo.Text.Trim());
settings.Smtp.From = txtSmtpEmail.Text.Trim();
config.Save();

No comments:

Post a Comment