Search code examples
c#smtpgmail

the SMTP server requires a secure connection or the client was not authenticated. The server response was 5.5.1, Authentication required


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.


Solution

    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");