/***************************************************************************
* python auto send email
* 声明:
* 本文主要是记录如何使用python的smtplib发邮件,中间遇到授权密码和邮箱
* 密码不同的问题。
*
* 2016-2-17 深圳 南山平山村 曾剑锋
**************************************************************************/
一、参考文章:
163邮箱报错WARN: 535 Error: authentication failed.啥问题?
http://www.zhihu.com/question/32009096
二、error:
1. 错误现象:
(535, 'Error: authentication failed')
2. 解决办法:
smtplib用的邮箱登入密码是授权密码,不是邮箱密码。授权密码需要在邮箱设置中设置,我用的是163的邮箱,所以需要在163邮箱中设置。
三、demo code:
# encoding: utf-8
import smtplib
sender = "zengjf42@163.com"
receivers = ["64128306@qq.com"]
message = """From: zengjf <zengjf42@163.com>
To: zoro <64128306@qq.com>
Subject: test email for python
this is a test email.
"""
try:
smtpObj = smtplib.SMTP()
smtpObj.connect("smtp.163.com", "25")
# 千万请注意下面的password是授权密码,不是邮箱的密码。
# 授权密码需要在163邮箱设置中设置。
state = smtpObj.login("zengjf42@163.com", "填入授权密码")
if state[0] == 235:
smtpObj.sendmail(sender, receivers, message)
print "send email success"
smtpObj.quit()
except smtplib.SMTPException, e:
print str(e)