title: Error message: "The SMTP server requires a secure connection or the client was not authenticated."
body: <p>I have searched other posts and set required things in my gmail account(<a href="https://myaccount.google.com/lesssecureapps" rel="nofollow noreferrer">https://myaccount.google.com/lesssecureapps</a>)</p>
  
  <p>but somehow it still throws exception with the message "The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at" </p>
  
  <pre><code>[HttpPost]
          public ActionResult SaveAppointment(Appointment mdl)
          {
              try
              {
                  SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", 587);
  
                  smtpClient.Credentials = new System.Net.NetworkCredential("username", "mypass");
                  smtpClient.UseDefaultCredentials = false;
                  smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
                  smtpClient.EnableSsl = true;
                  MailMessage mail = new MailMessage();
  
                  //Setting From , To and CC
                  mail.From = new MailAddress("username@gmail.com", "MyWeb Site");
                  mail.To.Add(new MailAddress("username@gmail.com"));
  
                  smtpClient.Send(mail);
  
                  return View();
              }
              catch(Exception ex)
              {
                  return null;
              }
  
          }
  </code></pre>
  
  <p>What should it be I missing?</p>