4

搜索 StackOverflow,我发现了这个关于如何从 Web.Config 检索 SMTP 设置的问题,但没有关于如何将 SMTP 更新回 web.config 文件的详细信息。

我从以下代码开始:

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

但很快被 Intellisense 发现,它SmptSection.Network是一个 Get(又名“只读”)访问器。

那么我应该如何以编程方式将我的 SMTP 数据写回 web.config 呢?

4

2 回答 2

4

你应该能够做这样的事情,这行得通吗?:

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();
于 2010-11-04T19:25:11.270 回答
0

看看这篇文章:http ://www.west-wind.com/WebLog/posts/8461.aspx

不过,看起来您需要相当高的访问权限(权限)。

具体来自文章:

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;
}
于 2010-11-04T19:05:01.697 回答