SmtpClient 和 app.config system.net 配置 [英] SmtpClient and app.config system.net configuration
问题描述
我正在开发用于发送电子邮件的 .NET 3.5 库时遇到问题.我把 system.net
配置放到 app.config
中:
I'm having an issue with a .NET 3.5 library I'm developing to send emails.
I put the system.net
configuration into app.config
:
<system.net>
<mailSettings>
<smtp from="mail@domain.com">
<network host="myserver.com" port="25" defaultCredentials="true" />
</smtp>
</mailSettings>
</system.net>
我不带参数实例化了 SmtpClient:
And I instantiate the SmtpClient without params:
SmtpClient client = new SmtpClient();
但未读取配置(我正在尝试使用 NUnit 测试库)并且我收到 System.InvalidOperationException
,因为未读取配置,因此主机为空.
But the configuration is not read (I'm trying to test the library with NUnit) and I get a System.InvalidOperationException
, because the configuration is not read and thus the host is null.
不应该自动读取配置吗?
Shouldn't the configuration be read automatically?
推荐答案
确保将您的配置块(如上所示)添加到 {appName}.exe.config 或 web.config - 类库的配置是在运行时取自这些文件之一,而不是取自类库的 app.config.
Make sure you add your configuration block (as shown above) to the {appName}.exe.config or web.config - the configuration for the class library is taken from one of those files at runtime, not from the app.config of the class library.
这篇关于SmtpClient 和 app.config system.net 配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!