Reputation: 77
I have been trying to get my application to send an email for over an hour now, and i've tried everything i've found online but i still get the exception mentioned in the title of my question.
Here is the code i used:
SmtpClient client = new SmtpClient();
client.Host = "smtp.gmail.com";
client.Port = 587;
client.Credentials = new NetworkCredential("Sender email", "Sender email password");
client.EnableSsl = true;
client.UseDefaultCredentials = false;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
MailMessage mail = new MailMessage();
mail.From = new MailAddress("Sender email", "Sender");
mail.To.Add(new MailAddress("My email"));
mail.Subject = "TEST";
mail.IsBodyHtml = true;
mail.Body = sb.ToString();
client.Send(mail);
I have allowed access to less secure apps on my account. I have also tried enabling 2FA and then generating an application specific password, but the exception stays.
Upvotes: 3
Views: 16336
Reputation: 4868
Check your gmail account and turn on "Acess for less secure apps"
Set client.UseDefaultCredentials = false;
before client.Credentials = new NetworkCredential("Sender email", "Sender email password");
Upvotes: 11