导航

python Email 之 Error:authentication failed解决方法

Posted on 2017-07-11 20:47  Young哥哥  阅读(2040)  评论(0)    收藏  举报

写邮件发送测试报告代码的时候,发现authentication failed 535 错误,特记下来,以备后用

#coding: utf-8  
import smtplib
from email.mime.text import MIMEText
from email.header import Header

sender = 'huochen1994@163.com'
receiver = 'huochen1994@126.com'
subject = 'python email test'
smtpserver = 'smtp.163.com'
username = 'huochen1994@126.com'
password = '*********'

msg = MIMEText( 'Hello Python', 'text', 'utf-8' )
msg['Subject'] = Header( subject, 'utf-8' )

smtp = smtplib.SMTP()
smtp.connect( smtpserver )
smtp.login( username, password )
smtp.sendmail( sender, receiver, msg.as_string() )
smtp.quit()

  在上述代码usernamepassword,变量中填写邮箱的帐号密码那么会看到以下报错:

Traceback (most recent call last):
  File "mail.py", line 18, in <module>
    smtp.login( username, password )  
  File "/usr/lib64/python2.6/smtplib.py", line 589, in login
    raise SMTPAuthenticationError(code, resp)
smtplib.SMTPAuthenticationError: (535, 'Error: authentication failed')

  

解决方法:

调用163邮箱服务器来发送邮件,我们需要开启POP3/SMTP服务,这时163邮件会让我们设置客户端授权码,这个授权码
替代上面代码部分的passwd即可成功发送邮件