2011-03-11 72 views
1

我正在研究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: }

回答

3

how can i get it right?

为了能够送你需要一个SMTP服务器的电子邮件。如果你没有一个SMTP服务器设置你可以为调试目的放在你的web.config以下:

<system.net> 
    <mailSettings> 
     <smtp deliveryMethod="SpecifiedPickupDirectory"> 
      <specifiedPickupDirectory pickupDirectoryLocation="c:\email"/> 
      <network host="localhost"/> 
     </smtp> 
    </mailSettings> 
</system.net> 

现在,所有的电子邮件将被存储在c:\emails文件夹,而不是发送,这样就可以检查如果你的代码正常工作。

至于配置真正的SMTP服务器,这是一个问题,你可能会问Serverfault,因为它是StackOverflow的主题。

+0

我试过上面的方法。但它仍然说相同 – software 2011-03-11 18:16:54

+1

我测试了它,它正在工作,您需要在发送电子邮件之前手动创建电子邮件文件夹。 http://amrelgarhy.com/blog/sending-mails-development-machine-without-actual-mail-server/ – 2011-06-05 17:32:14

2

我有这个相同的问题,并能够解决它。问题在于,当您更改Web.config中的代码并将其保存时,如果您仍然有运行Web页面的错误消息以及可能在Visual Studio中运行的其他内容,实际上并未保存它。

因此,要解决这个问题

  1. 关闭所有网页。
  2. 关闭Visual Studio
  3. 回到VS并检查Web.config文件。你会注意到它没有mailSettings代码。
  4. 添加mailSettings代码并再次运行。它现在运行良好。
+0

这听起来很奇怪。我从未亲自目睹过这种行为。 – NotMe 2013-09-04 01:52:41

1

我有同样的问题,并尝试在这里的解决方案。没有工作。然后我看到View文件夹中有一个web.config文件,并且在根文件夹中有一个web.config文件。确保你修改了根文件夹web.config。