SMTP authentication issue with Python – Error 535: Username and Password not accepted

I’m encountering an issue while running a Python script (test_smtp.py) that involves SMTP authentication with Gmail. The specific error message I’m getting is:

Error: (535, b'5.7.8 Username and Password not accepted. For more information, go to\n5.7.8 https://support.google.com/mail/?p=BadCredentials w14-20020adfee4e000000b0033609750752sm6314914wro.8 - gsmtp')

Here’s the content of the test_smtp.py script:

import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from configparser import ConfigParser

def send_test_email():
    config = ConfigParser()
    config.read("config.ini")

    sender_email = config.get("Email", "sender_email")
    receiver_email = config.get("Email", "receiver_email")
    password = config.get("Email", "password")

    message = MIMEMultipart("alternative")
    message["Subject"] = "Test Email"
    message["From"] = sender_email
    message["To"] = receiver_email

    text = "Just a test email."
    part1 = MIMEText(text, "plain")
    message.attach(part1)

    try:
        with smtplib.SMTP_SSL("smtp.gmail.com", 465) as server:
            server.login(sender_email, password)
            server.sendmail(sender_email, receiver_email, message.as_string())
        print("Email sent successfully.")
    except Exception as e:
        print("Error sending email: ", e)

if __name__ == "__main__":
    send_test_email()

I have verified that the .ini file (config.ini) is correct. The relevant code for reading the configuration is as follows :

[Email]
sender_email = [email protected]
receiver_email = [email protected]
password = passwword :(

The script (test_smtp.py) is expected to send a test email using the provided Gmail account credentials.

  • i get the same error. i believe that you have to sign in to the email provider and authenticate in some way. It will be useful to see an answer. . .

    – 




  • This might help stackoverflow.com/q/67360013/1841839

    – 

  • Does this answer your question? Why when I compile I get username and password are not accepted?

    – 

Leave a Comment