先在qq邮箱设置中打开smtp并获取授权码
from flask import Flask
from flask_mail import Message, Mail
from flask_script import Manager
app = Flask(__name__)
app.config['MAIL_SERVER'] ='smtp.qq.com'
app.config['MAIL_USERNAME'] ='【qq邮箱,即登录名】'
app.config['MAIL_PASSWORD'] ='授权码'
app.config['MAIL_USE_TLS'] =True【此处必须设置为True,其中TLS是指的是传输层安全协议】
manager = Manager(app)
@app.route('/')
def index():
msg = Message(
subject='test',
recipients=['【接收邮箱,列表形式】'],
body='haha',
sender='【qq邮箱】'
)
mail = Mail(app)
mail.send(msg)
return 'mail 发送成功!'
if __name__ =='__main__':
manager.run()
在terminal中输入命令启用服务器即可成功发送