python SMTP 发送邮件 阿里企业邮箱、163邮箱 及535错误

class SendEmail(object):
def __init__(self, type, to_addr):
self.to_addr = to_addr
self.sys_date = time.strftime('%Y-%m-%d', time.localtime())
content = u'这是测试邮件内容'if type == 'hichina': # 阿里企业邮箱
self.from_addr = 'username@520czj.com'
self.password = 'user password'
self.smtp_server = 'smtp.mxhichina.com'
else: # 163邮箱
self.from_addr = "username@163.com"
self.password = 'user password'
self.smtp_server = "smtp.163.com" def _format_addr(self, s):
name, addr = parseaddr(s)
return formataddr((Header(name, 'utf-8').encode(), addr.encode('utf-8') if isinstance(addr, unicode) else addr)) def send_email(self, content):
msg = MIMEText(content, 'plain', 'utf-8')
msg['From'] = self._format_addr(u'车装甲客服 <%s>' % self.from_addr)
msg['To'] = self._format_addr(u'经销商 <%s>' % self.to_addr)
msg['Subject'] = Header(u'送油通知', 'utf-8').encode() server = smtplib.SMTP(self.smtp_server, 25)
# server.set_debuglevel(1) # 打印debug信息
server.login(self.from_addr, self.password)
server.sendmail(self.from_addr, [self.to_addr], msg.as_string())
server.quit()

注意:

1. 阿里企业邮箱默认SMTP是开通的,但是给同一收件人半小时内不能发第二封

2. 163邮箱默认是不开通SMTP的,设置如下这里需要设置授权密码,设置之后程序里的password为此授权密码,不是登录密码;

python  SMTP 发送邮件 阿里企业邮箱、163邮箱 及535错误

3. 163邮箱抛异常:

raise SMTPAuthenticationError(code, resp)
smtplib.SMTPAuthenticationError: (535, 'Error: authentication failed')

两个原因1)没有设置客户端授权;2)密码错误;

上一篇:Western Subregional of NEERC, Minsk, Wednesday, November 4, 2015 Problem G. k-palindrome dp


下一篇:ZOJ 4010 Neighboring Characters(ZOJ Monthly, March 2018 Problem G,字符串匹配)