1

I have the following code inside my asp.net core MVC web application, to send an email using google smtp server:-

MailMessage mail = new MailMessage();
mail.From = new MailAddress("info@***.com");
mail.To.Add(new MailAddress("info@**.com"));
mail.Subject = "New Submission from Web Site";
mail.IsBodyHtml = true;
System.Text.StringBuilder mailBody = new System.Text.StringBuilder();
mailBody.AppendLine("<b>Dear Sirs, </b><br/><br/>");
mailBody.AppendLine(
"New Submission has been submitted from Web site. To view the new submission please click on the link <a href'https://*****/Submissions/details/" + sc.Submission.Id.ToString()
 + " <br/> <span style='font-weight:bold'>Regards</span> <br/> <span style='font-weight:bold'>Web Site</span>"
);
                
mail.Body = mailBody.ToString();
// Create a new Smpt Client using Google's servers
var smtpClient = new SmtpClient();
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.EnableSsl = true;
smtpClient.Host = "smtp.gmail.com";
smtpClient.Port = 587;


string smtpConn = configuration.GetSection("SMTP").GetSection("pass").Value;

smtpClient.Credentials = new System.Net.NetworkCredential
("info@****.com", smtpConn); // ***use valid credentials***

smtpClient.Send(mail);

but i got this error:-

SmtpException: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.0 Authentication Required. Learn more at

any idea? although i am running the application from a machine where i can login to the gmail account from it...

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
John John
  • 1
  • 72
  • 238
  • 501
  • Make sure the user has less secure apps enabled, also check that they have 2fa disabled on their account. If its still not working you may have to add https://developers.google.com/gmail/imap/xoauth2-protocol – Linda Lawton - DaImTo Jun 24 '20 at 12:57

1 Answers1

4

SmtpException: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.0 Authentication Required. Learn more at

When you encounter this error, it may be caused by the following situations

  1. Wrong password, solution: just enter the correct password.

  2. When you try to log in from some App.

    Solution: Go to the security settings of this link, and enable less secure apps.

  3. This usually happens when you try to log in from another time zone or IP address computer. The production server and the email ID you use are in different time zones.

    There are two solutions for this situation:

    • Log in to production server via remote access, and sign in to
      gmail once with your credentials. They will ask for the confirmation, confirm it and log out.

    • Or log in gmail to your local computer, Follow this Link and
      choose review this activity and take proper actions.

If you do not have access to the production server, then try:

You have to enable login from other timezone/ip for your google account.

to do this follow the link https://g.co/allowaccess and allow access by clicking the continue button.

More details, you could refer to this discussion.

LouraQ
  • 6,443
  • 2
  • 6
  • 16
  • the g.co/allowacess link did it for me after seeing the same suggestions in 20 different articles. – T3.0 Jul 21 '21 at 21:48