I am working on the partyInvities (the first asp.net mvc application) example on Pro Asp.Net book.
When I try to send email to the party organizer I am getting an error saying that the smtp host is not specified. How can I get it right? Here is my code and error.
//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 config
<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>
//error
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: }