问题
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,
回答1:
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

回答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()
回答3:
The reason for this problem is that the password should be the client authorization code, not the login password of the mailbox.
回答4:
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"
- 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
来源:https://stackoverflow.com/questions/38602682/smtplib-smtpauthenticationerror-535-5-7-3-authentication-unsuccessful