我的 ASP.NET 应用程序如何从 web.config 自动获取 SMTP 设置?
我注意到我们总是这样:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
文档指出SmtpClient 从应用程序或计算机配置文件中读取其配置。对于 Web 应用程序,应用程序配置文件是 web.config。这也意味着,如果 Web.config 中未设置 mailSettings 元素,它将在放弃之前查找 machine.config 中的设置:
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:
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
优秀的答案德里斯。我希望我有足够的声誉来升级你的答案,但我没有:(
无论如何,我提供了类似的答案,尽管它是像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.