Asp.net mvc 2 来自pro Asp.net书籍的示例?

Asp.net mvc 2 来自pro Asp.net书籍的示例?,asp.net-mvc-2,Asp.net Mvc 2,我正在编写Pro asp.net书籍上的PartyVities(第一个asp.net mvc应用程序)示例 当我尝试向聚会组织者发送电子邮件时,我收到一个错误,表示未指定smtp主机。我怎样才能把它做好?这是我的代码和错误 //code guestresponse public class GuestResponse { [Required(ErrorMessage="* Please Enter Your Name")] public string Name { g

我正在编写Pro asp.net书籍上的PartyVities(第一个asp.net mvc应用程序)示例

当我尝试向聚会组织者发送电子邮件时,我收到一个错误,表示未指定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(
             "rsvps@example.com", "party@example.com", 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);
         }

     }


     }
//网络配置

<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>

//错误

“/”应用程序中出现服务器错误。未指定SMTP主机。 描述:在执行过程中发生未处理的异常 当前的web请求。请查看堆栈跟踪以了解更多信息 有关错误的信息及其在代码中的来源

异常详细信息:System.InvalidOperationException:SMTP主机已关闭 未指定

源错误:

第49行:{
第50行:smtpClient.Send(mailMessage);
第51行:}
第52行:
第53行:}

我怎样才能把它做好

为了能够发送电子邮件,您需要一个SMTP服务器。如果没有SMTP服务器设置,出于调试目的,可以在web.config中设置以下内容:

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

现在,所有电子邮件都将存储在
c:\emails
文件夹中,而不是发送,这样您就可以检查代码是否正常工作

至于配置真正的SMTP服务器,您可能会问这个问题,因为它与StackOverflow无关

我怎样才能把它做好

为了能够发送电子邮件,您需要一个SMTP服务器。如果没有SMTP服务器设置,出于调试目的,可以在web.config中设置以下内容:

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

现在,所有电子邮件都将存储在
c:\emails
文件夹中,而不是发送,这样您就可以检查代码是否正常工作


至于配置真正的SMTP服务器,您可能会问这个问题,因为它与StackOverflow无关。

我也遇到过同样的问题,并且能够解决它。问题是,当您更改Web.config中的代码并保存它时,如果您的网页仍在运行错误消息以及Visual Studio中可能运行的其他内容,则实际上不会保存它

所以要解决它

  • 关闭所有网页
  • 关闭Visual studio
  • 返回VS并检查Web.config文件。你会注意到它里面没有mailSettings代码
  • 添加邮件设置代码并再次运行。现在一切都会好起来的

  • 我有同样的问题,并且能够解决它。问题是,当您更改Web.config中的代码并保存它时,如果您的网页仍在运行错误消息以及Visual Studio中可能运行的其他内容,则实际上不会保存它

    所以要解决它

  • 关闭所有网页
  • 关闭Visual studio
  • 返回VS并检查Web.config文件。你会注意到它里面没有mailSettings代码
  • 添加邮件设置代码并再次运行。现在一切都会好起来的

  • 我也遇到了同样的问题,并在这里尝试了解决方案。什么都没用。然后我看到View文件夹中有一个web.config,根文件夹中也有一个。请确保您正在修改根文件夹web.config。

    我遇到了相同的问题,并在此处尝试了解决方案。什么都没用。然后我看到View文件夹中有一个web.config,根文件夹中也有一个。确保您正在修改根文件夹web.config。

    我测试了它,它正在工作,您需要在发送电子邮件之前手动创建电子邮件文件夹。我测试了它,它正在工作,你需要手动创建电子邮件文件夹之前发送电子邮件。这听起来很奇怪。我从未亲眼目睹过这种行为。这听起来很奇怪。我从未亲眼目睹过这种行为。