1

I am trying to sign in into my Gmail to send emails from Python, exactly from SMTPlib library.

I checked some answers here and most of them mention that I should allow Less secure apps, I did that already, I also have 2-Step Authentication turned off.

Here is exactly what I tried :

import smtplib, ssl
from email.message import EmailMessage

my_email = "xxxxx@gmail.com"
my_password = "Mypasswrd"
other_email = "xxxxx@gmail.com"

email = EmailMessage()
email['from'] = my_email
email['to'] = other_email
email['subject'] = 'Test'
email.set_content('Just testing')
smtp = smtplib.SMTP(host='smtp.gmail.com',port=587)
smtp.ehlo()
smtp.starttls()
smtp.login(my_email,my_password)

And here is the error message I keep getting:

smtplib.SMTPAuthenticationError: (534, b'5.7.14 <https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbv\n5.7.14 MKRQdTnq4XnXPAPVi9BKgDrbJ-EarzJPoivk3G1WLburL60D14QnkyWEy78Xj_kvdgcuQ\n5.7.14 ATCC_NvPh1JEpdKSCWlfKMdiP6ZvqsoqCVeQXpRQMqpq_Js25fKsdnZG_0A8sLLL>\n5.7.14 Please log in via your web browser and then try again.\n5.7.14  Learn more at\n5.7.14  https://support.google.com/mail/answer/78754 21sm9494384wme.6 - gsmtp')

Any help please?

Thanks.

1 Answers1

2

If you are 100 % sure that your username and password are correct, I believe you are looking for this answer, namely this part of it:

Still not working? If you still get the SMTPAuthenticationError but now the code is 534, its because the location is unknown. Follow this link:

https://accounts.google.com/DisplayUnlockCaptcha

Click continue and this should give you 10 minutes for registering your new app. So proceed to doing another login attempt now and it should work.

UPDATE: This doesn't seem to work right away you may be stuck for a while getting this error in smptlib:

235 == 'Authentication successful' 503 == 'Error: already authenticated'

The message says to use the browser to sign in:

SMTPAuthenticationError: (534, '5.7.9 Please log in with your web browser and then try again. Learn more at\n5.7.9 https://support.google.com/mail/bin/answer.py?answer=78754 qo11sm4014232igb.17 - gsmtp')

After enabling 'lesssecureapps', go for a coffee, come back, and try the 'DisplayUnlockCaptcha' link again. From user experience, it may take up to an hour for the change to kick in. Then try the sign-in process again.

My Work
  • 2,143
  • 2
  • 19
  • 47