1

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

3 Answers 3

3

how can i get it right?

In order to be able to send an email you need an SMTP server. If you don't have an SMTP server setup you could for debugging purposes put the following in your web.config:

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

Now all emails will be stored in the c:\emails folder instead of being sent so that you can check if your code works properly.

As far as configuring a real SMTP server this is a question you might ask on Serverfault because it is off-topic for StackOverflow.

Sign up to request clarification or add additional context in comments.

1 Comment

I tested it and it is working, you need to create the email folder manually before sending emails. amrelgarhy.com/blog/…
2

I had this same issue and was able to resolve it. The problem is when you are changing the code in the Web.config and saving it, it is not actually saving it if you still have Web Pages running with the error message as well as possibly other things running within Visual Studio.

So to fix it

  1. Close all web pages.
  2. Close Visual studio
  3. Go back into VS and check the Web.config file. You will notice it doesn't have the mailSettings code in it.
  4. Add the mailSettings code and run it again. It will run fine now.

1 Comment

This just sounds...weird. I have never personally witnessed this behavior.
1

I had the same issue and tried the solutions here. Nothing worked. I then saw there was a web.config in View folder as well as one in the root folder. Make sure you are modifying the root folder web.config.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.