如何以编程方式将 SMTP 服务器详细信息存储(保存)回 web.config

发布于 2024-09-30 12:33:56 字数 665 浏览 2 评论 0原文

搜索 StackOverflow,我发现 这个关于如何检索 SMTP 的问题Web.Config 中的设置,但没有有关如何将 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 呢?

Searching StackOverflow, I found this question on how to Retrieve SMTP settings from Web.Config, but no details on how to update the SMTP back to the 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 clued in by Intellisense that SmptSection.Network is a Get (aka "read-only") accessor.

So how am I supposed to programmatically write my SMTP data back to web.config?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

盛装女皇 2024-10-07 12:33:56

你应该能够做这样的事情,这有效吗?:

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();

You should be able to do something like this, does this 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();
人心善变 2024-10-07 12:33:56

看看这篇文章: 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;
}

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

Looks like you need fairly high access (permissions) though.

Specifically 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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文