语言:python
一、问题描述:
在使用python的smtplib模块测试自动发送邮件时,遇到(535, b'Error: authentication failed')报错。代码片段如下:
def mail(message):
ret = True
current_dt = time.strftime("%Y-%m-%d", time.localtime())
# current_dt = datetime.strptime(current_dt, '%Y-%m-%d')
title = current_dt.split(" ")[0] + "程序test"
msg = MIMEText(message,'plain','utf-8')
msg['From'] = formataddr(["ai司徒", my_sender]) # 发件人昵称
msg['To'] = formataddr(["司徒", my_user]) # 接收人昵称
msg['Subject'] = title # 邮件的主题
server = smtplib.SMTP_SSL("smtp.163.com", 994) # 发件人邮箱中的SMTP服务器
server.login(my_sender, my_pass) # 发件人邮箱账号、邮箱授权码
server.sendmail(my_sender, [my_user,], msg.as_string())
server.quit() # 关闭连接
return ret
二、问题产生原因:
可能是smtp服务的邮箱身份验证失败,检查登录环节
三、解决方案:
检查后发现需要使用邮箱的smtp服务授权码而非密码,修改后运行成功。