title: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required.?8
body: <p>I was create sample email sender using c# but it shows an error is:</p>
  
  <p>The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required.</p>
  
  <pre><code>        System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
          mail.To.Add("toAddress@gmail.com");
          mail.From = new MailAddress("fromAddress@gmail.com", "Test Mail !", System.Text.Encoding.UTF8);
          mail.Subject = "Test Mail";
          mail.SubjectEncoding = System.Text.Encoding.UTF8;
          mail.Body = "Test message";
          mail.BodyEncoding = System.Text.Encoding.UTF8;
          mail.IsBodyHtml = true;
          mail.Priority = MailPriority.High;
          SmtpClient client = new SmtpClient();
          client.Credentials = new System.Net.NetworkCredential("myMail@gmail.com", "password");
          client.Port = 587;
          client.Host = "smtp.gmail.com";
          client.EnableSsl = true;
          client.UseDefaultCredentials = true;
          client.Send(mail);
  </code></pre>
  
  <p>Web config:</p>
  
  <pre><code>&lt;system.net&gt;
      &lt;mailSettings&gt;
        &lt;smtp from="myMail@gmail.com"&gt;
          &lt;network host="smtp.gmail.com" password="password"
            port="587" userName="myMail@gmail.com" /&gt;
        &lt;/smtp&gt;
      &lt;/mailSettings&gt;
  &lt;/system.net&gt;
  </code></pre>