smtpClient不读取Web.config

Ger*_*rdV 4 c# web-config smtpclient asp.net-mvc-4

在我的MVC4应用程序中,我正在尝试使用Web.config发送邮件以进行配置设置.我有剪纸作为模拟SMTP服务器运行.当我尝试使用smtpClient并在我的代码中设置host ="localhost"(也就是说,不是通过Web.config)时,一切正常.

我的Web.config

  <system.net>
    <mailSettings>
      <smtp deliveryMethod="Network" from="someone@somewhere.net">
        <network
           host="localhost"
           userName=""
           password=""
           defaultCredentials="true" />
      </smtp>
    </mailSettings>
  </system.net>
Run Code Online (Sandbox Code Playgroud)

我的代码

SmtpClient client = new SmtpClient();
client.Send(myEmailObject);
Run Code Online (Sandbox Code Playgroud)

错误

未指定SMTP主机

小智 12

ASP.NET MVC应用程序中有多个web.config.为了使大多数系统相关设置生效,必须将它们放在应用程序的根web.config中.这意味着服务器将其视为"〜/ web.config"并且位于Visual Studio解决方案的根文件夹中.