user2381114 user2381114 - 1 year ago 138
C# Question

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

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("");
smtpClient.Port = 587;
... etc

But I shouldn't have to do this, as I want to use the details inside
(Making it re-usable for various different controllers).

in my

<smtp from="" deliveryMethod="Network" >
<network host="" defaultCredentials="true"
port="587" enableSsl="true" userName=""

My Controller action:

public ActionResult SubmitFeature(FormData formData)
SmtpClient smtpClient = new SmtpClient();

MailMessage mail = new MailMessage();
mail.To.Add(new MailAddress(""));
mail.Body = "Test";


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.

Answer Source

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 <> element is a top level element and not nested inside of AppSettings or <system.web>.


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)


      <smtp from="">
        <network host="" 
                 defaultCredentials="false" /> <!--This must be false on Gmail-->


var smtpClient = new SmtpClient();
var msg = new MailMessage();
msg.Subject = "Test";
msg.Body = "This is just a test email";

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download