官方参考文档:https://pythonhosted.org/Flask-Mail/
报错1:smtplib.SMTPAuthenticationError(错误原因:看看你的配置参数后面是否有逗号,网上复制的代码有逗号)
smtplib.SMTPAuthenticationError: (535, b'Error: \xc7\xeb\xca\xb9\xd3\xc3\xca\xda\xc8\xa8\xc2\xeb\xb5\xc7\xc2\xbc\xa1\xa3\xcf\xea\xc7\xe9\xc7\xeb\xbf\xb4: http://service.mail.qq.com/cgi-bin/help?subtype=1&&id=28&&no=1001256')
flask-mail配置参数设置
#python语法后面没有逗号,网上复制的代码小心....
MAIL_SERVER = 'smtp.qq.com'
MAIL_PORT = 465
MAIL_USE_TLS = False
MAIL_USE_SSL = True
#这里不是密码是授权码(切勿搞错了)
MAIL_PASSWORD = 'xxxxxxxxxxxxxx'
MAIL_USERNAME = '528185681@qq.com'
报错2:flask-mail TypeError: getaddrinfo() argument 1 must be string or None(错误原因:配置参数MAIL_PASSWORD不是密码,而是邮箱的授权码)
一个完整的成功案例:
1、配置文件
AIL_SERVER = 'smtp.qq.com'
MAIL_PORT = 465
MAIL_USE_TLS = False
MAIL_USE_SSL = True
#这里不是密码是授权码(切勿搞错了)
MAIL_PASSWORD = 'xxxxxxxxxxxxx'
MAIL_USERNAME = '528185681@qq.com'
2、实现代码
from kirin_app import mail
from flask_mail import Message
class Email:
def __init__(self):
self.msg = Message("第一封邮件标题",
sender="from@qq.com",
recipients=["to@163.com"],
body="test")
def send_email(self):
mail.send(self.msg)
Email().send_email()