Friday, February 12, 2016

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.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.