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.
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.