我的 ASP.NET 应用程序如何从 web.config 自动获取 SMTP 设置?

发布于 2024-08-31 19:18:35 字数 452 浏览 4 评论 0原文

我注意到我们总是这样:

SmtpClient mSmtpClient = new SmtpClient();
// Send the mail message
mSmtpClient.Send(mMailMessage);

设置凭据的唯一位置是在 web.config 中:

  <system.net>
    <mailSettings>
      <smtp>
        <network host="xxx.xx.xxx.229" userName="xxxxxxxx" password="xxxxxxxx"/>
      </smtp>
    </mailSettings>
  </system.net>

所以我的问题是,它如何自动将它们取出?

I noticed that we always just are like:

SmtpClient mSmtpClient = new SmtpClient();
// Send the mail message
mSmtpClient.Send(mMailMessage);

And the only place the credentials are set are in web.config:

  <system.net>
    <mailSettings>
      <smtp>
        <network host="xxx.xx.xxx.229" userName="xxxxxxxx" password="xxxxxxxx"/>
      </smtp>
    </mailSettings>
  </system.net>

So my question is, how does it automagically get them out?

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

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

发布评论

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

评论(4

丶视觉 2024-09-07 19:18:35

文档指出SmtpClient 从应用程序或计算机配置文件中读取其配置。对于 Web 应用程序,应用程序配置文件是 web.config。这也意味着,如果 Web.config 中未设置 mailSettings 元素,它将在放弃之前查找 machine.config 中的设置:

“这个构造函数初始化主机,
凭证和端口属性
新的 SmtpClient 通过使用
应用程序或机器中的设置
配置文件。”

The documentation states that the parameterless constructor of SmtpClient reads its configuration from the application or machine configuration file. For a Web application, the application configuration file is web.config. This also means that if the mailSettings element is not set in Web.config, it will look for settings in machine.config, before giving up:

"This constructor initializes the Host,
Credentials, and Port properties for
the new SmtpClient by using the
settings in the application or machine
configuration files."

虚拟世界 2024-09-07 19:18:35
var config = WebConfigurationManager.OpenWebConfiguration("Web.config");    
var settings= config.GetSectionGroup("system.net/mailSettings") as MailSettingsSectionGroup;

if (settings!= null)
{
    var port = settings.Smtp.Network.Port;
    var host = settings.Smtp.Network.Host;
    var username = settings.Smtp.Network.UserName;
    var password = settings.Smtp.Network.Password;      
}
var config = WebConfigurationManager.OpenWebConfiguration("Web.config");    
var settings= config.GetSectionGroup("system.net/mailSettings") as MailSettingsSectionGroup;

if (settings!= null)
{
    var port = settings.Smtp.Network.Port;
    var host = settings.Smtp.Network.Host;
    var username = settings.Smtp.Network.UserName;
    var password = settings.Smtp.Network.Password;      
}
予囚 2024-09-07 19:18:35

Windows 文件夹中有一个 machine.config 文件,每个网站(或应用程序)都有一个 web.config 文件(或 app.config 文件)。这些文件组合在一起即可获取应用程序域的设置。

smtp 类只需访问配置,可能通过 ConfigurationManager 类< /a>

There is a machine.config file in your windows folder, and each web site (or application) has a web.config file (or an app.config file). These files are combined to get the settings for the app domain.

The smtp class simply accesses the configuration, probably through the ConfigurationManager Class

薆情海 2024-09-07 19:18:35

优秀的答案德里斯。我希望我有足够的声誉来升级你的答案,但我没有:(

无论如何,我提供了类似的答案,尽管它是像Abatishchev所示手动完成的。唯一的区别是我使用不可访问的atm的enableSsl解决了问题

在此处查看文章主题。

Excelent answer Driis. I wish i had enough reputation to uprate your answer, but i dont :(

Anyway, i provided an answer to something similar, although it's done manually like Abatishchev shows. Only difference is that i solved the issue with the enableSsl that are not accesible atm.

See article thread here.

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