Gmail SMTP authentication error when using C# SmtpClient to send emails

I’m getting an authentication error when trying to send emails through Gmail using C# SmtpClient. The error message says 5.5.1 Authentication Required and mentions that the SMTP server needs a secure connection or the client wasn’t authenticated properly.

I’ve tried different port numbers but still can’t get it working. The same Gmail credentials work perfectly in other email clients, so I know the account settings are correct.

Here’s my current code:

SmtpClient mailClient = new SmtpClient("smtp.gmail.com", 587);
mailClient.Credentials = new NetworkCredential("[email protected]", "mypassword");
mailClient.EnableSsl = true;
mailClient.Timeout = 15000;
mailClient.DeliveryMethod = SmtpDeliveryMethod.Network;
mailClient.UseDefaultCredentials = false;

MailMessage message = new MailMessage();
message.From = new MailAddress("[email protected]");
message.To.Add("[email protected]");
message.Subject = "Test Email";
message.Body = "This is a test message";
message.BodyEncoding = System.Text.Encoding.UTF8;

mailClient.Send(message);

The stack trace shows the error occurs at the Send method call. Has anyone dealt with this Gmail SMTP authentication issue before? What am I missing in my configuration?

This is a Gmail security issue, not your code. Regular Gmail passwords don’t work with SMTP anymore - Google changed this for security reasons. You need to create an App Password instead. Go to your Google Account settings, turn on 2-factor authentication (if it’s not already on), then go to Security > App passwords and make a new one for Mail. Use that 16-character password in your code instead of your regular one. Should fix the auth error instantly. I had this exact same problem last month - this solved it immediately.

Your UseDefaultCredentials property is causing issues. You’re setting it to false after assigning credentials, which is incorrect. Move UseDefaultCredentials = false right after creating the SmtpClient object, before you assign your NetworkCredential. Additionally, ensure you’re using an App Password from Google instead of your regular password, as Gmail blocks regular passwords for third-party apps now. The order is significant here; I’ve witnessed this problem trip up many users when working with SmtpClient.

First, check if 2FA is enabled on your Gmail - app passwords won’t work without it. Try switching to port 465 with SSL instead of 587. I’ve seen other users have better luck with that combo. The auth error usually means Google’s blocking your connection, so double-check that less secure app access isn’t causing problems too.