Sunday, January 12, 2020

[FIX] The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.0 Authentication Required.

I got above error when i try to send my test results at the [AssemblyCleanup] step on my automation test suite.
This is how my code looks like.

 public static void SendEmail()
        {

            string fromAddress = "@gmail.com";
            string mailPassword = "";
            SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
            SmtpServer.Port = 587;
            SmtpServer.EnableSsl = true;
            SmtpServer.DeliveryMethod = SmtpDeliveryMethod.Network;
            SmtpServer.UseDefaultCredentials = false;
            SmtpServer.Credentials = new System.Net.NetworkCredential(fromAddress, mailPassword);
          

            MailMessage myMail = new System.Net.Mail.MailMessage();
            myMail.From = new MailAddress("xxxx@gmail.com");
            myMail.To.Add("xxxx");
            myMail.To.Add("xxxx");

            myMail.Subject = " Test Results";
         
            String body = "Test Status";
            myMail.IsBodyHtml = true;
            myMail.Body = body;
            myMail.Attachments.Add(new Attachment(@"..\..\..\xx\Reports\xx.html"));


            SmtpServer.Send(myMail);

        }

But still Im getting the above error so the solution was to enable Less secure app access by navigating to below URL
https://myaccount.google.com/u/3/lesssecureapps?pli=1&pageId=none

Then update your Gmail password to a very strong password.

No comments:

Post a Comment