How to Fix Error Authentication in Gmail

In previous tutorial, we have written an article about how to use Gmail to send email in Asp.net Core. We have received few users experience error message when try to setup it. So, in this tutorial, we will advise how to fix error the server response was 5.5.1 authentication required

Error Message

An exception of type ‘System.Net.Mail.SmtpException’ occurred in System.dll but was not handled in user code
Additional information: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required.

Gmail Code

Below is the code snippet using to send Email through Gmail Host:

[HttpPost]
public JsonResult SendEmail(string _name, string _phone, string _email, string _description)
{
    string senderID = "[email protected]";
    string senderPassword = "hello123";
    string result = "Email Sent Successfully";

    string body = " " + _name + " has sent an email from " + _email;
    body += "Phone : " + _phone;
    body += _description;
    try
    {
      MailMessage mail = new MailMessage();
      mail.To.Add(senderID);
      mail.From = new MailAddress(senderID);
      mail.Subject = "My Test Email!";
      mail.Body = body;
      mail.IsBodyHtml = true;
      SmtpClient smtp = new SmtpClient();
      smtp.Host = "smtp.gmail.com"; //Or Your SMTP Server Address
      smtp.Credentials = new System.Net.NetworkCredential(senderID, senderPassword);
      smtp.Port = 587;
      smtp.EnableSsl = true;
      smtp.Send(mail);
    }
    catch (Exception ex)
    {
       result = "problem occurred";
       Response.Write("Exception in sendEmail:" + ex.Message);
    }
    return Json(result);
}

First login into Gmail Account from which Gmail credentials are you using to send email.

Now go to Gmail Settings Feature, it comes in your Gmail Inbox on right side, as you see in picture below.

Move to Security Feature

Now click on Gmail Setting Feature, move to security feature or alternatively can hit/paste this URL in your browser https://www.google.com/settings/security/lesssecureapps where you can see settings.

Access for less secure apps

  1. Turn off (default) 
  2. Turn On  

Click on Turn On option to authorize emails sending through while programming (MVC, C#, PHP, Java etc.) with required credentials in Gmail account.

Summary

Above will help to resolve issue the server response was 5.5.1 authentication required while sending email through Gmail credentials in programming languages like ASP.Net, MVC, C#, PHP, Java etc.

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *