When I am trying to use the SMTP into my Python code for email services using Gmail account, I am not able to use it and throwing the below error:
smtplib.SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8 https://support.google.com/mail/?p=BadCredentials c12-20020aa7d60c000000b0042be14040c1sm2612116edr.86 - gsmtp')
Let’s get started with this article, where we’ll learn about all of the possible solutions.
What does smtplib.SMTPAuthenticationError: Username and Password not accepted Python Error Mean?
Google has recently posted on it’s official website that it is going to disable the support for third-party apps or devices which only ask for the google sign-in using only username and password, there should be multi-factor authentication to allow the use or access. Below was the google official announcement:
To help keep your account secure, from May 30, 2022, Google no longer supports the use of third-party apps or devices which ask you to sign in to your Google Account using only your username and password. Important: This deadline does not apply to Google Workspace or Google Cloud Identity customers. The enforcement date for these customers will be announced on the Workspace blog at a later date.
Thats why when use the SMTP into Python for email services, it throws the below error and email service doesn’t work into Python.
smtplib.SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8 https://support.google.com/mail/?p=BadCredentials c12-20020aa7d60c000000b0042be14040c1sm2612116edr.86 - gsmtp')
Why smtplib.SMTPAuthenticationError: Username and Password not accepted Error Occurs in Python?
When using the Email feature into Python using SMTP, it is impacted with google recent announcement 30-May-2022 of not to support the third-party apps or devices which only ask for the google sign-in using only username and password.
How to Fix the smtplib.SMTPAuthenticationError: Username and Password not accepted Error in Python?
To Fix the error, First enable the “2-Step Verification” in google account and then enable the “App Passwords” features to enable the 3-party services access using 2-Step verification method.
Below is the Step-by-Step process to Fix the smtplib.SMTPAuthenticationError: Username and Password not accepted Error in Python using Gmail account post 30-May-2022 Google’s Announcement:
Step 1: Login to Google Account
First of all, login to your Google account and Click on the “Security” tab.

Step 2: Click on “2-Step Verification” Tab
Click on the Security tab and then click on “2-Step Verification” Tab.

Step 3: Click on “Get Started” Option
You would be able to see the “Get Started” option below to proceed further, click on that.

Step 4: Click “Ok” to Proceed
Now click the prompted Ok button to proceed further.

Step 5: Login to Your Google account
Now it would ask you to enter the account credentials to verify that you are the authorised person to enable the 2-Step verification; you are authorised to make settings change to your Google account.

Step 6: Yes to Prompt on Your registered device
If you have registered any phone or tab for login prompt as login method, you need to allow it on prompted device.

Step 7: Add Login Backup Option
You need to add the optional backup login option incase you lose your phone or secondary option doesn’t work.

Step 8: Click “turn On” Button
Finally, you just need to click on the “turn On” button to enable the 2-Step Verification. this is the final step.

Step 9: Click on “App Passwords” Button to Enable
Once 2-Step verification is enabled for yours Google account, you would start seeing the “App Passwords” option in Security Tab. Default value for “App passwords” is set to “None”. Click on “App Passwords” Option to enable it.

Step 10: Login to Google account to Enable “App Passwords” Option
Now, it would ask you to insert the Google account password for verification purpose that you are authorised person to make the changes into Google account.

Step 11: Select Apps and Devices to enable “App Passwords”
Here you would see the option for selecting Apps and Devices for enabling the 3rd-Party Apps access using Multi-factor authentication.


Step 12: Use the “Generated App Password”
Now you would see the “Generated App Password” for that particular App.

In same way, you can create the passwords for multi Apps for logging on using Google.
Once you generate the App Passwords by enabling the “App Passwords” into yours Google account, use the App password into Python code to fix the smtplib.SMTPAuthenticationError: Username and Password not accepted Error.
Conclusion
Due to Google recent announcement that “To help keep your account secure, starting May 30, 2022 , Google will no longer support the use of third-party apps or devices that only ask for your username and password for you” SMTP Email feature isn’t working into Python and throws the Error smtplib.SMTPAuthenticationError: Username and Password not accepted.
To Fix the smtplib.SMTPAuthenticationError: Username and Password not accepted Error, as Google is recommending to use the “2-Step Verification” for 3rd-Party Apps access or devices and then enable the “App Password” feature to access the Apps. Once the App password is generated, use it into Python code to fix the error.
To Enable the “App Passwords” features for Apps access using Google account, First enable the “2-Step Verification” option and then Google would allow you to enable the “App Passwords” feature to set the Password for specific App access on basis of App and Devices parameter.
Hope it should help you to fix the smtplib.SMTPAuthenticationError: Username and Password not accepted Python Error with Gmail Account.