问题描述
我正在开发一个发送电子邮件的.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="[email protected]">
<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.
不应该配置被自动读取
推荐答案
确保将配置块(如上所示)添加到{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配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!