" SMTP主机没有指定&QUOT。 - 但它被指定? [英] "The SMTP host was not specified." - but it is specified?
问题描述
我在这里稍微困惑 - 我收到以下错误:
I'm slightly baffled here - I'm receiving the following error:
SMTP主机没有指定。
The SMTP host was not specified.
虽然我的code似乎是正确的(从我所看到的)。
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
不过,我不应该这样做,因为我想用里面的 mailSettings
(使之成为可重复使用的各种不同的控制器)的细节。
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
在我的的Web.Config
文件:
<system.net>
<mailSettings>
<smtp from="example@gmail.com" deliveryMethod="Network" >
<network host="smtp.gmail.com" defaultCredentials="true"
port="587" enableSsl="true" userName="example@gmail.com"
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("example@gmail.com"));
mail.Body = "Test";
smtpClient.Send(mail);
return View("Example");
}
有什么我失踪这可能会导致此?我还没有与任何的Web.Config其他设置弄乱左右,他们因为是建立一个新的MVC5项目时。
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.
推荐答案
在一个干净的MVC项目,我不能复制您的问题。这里继 ScottGu博客帖子,我能得到一个Gmail发送的电子邮件没有问题(VS 2013,.NET 4.5.1,MVC 5)。请注意,在&LT; system.net&GT;
元素是一个顶级元素,而不是嵌套的的AppSettings
的内部或&LT;&的System.Web GT;
。
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>
.
重要提示
有解决方案中的一些web.config文件中,确保 mailSettings
插入根级别的web.config(而不是一个位于视图文件夹)
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>
控制器
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);
目前还不清楚,如果你的一些包括是造成问题的额外属性(认为他们不应该),如交付方式。此外,有没有允许SMTP访问的设置或者是说只是IMAP / POP交货?
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?
如果你可以测试并成功地干净的项目,那么这将指向任何一个web.config转型问题,或者在项目其他一些设置(S)覆盖,你在的地方web.config中的设置。
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.
这篇关于&QUOT; SMTP主机没有指定&QUOT。 - 但它被指定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!