10

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 = 'abc@gmail.com'
    receivers = ['xyz@gmail.com']      

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

    smtpObj = smtplib.SMTP('xyz@gmail.com',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').
tripleee
  • 175,061
  • 34
  • 275
  • 318
Puneet Nebhani
  • 487
  • 1
  • 7
  • 17
  • If you are trying to authenticate against Office 365, this answer may be helpful: https://stackoverflow.com/a/49287610/3204023 – jdhildeb Mar 14 '18 at 21:06

8 Answers8

22

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

Puneet Nebhani
  • 487
  • 1
  • 7
  • 17
5

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

Majte
  • 264
  • 2
  • 9
2

had the same issue.

2 options,

try changing:

smtpObj = smtplib.SMTP('xyz@gmail.com',25)

to:

smtpObj = smtplib.SMTP('xyz@gmail.com',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 = 'mail@yourdomain.com'
    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()
user2433624
  • 59
  • 1
  • 6
  • Still not understood why the port 25 is not working, worked with port 587, whereas from other service 25 we use for smtp. still not clear why not worked on 25. Anyways thanks for answer. – Indrajeet Gour Aug 17 '23 at 20:12
  • 1
    port 25 is non tls and 587 us tls, gmail changes there policy to only allow encrypted traffic there for you need to use the tls – user2433624 Aug 21 '23 at 11:19
2

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.

Vincent Casey
  • 178
  • 2
  • 11
1

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
Devesh
  • 929
  • 12
  • 10
0

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

0

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

David J.
  • 1,753
  • 13
  • 47
  • 96
  • Port 25 is generally blocked. Randomly changing the port number is unlikely to help; instead, check the documentation of your email provider, or check with an admin. Port 25 will generally be correct for an internal server (e.g. inside a corporate network, trying to send email to the outside world) but port 587 is usually correct for any public-facing SMTP server, and then usually requires that you have an account with the provider, and authenticate using those credentials. – tripleee Aug 29 '21 at 13:27
0
import smtplib

my_email = "your_email@gmail.com"
rec_emil = "someone@gmail.com"
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.