How to programmatically save (save) SMTP server data back to web.config

Searching StackOverflow, I found this question on how to restore SMTP settings from Web.Config , but there are no details on how to update SMTP to a web.config file.

I started with the following code:

Configuration webConfig = WebConfigurationManager.OpenWebConfiguration("~"); MailSettingsSectionGroup settings = (MailSettingsSectionGroup)webConfig.GetSectionGroup("system.net/mailSettings"); SmtpSection smtp = settings.Smtp; SmtpNetworkElement net = smtp.Network; 

but was quickly associated with Intellisense that SmptSection.Network is a Get accessory (aka read-only).

So, how should I programmatically write SMTP data back to web.config?

+4
source share
2 answers

You should do something like this, does it work ?:

 Configuration webConfig = WebConfigurationManager.OpenWebConfiguration("~"); MailSettingsSectionGroup settings = (MailSettingsSectionGroup)webConfig.GetSectionGroup("system.net/mailSettings"); SmtpSection smtp = settings.Smtp; SmtpNetworkElement net = smtp.Network; net.Port = 25; net.Host = "localhost"; webConfig.Save(); 
+4
source

Take a look at this article: http://www.west-wind.com/WebLog/posts/8461.aspx

It looks like you need pretty high access (permissions).

In particular, from the article:

 protected void Page_Load(object sender, EventArgs e) { Configuration config = WebConfigurationManager.OpenWebConfiguration("~"); wwDbResourceProviderSection Section = config.GetSection("wwDbResourceProvider") as wwDbResourceProviderSection; Section.ShowControlIcons = true; config.Save(); return; } 
0
source

All Articles