title: SMTP email client is throwing errors "Timeout" or "requires secure connection" even with correct credential
body: <p>I was new to SMTP client in C#. So I decided to test it with my credentials. I built an ASP.NET Web forms application that has a Contact Us page on which I am trying to send an email to whoever person fills the form.</p>
  
  <p>I tried sample code after going through this article - <a href="https://www.c-sharpcorner.com/UploadFile/87b416/sending-a-simple-email-using-smtpclient-in-C-Sharp/" rel="nofollow noreferrer">https://www.c-sharpcorner.com/UploadFile/87b416/sending-a-simple-email-using-smtpclient-in-C-Sharp/</a></p>
  
  <p>I have one account in Yahoo so I used its SMTP domain "smtp.mail.yahoo.com" with port number: 465, then my app always threw Timeout exception. So I decided to try with Google's server "smtp.gmail.com" with "587" port and now it raised different exception with message:</p>
  
  <blockquote>
    <p>The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required</p>
  </blockquote>
  
  <p>I don't understand what are the prerequisites for working with SMTP on secure servers like Google and Yahoo. Please someone explain.</p>
  
  <p>Also note that I didn't have 2 step verification enabled for my Google account, just to make it clear since some questions on SO have mentioned that this might be the problem.</p>
  
  <p>I also read this question but I am testing directly on my machine - <a href="https://stackoverflow.com/questions/20906077/gmail-error-the-smtp-server-requires-a-secure-connection-or-the-client-was-not/">Gmail Error :The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required</a></p>
  
  <p>In case if it helps, here is the sample code:</p>
  
  <pre><code>try
  {
      MailMessage m = new MailMessage();
  
      m.From = new MailAddress("dummy123@gmail.com");
      m.To.Add(new MailAddress("dummyreceiver123@gmail.com"));
      m.Subject = TBSub.Text;
      m.Body = TBBody.Text;
      m.IsBodyHtml = true;
  
      NetworkCredential nc = new NetworkCredential();
      nc.UserName = "dummy123"
      nc.Password = "dummy@123";
  
      SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
      smtp.Credentials = nc;
      smtp.EnableSsl = true;
      smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
      smtp.Send(m);
  }
  catch (Exception ex)
  {
      //Log this error
  }
  </code></pre>