Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

smtpClient doesn't read the Web.config

In my MVC4 application, I'm trying to get mail sent using Web.config for configuration settings. I have papercut running as a mock SMTP server. When I try to use smtpClient and set host="localhost" in my code (that is, not through Web.config) everything works just fine.

My Web.config

  <system.net>
    <mailSettings>
      <smtp deliveryMethod="Network" from="[email protected]">
        <network
           host="localhost"
           userName=""
           password=""
           defaultCredentials="true" />
      </smtp>
    </mailSettings>
  </system.net>

My code

SmtpClient client = new SmtpClient();
client.Send(myEmailObject);

The error

The SMTP host was not specified

like image 795
GerardV Avatar asked Dec 11 '22 16:12

GerardV


1 Answers

There are multiple web.config's in a ASP.NET MVC application. In order for most of the system related settings to take effect, they have to be placed inside the application's root web.config. That means the one that the server sees as "~/web.config" and that is in the root folder of the Visual Studio solution.

like image 141
Roman Gruber Avatar answered Dec 24 '22 16:12

Roman Gruber