smtplib.SMTPAuthenticationError: (535, '5.7.3 Authentication unsuccessful')

Question:

I am trying to use smtplib for sending mails in python 2.7. The below code is pretty simple:

import smtplib

def main(argv=None):

    sender = '[email protected]'
    receivers = ['[email protected]']      

    message = """
    This is a test e-mail message.
    """

    smtpObj = smtplib.SMTP('[email protected]',25)

    smtpObj.login('abc', 'pwd')       
    smtpObj.sendmail(sender, receivers, message)         
    print "Successfully sent email"


if __name__ == '__main__':
    main()

Now when I execute the below code, I keep getting this exception:

smtplib.SMTPAuthenticationError: (535, '5.7.3 Authentication unsuccessful').
Asked By: Puneet Nebhani

||

Answers:

had the same issue.

2 options,

try changing:

smtpObj = smtplib.SMTP('[email protected]',25)

to:

smtpObj = smtplib.SMTP('[email protected]',587)

Other option is that your login is not correct. In my case im using exchange and the login name is not email adres but just username

Here is the code im using for exchange server:

import smtplib

def sendmail():
    subject = 'message subject'
    to = '[email protected]'
    sender = 'bjorn@***.nl'
    smtpserver = smtplib.SMTP("mail.mymailserver.nl",587)
    user = 'myussername'
    password = 'mypassword'
    smtpserver.ehlo()
    smtpserver.starttls()
    smtpserver.ehlo
    smtpserver.login(user, password)
    header = 'To:' + to + 'n' + 'From: ' + sender + 'n' + 'Subject:' + subject + 'n'
    message = header + 'n This is my message'
    smtpserver.sendmail(sender, to, message)
    smtpserver.close()
Answered By: user2433624

Actually, when I tried executing the same statements on python console, I came to know that the password was incorrect and it was due to different character encoding.

For all other users, refrain yourself from copy paste

two muppets

Answered By: Puneet Nebhani

The reason for this problem is that the password should be the client authorization code, not the login password of the mailbox.

Answered By: weiliang

This Problem is With Your Gmail if You double checked Your credentials.
You Can do following steps to resolve it:

1.Enable IMAP and/or POP3:

 1. Go to the "Settings", e.g. click on the "Gears" icon and select
    "Settings".
 2. Click on "Forwarding and POP/IMAP".
 3. Enable "IMAP Access" and/or "POP Download"
  1. Allow Less Secure Apps to sign in Your Gmail.
 1. Login with your gmail account and find "Allow less secure apps:"
    from [Here][1]
 2. Google manages security with your gmail account. You need to turn on "Allow 
      less secure apps:" and you will receive mail in your gmail account.
      [1]: https://myaccount.google.com/security#activity
Answered By: Devesh

You can also try changing the port from 587 to 25. This worked for me.

Answered By: David J.

For completeness answering this question, I have encountered exactly the same error multiple times – each time for a different reason. This error message has been haunting me and I kind of mutated into a self-proclaimed expert on this very error message. Here is a list of all the issues I faced with it and how I solved it.

  • Needed to enable "less secure app" in your Google Admin (https://admin.google.com/ac/security/lsa)

  • Even if the above is enabled, the service might fail if you have a two-factor authentication setup in your email. The solution is to create an app specific password (https://myaccount.google.com/u/5/apppasswords) that must be used as the password for smtplib in Python instead.

  • If you do this for the first time with an unknown IP. There may be a secret Captcha priming the authentication. You may see then actually a 534 error code instead of a 535 but I can’t guarantee that. You will need to click on a link first to clear the Captcha (https://accounts.google.com/DisplayUnlockCaptcha). You might need to wait 10 min before it actually registers it. So grab a coffee.

  • Finally, you will get the same error message, if you have not opened the correct port. I had port 587 not opened in firewalld. I had smtp and smtps as services enabled, but these only cover ports 25 and 465 by default. So, if you use 587, make sure it is open, e.g. for firewalld, sudo firewall-cmd –permanent –add-port=587/tcp and restart your firewalld service.

  • Lastly, this mail still fail if you have not setup your SSL correctly at your server. At some point I had the error message and upon further investigation in some logs I saw it failed to do a handshake.

  • Update lastly: I had the ehlo() accidently at the wrong place in my code that suddently started to throw the 535 Authentication error, that is:

    session = smtplib.SMTP(‘smtp.gmail.com’, 587)

    session.starttls()
    session.login(sender_address, sender_pass)

    session.ehlo("yourdomain.com") # <– Make sure this comes after starttls() and not before

Answered By: Majte

Google has removed this feature as of May 30th, 2022.

To help keep your account secure, from May 30, 2022, ​​Google no longer supports the use of third-party apps or devices which ask you to sign in to your Google Account using only your username and password.

Important: This deadline does not apply to Google Workspace or Google Cloud Identity customers. The enforcement date for these customers will be announced on the Workspace blog at a later date.

Answered By: Vincent Casey
import smtplib

my_email = "[email protected]"
rec_emil = "[email protected]"
password = "you_password"
message = "Hi there"

with smtplib.SMTP(host="smtp.gmail.com", port=587) as connection:
connection.starttls()
connection.login(user=my_email, password=password)
connection.sendmail(
    from_addr=my_email,
    to_addrs=rec_emil,
    msg=message.encode("utf-8")
    )
  1. Under the security setting set up two-factor authentication.
  2. Select the App password then click on "other app" and choose "other".
  3. at the final type any name and click on "Generate".
    you will be shown a new password. use this password in your app.
    Check your spam if you don’t find the email in your inbox. You can also confirm an email for sender-sent items.
Answered By: Anees Ahmad
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.