Selenium+pyhon中发送邮件遇到的问题排查

运行环境:pyhton2.7

 

1、【报错】

user = 'XXXXXX@qq.com'

password = 'XXXXX'

smtp = smtplib.SMTP()

smtp.connect(smtp_server)

smtp.login(user,password)

 

 

运行都此行代码smtp.login(user,password)报错提示验证失败:

smtplib.SMTPAuthenticationError: (535, 'Error: authenticationfailed')

 

【解决方案】

刚开始分析认为是用户名和密码写错,但检查了几次后没有任何错误,想起在使用雷鸟邮箱登录qq邮箱时也遇到过类似错误,最后的解决办法是:像QQ和163邮箱现在都有个客户端密码,用第三方登录时需用客户端密码(授权码,在邮箱设置中可进行)登录才行,python也是如此,因此去设置好客户端密码,再用客户端密码登录。

Selenium+pyhon中发送邮件遇到的问题排查

该问题解决;

2、发送邮件

【报错】

   import smtplib

from email.mime.text import MIMEText

   #发送邮件主题

   subject = 'python email test'

   #编写html类型的邮件正文

   msg = MIMEText('

hello,你好!

','html','utf-8')   

   smtp = smtplib.SMTP()

   smtp.connect(smtp_server)

   smtp.login('xuxiaojingtang@163.com','123456')

   smtp.sendmail('xuxiaojingtang@163.com',['[email protected]'],msg.as_string())

smtp.quit()

 

执行此代码完成后报错

Traceback (most recent call last):

 File "D:\Eclipsetest\selenium_Web\WebTest\test1225.py", line 87,in

   smtp.sendmail(sender, receiver,msg.as_string())

 File "D:\SoftwareInstallation\Python27\lib\smtplib.py", line 746,in sendmail

   raise SMTPDataError(code, resp)

smtplib.SMTPDataError: (554, 'DT:SPM 163smtp8,DMCowAC3Lep4wmdYju2rGw--.5337S2 1483195001,please seehttp://mail.163.com/help/help_spam_16.htm?ip=123.163.174.7&hostid=smtp8&time=1483195001')

 

【解决方案】

网上查询信息:报错error554是因为信封发件人和信头发件人不匹配。可以看出图片中并没有发件人和主题,所以修改代码如下:

   import smtplib

from email.mime.text import MIMEText

   #发送邮件主题

   subject = 'python email test'

   #编写html类型的邮件正文

msg = MIMEText('

hello,你好!

','html','utf-8')   

  msg['From'] = '[email protected] '

  msg['Subject'] = Header(subject, 'utf8').encode()

msg['To'] = '[email protected] '

   smtp = smtplib.SMTP()

   smtp.connect(smtp_server)

   smtp.login('xuxiaojingtang@163.com','123456')

   smtp.sendmail('xuxiaojingtang@163.com',['[email protected]'],msg.as_string())

smtp.quit()