Test Automation
Test Automation

Reputation: 221

Gmail SMTP send - 535-5.7.8 Username and Password not accepted

I am having trouble sending an email from my gmail account with my java application.

I have written simple Java code to send emails from my application. I have MFA turned on and I have created an App Password and I am using it in my code. When I execute the code I get the message

javax.mail.AuthenticationFailedException: 535-5.7.8 Username and Password not accepted. Learn more at 535 5.7.8 https://support.google.com/mail/?p=BadCredentials ft13-20020a17090b0f8d00b0022630ba1c80sm1576429pjb.42 - gsmtp

I am fairly certain that the error message is not related to a code issue (but what do I know?) and I have double checked the credentials multiple times to ensure they are correct. I'm trying to understand what else could be the issue. I'd appreciate any advice or things to check.

Upvotes: 22

Views: 45648

Answers (3)

Hafeez Mohammad
Hafeez Mohammad

Reputation: 61


For those who are not able to find the App Password Page (2024)

Please follow the below steps


  1. Open a new tab in incognito mode.

  1. Open gmail account.

  1. Click on Manage account.

  1. Under security tab, Turn on the 2 step verification if not already done.

  1. Search for "App password" in the search option provided at the top.

  1. It will ask you to login again with your password. Login with the password.

  1. Now, You should be able to see the App password page.

Hope this helps ....

Upvotes: 6

GG.
GG.

Reputation: 21854

Edit January 2024:

Beginning September 30, 2024, Google Workspace will no longer support the sign-in method for third-party apps or devices that require users to share their Google username and password (the "Less secure app access" option).

OAuth and App Passwords will still work tho.

Announcement: https://workspaceupdates.googleblog.com/2023/09/winding-down-google-sync-and-less-secure-apps-support.html


Original answer:

Not sure if it's because my account is part of Google Workplace, but as of November 2023, I still have access to the "Less secure app access" option:

enter image description here

As the message says in the image, Google will eventually automatically turn it off and the message "535-5.7.8 Username and Password not accepted" will appear when trying to log in with SMTP.

The process to reenable it is as simple as:

  1. Log in to your Google account
  2. Go to "Manage your Google account"
  3. Go to "Security"
  4. Go to "Less secure app access"
  5. Turn it on

To not forget about it, I log the process into stdout when I catch the exception in my app:

with SMTP(SMTP_CONN_STRING) as smtp:
    try:
        smtp.ehlo()
        smtp.starttls()
        smtp.ehlo()
        smtp.login(SMTP_USERNAME, SMTP_PASSWORD)
    except SMTPAuthenticationError as e:
        logging.info(str(e))
        logging.info(
            "⚠️  Google automatically turned off the security option to log in"
            " with a username and a password."
        )
        logging.info(
            "⚠️  To enable it again: Log in to the Google account >"
            " Manage your Google account > Security > Less secure app access > Turn in on"
        )

Upvotes: 3

Linda Lawton - DaImTo
Linda Lawton - DaImTo

Reputation: 117016

Username and Password not accepted

Is the standard error message when you are trying to connect to Googles SMPT server and are trying to use the users actual google password.

As of May 30 2022 google has removed the less secure apps option. There is no way to turn this on as it no longer exists.

You have two options

  1. Enable 2fa on your google account and create an apps password and use that in place of your true password in your code.
  2. switch to using Xoauth2 most of the libraries support it. it will depend upon the language you are using though.

Quick fix for SMTP username and password not accepted error

Upvotes: 17

Related Questions