Gmail SMTP send - 535-5.7.8 Username and Password not accepted
Asked Answered
M

3

16

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.

Margit answered 5/1, 2023 at 16:57 Comment(0)
B
12

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

Bug answered 5/1, 2023 at 17:4 Comment(4)
Doesn't work for me even with an apps password. Pretty disappointed, although I watched your video and it explains everything really well, so thank you!Borgeson
open a new question with your code and i will have a look it should work fine.Bug
I was facing a similar issue, but the strange part is, my Laravel app can't send email via local environment but it can from staging server. Its driving me nuts. Well, at least your answer give enough reason to switch to MailTrap in local.Bothy
I would love to see your code if you want to send it to me or open a question. Fixing random errors is a hobby 😁Bug
W
2

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 ....

Wahl answered 21/5 at 17:48 Comment(0)
A
1

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"
        )
Auvergne answered 30/11, 2023 at 18:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.