“The SMTP host was not specified.” - but it is specified?

一世执手 提交于 2019-12-03 23:44:56

In a clean MVC project, I am unable to replicate your issue. Following the ScottGu blog post here, I was able to get a gmail sent email without issue (VS 2013, .NET 4.5.1, MVC 5). Note the the <system.net> element is a top level element and not nested inside of AppSettings or <system.web>.

Important

There are a few web.config files in your solution, ensure that the mailSettings is inserted into the root level web.config (and not the one located in the Views folder)

Web.Config

<configuration>
  <system.net>
    <mailSettings>
      <smtp from="myEmail@gmail.com">
        <network host="smtp.gmail.com" 
                 port="587" 
                 enableSsl="true" 
                 userName="myEmail@gmail.com" 
                 password="SuperSecretPwd" 
                 defaultCredentials="false" /> <!--This must be false on Gmail-->
      </smtp>
    </mailSettings>
  </system.net>
</configuration>

Controller

var smtpClient = new SmtpClient();
var msg = new MailMessage();
msg.To.Add("MyOtherAddress@yahoo.com");
msg.Subject = "Test";
msg.Body = "This is just a test email";
smtpClient.Send(msg);

It is unclear if some of the extra attributes you have included are causing issues (thought they shouldn't) such as delivery method. Also, is there a setting for allowing SMTP access or is that just for IMAP/POP delivery?

If you can test and are successful in a clean project, then this would point to either a web.config transformation problem or some other setting(s) in your project overriding the web.config settings that you have in place.

Malachi

The solution was mentioned in the Chat, but never edited into the answer above.

Make sure that you make these settings in the web.config of the Root Level and not in the Views folder.

@Tommy: ...this looks like the web.config from your Views folder and not the web.config at the root of the application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!