'smtplib.SMTPAuthenticationError, even when I created an application password (Python)?

I've been researching for days on Stack Overflow on how to send an email via Gmail using smtplib. I've finally got the gist of what I'm supposed to do, if I don't want to turn on "Less secure app access." I've switched to using two-step verification, and I created an application password for my python program to use.

Here is my code:

import smtplib, ssl
from email.mime.text import MIMEText

def send_email_gmail(subject, message, destination):
    # First assemble the message
    msg = MIMEText(message, 'plain')
    msg['Subject'] = subject

    # Login and send the message
    port = 465
    my_mail = '[email protected]'
    my_password = 'thepasswordtouse'
    context = ssl.create_default_context() 
    with smtplib.SMTP_SSL('smtp.gmail.com', port, context=context) as server:
        server.ehlo()
        server.login(my_mail, my_password)
        server.sendmail(my_mail, destination, msg.as_string())


send_email_gmail('Test subject', 'This is the message', '[email protected]')

However, when I run the above code, I get a smtplib.SMTPAuthenticationError:

Traceback (most recent call last):
  File "/home/user/programs/python/testing/test.py", line 21, in <module>
    send_email_gmail('Test subject', 'This is the message', '[email protected]')
  File "/home/user/programs/python/testing/test.py", line 17, in send_email_gmail
    server.login(my_mail, my_password)
  File "/usr/lib/python3.8/smtplib.py", line 743, in login
    raise last_exception
  File "/usr/lib/python3.8/smtplib.py", line 732, in login
    (code, resp) = self.auth(
  File "/usr/lib/python3.8/smtplib.py", line 655, in auth
    raise SMTPAuthenticationError(code, resp)
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 w10sm17413068qtk.90 - gsmtp')

I've tried placing server.ehlo() various places around the code, I've tried using SSL and not using SSL, I've tried using different code structure, I've tried creating and re-creating new application passwords, I've tried logging in using my normal account and username, I've tried using my user name and my email address for the username argument of server.login(), I've tried using spaces in the password and no spaces, all of it gives the same error message. Also, this code is running on the same device that I normally log in on, so it doesn't have to do with the fact that it's an unknown device.

Can anyone shed some light on why I can't log in to my Gmail account, and how to make it so that I can?



Solution 1:[1]

I suggest not having to import the SSL function and deleting the below line of code

context = ssl.create_default_context()

Try this instead

    server = smtplib.SMTP_SSL('smtp.gmail.com', 465)

    # Perform operations via server
    server.login(my_mail, my_password)
    server.sendmail(my_mail, destination, msg.as_string())
    server.quit()

The link to the full code is here

Code Link

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 capitalistlion