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

Question:

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

Asked By: Sylvester Kruin

||

Answers:

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

Answered By: capitalistlion

I’ve finally found the cause of the problem.

I discovered this answer when looking for the solution to another related problem I’ve been having where I can’t sign in to my Gmail account using Thunderbird. After searching for a solution, I came across this message, which says:

I think I have finally come to the bottom of this issue.
It is described here:
https://support.google.com/mail/thread/127545584/i-have-created-a-supervised-account-and-i-m-want-to-add-it-to-my-mail-app-in-mac-but-its-offline?hl=en&msgid=128001398

Basically, according to recommended answer, it boils down to the following sentence:

"Thanks. AFAIK, Accounts managed by Family Link are not allowed to sign in via email clients. I was unable to get my test child account added to Thunderbird. "

Since I have recently started using Family Link to limit screen time on my phone,
it seems that it prevents me from accessing Google Mail on Thunderbird.

My Gmail account is also managed by Family Link, so in order access my Gmail account using a third-party email client (like Thunderbird or, as in this case, a Python program), I’ll have to disable Family Link.

Answered By: Sylvester Kruin
Categories: questions Tags: , , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.