如何以编程方式将SMTP服务器详细信息存储(保存)回web.config [英] How to programmatically store (save) SMTP server details back to web.config

查看:179
本文介绍了如何以编程方式将SMTP服务器详细信息存储(保存)回web.config的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我从以下代码开始:

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

但是很快被Intellisense认为SmptSection.Network是Get(也称为只读")访问器.

but was quickly clued in by Intellisense that SmptSection.Network is a Get (aka "read-only") accessor.

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

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

推荐答案

您应该可以执行以下操作,这行得通吗?

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

这篇关于如何以编程方式将SMTP服务器详细信息存储(保存)回web.config的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆