Search code examples
pythonsmtpgmailsmtpclient

Login Authentication failed with Gmail SMTP (Updated)


When trying to login into a Gmail account using SMTP, this error message occurs:

SMTPAuthenticationError(code, resp)
smtplib.SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted.

Code causing the error:

import smtplib

server = smtplib.SMTP("smtp.gmail.com", 587)
server.starttls()
server.login("[email protected]", "your_password")
message = "TEST"

server.sendmail("[email protected]", "[email protected]", message)
server.quit()

Solution

  • Google has disabled the ability to enable less secure apps as of May 2022. Because of this, the previous solution of enabling less secure apps is no longer valid.

    Steps:

    1. Go into your sending email address and make your way to the settings.
    2. Find two-step authentication and enable it.
    3. Under two-step authentication there should be a tab labeled App passwords. Click on it then select mail as the app and your device of choice
    4. Use the password generated from the app password as the password for your Gmail account.

    Credits to: Link to source