"The SMTP host was not specified." - but it is specified?
Solution 1
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="[email protected]">
<network host="smtp.gmail.com"
port="587"
enableSsl="true"
userName="[email protected]"
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("[email protected]");
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.
Solution 2
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.
Related videos on Youtube
user2381114
Updated on June 30, 2021Comments
-
user2381114 almost 3 years
I'm slightly baffled here - I'm receiving the following error:
The SMTP host was not specified.
Even though my code appears to be correct (from what I can see).
I am able to do it manually by including all the details inside of the controller, e.g.
SmtpClient smtpClient = new SmtpClient("smtp.gmail.com"); smtpClient.Port = 587; ... etc
But I shouldn't have to do this, as I want to use the details inside
mailSettings
(Making it re-usable for various different controllers).mailSettings
in myWeb.Config
file:<system.net> <mailSettings> <smtp from="[email protected]" deliveryMethod="Network" > <network host="smtp.gmail.com" defaultCredentials="true" port="587" enableSsl="true" userName="[email protected]" password="example"/> </smtp> </mailSettings> </system.net>
My Controller action:
[HttpPost] public ActionResult SubmitFeature(FormData formData) { SmtpClient smtpClient = new SmtpClient(); MailMessage mail = new MailMessage(); mail.To.Add(new MailAddress("[email protected]")); mail.Body = "Test"; smtpClient.Send(mail); return View("Example"); }
Is there anything I'm missing which may be causing this? I haven't messed around with any other settings in Web.Config, they are as is when setting up a new MVC5 project.
-
turtlepick over 10 yearsAny chance you added this to "Release.Web.Config" instead of the actual Web.Config?
-
user2381114 over 10 years@flaviotsf Nope, it's inside Web.Config. Am I supposed to reference using the Web Config inside the controller elsewhere or?
-
Wiktor Zychla over 10 yearsI believe you should change
defaultCredentials
tofalse
to authenticate with provided credentials. -
user2381114 over 10 years@WiktorZychla I receive the exact same error after this change sadly
-
-
user2381114 over 10 yearsHello, I am actually able to send emails, however I have to specify the attributes (such as port, enableSsl) inside the controller. The
Web.Config
file doesn't seem to have any effect at all. When I changenew SmtpClient();
tonew SmtpClient("smtp.gmail.com");
, I actually receive a different error - "A from address must be specified", which makes me think theWeb.Config
file isn't even being recognised? -
Tommy over 10 years@user2945993 - thats what I am saying though. I can set this up in the web.config and it works without issue. Can you try the code I posted above in a new/clean project? Do you still have the issues?
-
Tommy over 10 years@user2945993 Are you 100% that these values are not being overwritten by a web.config transformation? Are you debugging locally or publishing to a server?
-
user2381114 over 10 yearsI'm unsure if they're being overridden - just testing a new project now and will let you know.
-
Tommy over 10 years@user2945993 - good deal. Literally copy the code I have above and replace the necessary values. Also, the
<system.net>
is a top level web.configuration element. Ensure you don't have it buried under some other element (like inside system.web or appSettings) -
user2381114 over 10 yearsIt works 100% fine in a new project. You're right, I'm going to have to look at what could be overriding this.
-
Tommy over 10 years@user2945993 Good deal. Double check your configuration settings, make sure you don't have a funny typo, elements in wrong position inside the configuration file and that the compiler isn't transforming and erasing your settings. Beyond that, I am not sure I can help much more at this point :/
-
user2381114 over 10 yearsThanks. Would seeing my config file help? I could chuck it in a pastebin quick.
-
Tommy over 10 years@user2945993 - sure, just be sure to scrub PWDs out of there first :)
-
Tommy over 10 years@user2945993 let us continue this discussion in chat