8

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').

Kindly advise.

Thanks,

Puneet Nebhani
  • 367
  • 1
  • 5
  • 16
  • 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

6 Answers6

14

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
  • 367
  • 1
  • 5
  • 16
1

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()
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
  • 619
  • 8
  • 9
0

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

0

the low security methode was temporary and i couldn't use it in production but I found an article that made it easier using OAuth for authentication to GMail here with an example code and it works perfectly.

0

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

David J.
  • 1,313
  • 7
  • 37
  • 69