5.5.1 Authentication Error in Gmail

Code for sending email using gmail in C# :

using System.Net.Mail;
using System.Net;

MailMessage mail = new MailMessage();
mail.To.Add("dummy@test.com");
mail.From = new MailAddress("dummy-sender@gmail.com");
mail.Subject = "subject";
mail.SubjectEncoding = System.Text.Encoding.UTF8;
mail.Body = "foo foo bar";
mail.Priority = MailPriority.High;

SmtpClient client = new SmtpClient();
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential("dummy-sender@gmail.com", "gmail-password");
client.Port = 587;
client.Host = "smtp.gmail.com";
client.EnableSsl = true;
client.Send(mail);

Error:

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

Solution:

Login to your gmail account from which you are trying to send email and then go to https://www.google.com/settings/security/lesssecureapps

Turn on access for less secure apps and error would be resolved.

This Gmail error, 5.5.1 Authentication, means Google rejected the login used to send mail from code. Google has tightened sign in, so a plain account password is usually refused.

The reliable fixes are to use an app password with two factor authentication enabled, or to authenticate with OAuth 2.0. The account password alone no longer works for most accounts.

Comments

Popular posts from this blog

[Solved] Error: No such keg: /usr/local/Cellar/gcc

[How To] Unfollow Non-followers on Instagram