2

I was create sample email sender using c# but it shows an error is:

The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required.

private void SendHtmlFormattedEmail(string recepientEmail, string subject, string body)
{
    using (MailMessage mailMessage = new MailMessage())
    {
       mailMessage.From = new MailAddress("sysumurugan@gmail.com");
       mailMessage.Subject = subject;
       mailMessage.Body = body;
       mailMessage.To.Add(new MailAddress(recepientEmail));
       SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
       smtp.EnableSsl = true;
       smtp.UseDefaultCredentials = false;
       System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential("sysumurugan@gmail.com", "XXXXXXXXX");
       smtp.Credentials = NetworkCred;
       smtp.Timeout = 10000;
       smtp.Send(mailMessage);
    }
}

please help me for sample mail sender code

Status:System.Net.Mail.SmtpStatusCode.MustIssueStartTlsFirst

Arul Murugan.C
  • 111
  • 1
  • 2
  • 14
  • Credentials you provide in `NetworkCred` will be ignored if `UseDefaultCredentials` is `true`. [MSDN](https://msdn.microsoft.com/en-us/library/system.net.mail.smtpclient.usedefaultcredentials%28v=vs.110%29.aspx) – Adriano Repetti Mar 10 '15 at 11:34

2 Answers2

4

Try swapping these lines like so:

smtp.UseDefaultCredentials = false;
System.Net.NetworkCredential NetworkCred = new    System.Net.NetworkCredential("sysumurugan@gmail.com", "XXXXXXXXX");
David B
  • 889
  • 2
  • 11
  • 29
  • I swap these lines and run, but still It show same error. – Arul Murugan.C Mar 10 '15 at 11:54
  • 1
    This question has been answered previously: http://stackoverflow.com/questions/20906077/gmail-error-the-smtp-server-requires-a-secure-connection-or-the-client-was-not – David B Mar 10 '15 at 12:04
1

I went to the following link: https://www.google.com/settings/security/lesssecureapps

and turned on the option Access for less secure apps

enter image description here

David B
  • 889
  • 2
  • 11
  • 29
Arul Murugan.C
  • 111
  • 1
  • 2
  • 14