title: Contact Us Form ASP.net MVC
body: <p>I have created contact us form with the following code:</p>
  
  <p><strong>ConatModels.cs</strong></p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.ComponentModel.DataAnnotations;
  using System.Linq;
  using System.Web;
  
  namespace DemoVer1.Models
  {
      public class ContactModels
      {
         [Required(ErrorMessage = "First Name is required")] 
  
          public string FirstName { get; set; }
          public string Supject { get; set; }
          [Required]
  
          public string Email { get; set; }
          [Required]
          public string Message { get; set; } 
  
      }
  }
  </code></pre>
  
  <p><strong>HomeController.cs:</strong></p>
  
  <pre><code>   using DemoVer1.Models;
      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Net.Mail;
      using System.Text;
      using System.Web;
      using System.Web.Mvc;
  
      namespace DemoVer1.Controllers
      {
          public class HomeController : Controller
          {
              public ActionResult Index()
              {
                  return View();
              }
  
              public ActionResult About()
              {
                  ViewBag.Message = "Your application description page.";
  
                  return View();
              }
  
              public ActionResult Contact(ContactModels c)
              {
                  if (ModelState.IsValid)
                  {
                      try
                      {
                          MailMessage msg = new MailMessage();
                          SmtpClient smtp = new SmtpClient("smtp.gmail.com");
                          MailAddress from = new MailAddress(c.Email.ToString());
                          StringBuilder sb = new StringBuilder();
                          msg.From = new MailAddress("sender@gmail.com");// replace it with sender email address
  
                          msg.To.Add("recipient@gmail.com");// replace ti with recipient email address
                          msg.Subject = "Contact Us";
                          smtp.EnableSsl = true;
  
                          smtp.Credentials = new System.Net.NetworkCredential("sender@gmail.com", " email password");
                          smtp.Port = 587;
                          sb.Append("First name: " + c.FirstName);
                          sb.Append(Environment.NewLine);
                          sb.Append("Last name: " + c.Supject);
                          sb.Append(Environment.NewLine);
                          sb.Append("Email: " + c.Email);
                          sb.Append(Environment.NewLine);
                          sb.Append("Comments: " + c.Message);
                          msg.Body = sb.ToString();
                          smtp.Send(msg);
                          msg.Dispose();
                          return View("Success");
                      }
                      catch (Exception)
                      {
                          return View("Error");
                      }
                  }
                  return View();
              }
          }
      }
  </code></pre>
  
  <p><strong>Contact.cshtml</strong></p>
  
  <pre><code>  @model DemoVer1.Models.ContactModels
      @{
          ViewBag.Title = "Contact";
      }
  
      &lt;h1&gt;contact us&lt;/h1&gt;
      @using (Html.BeginForm())
      {
  
          @Html.ValidationSummary(true)
          &lt;div class="row"&gt;
              @Html.LabelFor(model =&gt; model.FirstName, "First Name:")
              @Html.EditorFor(model =&gt; model.FirstName)
              @Html.ValidationMessageFor(model =&gt; model.FirstName)
  
          &lt;/div&gt;
          &lt;div class="row"&gt;
              @Html.LabelFor(model =&gt; model.Supject, "Last Name:")
              @Html.EditorFor(model =&gt; model.Supject)
          &lt;/div&gt;
          &lt;div class="row"&gt;
              @Html.LabelFor(model =&gt; model.Email, "Email:")
              @Html.EditorFor(model =&gt; model.Email)
          &lt;/div&gt;
          &lt;div class="row"&gt;
              @Html.LabelFor(model =&gt; model.Message, "Comments:")
              @Html.TextAreaFor(model =&gt; model.Message)
          &lt;/div&gt;
          &lt;div class="row"&gt;
              &lt;input type="submit" value="submit" /&gt;
              &lt;input type="reset" value="reset" /&gt;
          &lt;/div&gt;
  
      } 
  </code></pre>
  
  <p>If I run it, I get an error!I don't know what is the problem.
  I appreciate your help. Thanks in advance :)</p>