The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required?
I have the same problem.
I have found this solution:
Google may block sign in attempts from some apps or devices that do not use modern security standards. Since these apps and devices are easier to break into, blocking them helps keep your account safer.
Some examples of apps that do not support the latest security standards include:
- The Mail app on your iPhone or iPad with iOS 6 or below
- The Mail app on your Windows phone preceding the 8.1 release
- Some Desktop mail clients like Microsoft Outlook and Mozilla Thunderbird
Therefore, you have to enable Less Secure Sign-In (or Less secure app access) in your google account.
After sign into google account, go to:
https://www.google.com/settings/security/lesssecureapps
or
https://myaccount.google.com/lesssecureapps
In C#, you can use the following code:
using (MailMessage mail = new MailMessage())
{
mail.From = new MailAddress("[email protected]");
mail.To.Add("[email protected]");
mail.Subject = "Hello World";
mail.Body = "<h1>Hello</h1>";
mail.IsBodyHtml = true;
mail.Attachments.Add(new Attachment("C:\\file.zip"));
using (SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587))
{
smtp.Credentials = new NetworkCredential("[email protected]", "password");
smtp.EnableSsl = true;
smtp.Send(mail);
}
}
-------------------
Info shared by Michael Freidgeim in below comments area:
Similar answer with screenshots https://stackoverflow.com/a/32457468/52277
First check for gmail's security related issues. You may have enabled double authentication in gmail. Also check your gmail inbox if you are getting any security alerts. In such cases check other answer of @mjb as below
Below is the very general thing that i always check first for such issues
client.UseDefaultCredentials = true;
set it to false.
Note @Joe King's answer - you must set client.UseDefaultCredentials before you set client.Credentials
Ensure you set SmtpClient.Credentials
after calling SmtpClient.UseDefaultCredentials = false
.
The order is important as setting SmtpClient.UseDefaultCredentials = false
will reset SmtpClient.Credentials
to null.