我正在研究Pro Asp.Net上的partyInvities(第一个asp.net mvc应用程序)示例。从亲Asp.net书的例子?
当我尝试发送电子邮件给派对管理器时,我收到一条错误消息,指出未指定smtp主机。我怎样才能解决它?这是我的代码和错误。
//code guestresponse
public class GuestResponse
{
[Required(ErrorMessage="* Please Enter Your Name")]
public string Name { get; set; }
[Required(ErrorMessage="* Please Enter Your Email Id")]
[RegularExpression(".+\\@.+\\...+",ErrorMessage=" * Please Enter Valid Email Id")]
public string Email {get; set; }
[Required(ErrorMessage = "* Please Enter Your Phone Number")]
public string Phone { get; set; }
[Required(ErrorMessage = "* Please Specify Whether You Will Attend or Not")]
public bool? WillAttend { get; set; }
private MailMessage BuildMailMessage()
{
var message = new StringBuilder();
message.AppendFormat("Date: {0: yyyy-MM-dd hh:mm}\n", DateTime.Now);
message.AppendFormat("RSVP from : {0}\n", Name);
message.AppendFormat("Email: {0}\n", Email);
message.AppendFormat("Phone: {0}\n", Phone);
message.AppendFormat("Can Come: {0}\n", WillAttend.Value ? "yes" : "No");
return new MailMessage(
"[email protected]", "[email protected]", Name + (WillAttend.Value ? "will attend" : "Won't attend"), message.ToString()); //From, To, Subject, Body
}
public void Submit()
{
using (var smtpClient= new SmtpClient())
using (var mailMessage = BuildMailMessage())
{
smtpClient.Send(mailMessage);
}
}
}
// web配置
<configuration>
<system.net>
<mailSettings>
<smtp deliveryMethod="Network">
<network host="smtp.example.com"/>
</smtp>
</mailSettings>
</system.net>
<system.web>
<httpHandlers>
<add path="*" verb="*" type="System.Web.HttpNotFoundHandler"/>
</httpHandlers>
<!--
Enabling request validation in view pages would cause validation to occur
after the input has already been processed by the controller. By default
MVC performs request validation before a controller processes the input.
To change this behavior apply the ValidateInputAttribute to a
controller or action.
-->
<pages
validateRequest="false"
pageParserFilterType="System.Web.Mvc.ViewTypeParserFilter, System.Web.Mvc, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
pageBaseType="System.Web.Mvc.ViewPage, System.Web.Mvc, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
userControlBaseType="System.Web.Mvc.ViewUserControl, System.Web.Mvc, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<controls>
<add assembly="System.Web.Mvc, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" namespace="System.Web.Mvc" tagPrefix="mvc" />
</controls>
</pages>
</system.web>
<system.webServer>
<validation validateIntegratedModeConfiguration="false" />
<handlers>
<remove name="BlockViewHandler"/>
<add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />
</handlers>
</system.webServer>
</configuration>
//错误
Server Error in '/' Application. The SMTP host was not specified. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.InvalidOperationException: The SMTP host was not specified.
Source Error:
Line 49: {
Line 50: smtpClient.Send(mailMessage);
Line 51: }
Line 52:
Line 53: }
我试过上面的方法。但它仍然说相同 – software 2011-03-11 18:16:54
我测试了它,它正在工作,您需要在发送电子邮件之前手动创建电子邮件文件夹。 http://amrelgarhy.com/blog/sending-mails-development-machine-without-actual-mail-server/ – 2011-06-05 17:32:14