title: ASP.NET MVC SMTP is not working and return false value once i click submit button
body: <p>I tried to send an email using SMTP in ASP.NET MVC, and every time the function returns a false value. Can you guys please help me fix the problem?</p>
  <p>I haven't got any errors but once I click submit button with some value, <code>client.Send(mailMessage)</code> returns false value.</p>
  <pre><code>@using (Html.BeginForm(&quot;Contact&quot;, &quot;Contact&quot;)) {
  
  &lt;div class=&quot;container&quot;&gt;
  &lt;p style=&quot;background:red&quot; class=&quot;text-center w-50 m-auto&quot;&gt;@ViewBag.contact&lt;/p&gt;
  
  @Html.ValidationSummary(true, &quot;&quot;, new { @class = &quot;text-danger&quot; })
  
  &lt;div class=&quot;row&quot;&gt;
      &lt;div class=&quot;col-lg-6 col-md-6 col-sm-12 form-group&quot;&gt;
          @Html.EditorFor(model =&gt; model.name, new { htmlAttributes = new { @class = &quot;form-control mt-2&quot;, placeholder = &quot;John Doe&quot; } })
      &lt;/div&gt;
      &lt;div class=&quot;col-lg-6 col-md-6 col-sm-12 form-group&quot;&gt;
          @Html.EditorFor(model =&gt; model.email, new { htmlAttributes = new { @class = &quot;form-control mt-2&quot;, placeholder = &quot;doe@email.com&quot; } })
      &lt;/div&gt;
  &lt;/div&gt;
  &lt;div class=&quot;form-group&quot;&gt;
      @Html.EditorFor(model =&gt; model.subject, new { htmlAttributes = new { @class = &quot;form-control mt-5&quot;, placeholder = &quot;Please drop your comment?&quot; } })
  &lt;/div&gt;
  
  &lt;input style=&quot;margin-top: 50px&quot; type=&quot;submit&quot; value=&quot;Send&quot; id=&quot;btnsubmit&quot; class=&quot;btn btn-primary text-center&quot; /&gt;
  
  &lt;/div&gt;
  </code></pre>
  <p>And my controller looks like this:</p>
  <pre><code>[HttpPost]
  public ActionResult Contact(Contact contact)
  {
      ViewBag.contact = null;
  
      if (!ModelState.IsValid)
      {
          ViewBag.contact = &quot;Something went wrong&quot;;
          return View(&quot;Contact&quot;, contact);
      }
  
      ViewBag.message = null;
      bool result = false;
  
      result = SendEmail(contact.email, contact.name, contact.subject);
  
      if (result == true)
      {
          return RedirectToAction(&quot;Index&quot;);
      }
      else
          return View(&quot;Contact&quot;, contact);
  }
  
  public bool SendEmail(string toEmail, string subject, string body)
  {
      try
      {
          string senderEmail = System.Configuration.ConfigurationManager.AppSettings[&quot;SenderEmail&quot;].ToString();
          string senderPassword = System.Configuration.ConfigurationManager.AppSettings[&quot;SenderPassword&quot;].ToString();
  
          SmtpClient client = new SmtpClient(&quot;smtp.gmail.com&quot;, 587);
          client.EnableSsl = true;
          client.Timeout = 100000;
          client.DeliveryMethod = SmtpDeliveryMethod.Network;
          client.UseDefaultCredentials = false;
          client.Credentials = new NetworkCredential(senderEmail, senderPassword);
  
          MailMessage mailMessage = new MailMessage(senderEmail, toEmail, subject, body);
          mailMessage.IsBodyHtml = true;
          mailMessage.BodyEncoding = UTF8Encoding.UTF8;
  
          client.Send(mailMessage);
  
          ViewBag.contact = &quot;Successfully sent&quot;;
  
          return true;
      }
      catch(Exception e)
      {
          ViewBag.contact = &quot;Unsuccessful, something was wrong&quot;;
          return false;
      }
  }
  </code></pre>
  <p>Can you please help me get rid of this problem?</p>
  <p>Thank you in advance.</p>