131

I am attempting to send an email in Python, through Gmail. Here is my code:

import smtplib


fromaddr = '......................'  
toaddrs  = '......................'  
msg = 'Spam email Test'  

username = '.......'  
password = '.......'

server = smtplib.SMTP('smtp.gmail.com', 587)  
server.ehlo()
server.starttls()
server.login(username, password)  
server.sendmail(fromaddr, toaddrs, msg)  
server.quit()

I get the error:

Traceback (most recent call last):
  File "email_send.py", line 18, in <module>
    server.login(username, password)
  File "C:\.....\Python\lib\smtplib.py", line 633
, in login
    raise SMTPAuthenticationError(code, resp)
smtplib.SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepte
d. Learn more at\n5.7.8 http://support.google.com/mail/bin/answer.py?answer=1425
7\n5.7.8 {BADCREDENTIALS} s10sm9426107qam.7 - gsmtp')

This seems to be a problem with the login. I am certain that my login details are correct, except for one thing. Should username be "blah@gmail.com", or simply "blah"? I tried both, same error.

Any idea whats wrong?

NOTE: all the periods are instead of password/email/file paths/etc.

Kai - Kazuya Ito
  • 6,454
  • 5
  • 44
  • 50
Jacob Kudria
  • 1,870
  • 3
  • 16
  • 18
  • 1
    On https://support.google.com/mail/answer/14257 possible issue can be captcha or other type of verification process that needs user interaction. Have you tried to login normally? – Robert Lujo May 12 '13 at 22:50
  • 2
    Exactly, Gmail is very unstable for sending mail through code, I experienced lots of problems when sending email from python code through gmail accounts, Yahoo accounts on the other hand work very good. – PepperoniPizza May 12 '13 at 23:06
  • 1
    http://stackoverflow.com/questions/16203071/sending-emails-in-python-weird-behaviour – PepperoniPizza May 12 '13 at 23:08
  • 3
    Your code works with my username@gmail.com. If I send wrong username or password I get the same error. – Txema May 14 '13 at 13:21
  • 1
    @Txema: yes, my password was incorrect. Thanks anyways! – Jacob Kudria May 14 '13 at 16:53
  • Use this link and turn on this option by logging in your desired email. https://myaccount.google.com/lesssecureapps Thanks. – Shawnock Guha Paul May 04 '21 at 20:22

10 Answers10

288

I ran into a similar problem and stumbled on this question. I got an SMTP Authentication Error but my user name / pass was correct. Here is what fixed it. I read this:

https://support.google.com/accounts/answer/6010255

In a nutshell, google is not allowing you to log in via smtplib because it has flagged this sort of login as "less secure", so what you have to do is go to this link while you're logged in to your google account, and allow the access:

https://www.google.com/settings/security/lesssecureapps

Once that is set (see my screenshot below), it should work.

Less Secure Apps

Login now works:

smtpserver = smtplib.SMTP("smtp.gmail.com", 587)
smtpserver.ehlo()
smtpserver.starttls()
smtpserver.ehlo()
smtpserver.login('me@gmail.com', 'me_pass')

Response after change:

(235, '2.7.0 Accepted')

Response prior:

smtplib.SMTPAuthenticationError: (535, '5.7.8 Username and Password not accepted. Learn more at\n5.7.8 http://support.google.com/mail/bin/answer.py?answer=14257 g66sm2224117qgf.37 - gsmtp')

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.

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.

UPDATE:: See my answer here: How to send an email with Gmail as provider using Python?

radtek
  • 30,748
  • 10
  • 135
  • 106
  • 1
    I have enabled it for less secure devices, but then also login doesn't seem to work for me. Is there any other thing else in which changes are to be made – proprius Feb 04 '16 at 11:22
  • @proprius, see my answer here http://stackoverflow.com/questions/10147455/how-to-send-an-email-with-gmail-as-provider-using-python/27515833#27515833 – radtek Feb 04 '16 at 17:46
  • This is true also for other email services. I had the same issue with Yahoo. So, be sure to have this option ENABLED, of your script, bot, etc., will not work. – ivanleoncz Mar 28 '19 at 18:16
  • This is also because when you're not using your gmail after turning `ENABLED` google automatically turns it off. – Ice Bear Dec 22 '20 at 07:03
  • Allowing "lesssecureapps" enables using smtp. Works after flipping the switch. Upvoted ! – Simplicity's_Strength May 27 '21 at 23:22
  • It's outdated. Google is not supporting this feature anymore! https://support.google.com/accounts/answer/6010255?hl=en&visit_id=637896899107643254-869975220&p=less-secure-apps&rd=1#zippy=%2Cuse-an-app-password – OmG Jun 01 '22 at 14:20
43

I had the same issue. The Authentication Error can be because of your security settings, the 2-step verification for instance. It wont allow third party apps to override the authentication.

Log in to your Google account, and use these links:

Step 1 [Link of Disabling 2-step verification]:

https://myaccount.google.com/security?utm_source=OGB&utm_medium=act#signin

Step 2: [Link for Allowing less secure apps]

https://myaccount.google.com/u/1/lesssecureapps?pli=1&pageId=none

It should be all good now.

Rahul Shenoy
  • 699
  • 6
  • 8
  • This also works! we just need to do `Step 2` cause google automatically brings the `less secure apps` to `off` if we don't use much the google account. – Ice Bear Dec 22 '20 at 07:06
21

If you're using smtp.gmail.com, then you have to do the following:

  1. Turn on the less secure apps

  2. You'll get the security mail in your gmail inbox, Click 'Yes,it's me' in that.

  3. Now run your code again.

Gaurav
  • 479
  • 4
  • 5
10

I had same issue. And I fix it with creating an app-password for Email application on Mac. You can find it at my account -> Security -> Signing in to Google -> App passwords. below is the link for it. https://myaccount.google.com/apppasswords?utm_source=google-account&utm_medium=web

Leo_Liu_MJ
  • 159
  • 1
  • 4
  • Note: You **have** to enable App Passwords if you have MFA on. I had to go to my administrator account: `admin.google.com -> security -> less secure enable.` Then I had to go to my personal account: `security -> App Passwords`. Then I could run a TLS connection & send an email. – kevin_theinfinityfund Jun 10 '21 at 05:13
7

if you are getting error this(535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8 https://support.google.com/mail/?p=BadCredentials o60sm2132303pje.21 - gsmtp')

then simply go in you google accountsettings of security section and make a less secure account and turn on the less secure button

vikash tiwary
  • 71
  • 1
  • 3
  • Less secure app access Some apps and devices use less secure sign-in technology, which makes your account vulnerable. You can turn off access for these apps, which we recommend, or turn it on if you want to use them despite the risks. Google will automatically turn this setting OFF if it’s not being used. Learn more Allow less secure apps: ON – vikash tiwary Sep 27 '19 at 07:22
6

If you turn-on 2-Step Verification, you need generate a special app password instead of using your common password. https://myaccount.google.com/security#signin

northcamel
  • 71
  • 1
  • 4
  • This is usually better than disablin' less secure apps because of security reasons, but they're complex so you can't remember them, this means either you have to like write it in a comment and erase it before publishing the final product or maybe store it in a database – PrabhavDevo Oct 07 '21 at 05:23
3

I had the same issue , i solved this by allowing "less secure app access" . This can be found in Security tab on Google Account: enter image description here

A. chahid
  • 99
  • 1
  • 4
0

Denied

The solution of using "Access for the less secure app" in Gmail has been denied (find more here).

Update

By the way, you can get access to the gmail account by the solution proposed by Google, called "App password". The solution is simple:

1. Active two-step verification of the corresponding account.
2. Create an app password. 
3. Exactly do the same implementation that you have for sending an email (explained in your question). 
   Except, replace the password with the generated app password (a sixteen digit password).

For more details you can also follow this post (it's working well for me).

OmG
  • 17,400
  • 7
  • 51
  • 81
-1

In my case, I allowed the access, but the problem was that I was using server.login('Name <email>', password). Please, make sure to use only your email here: server.login('youremail@gmail.com', password).

Response after change:

(235, '2.7.0 Accepted')

Response prior:

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 130sm13119106qkh.99 - gsmtp')
-2

Try turning on less secure apps and make sure that you have smtp.gmail.com or for different search up

Mining Pro
  • 17
  • 2