3

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.

chrisstar123
  • 77
  • 1
  • 2
  • 8

1 Answers1

11
  1. Check your gmail account and turn on "Acess for less secure apps"

  2. Set client.UseDefaultCredentials = false; before client.Credentials = new NetworkCredential("Sender email", "Sender email password");

chrisstar123
  • 77
  • 1
  • 2
  • 8
ISHIDA
  • 4,700
  • 2
  • 16
  • 30