flask-mail利用163邮箱发送邮件给qq邮箱

1.配置信息

1)在163的设置中,开启客户端的授权密码

2)配置参数

app.config['MAIL_SERVER'] = 'smtp.163.com'
app.config['MAIL_PORT'] = 25
app.config['MAIL_USERNAME'] = 'xxx@163.com'
app.config['MAIL_PASSWORD'] = 'xxx'
app.config['FLASKY_ADMIN'] = 'xxx@qq.com'

2.使用python shell 模式来测试配置是否正确

from test import Message
from test import mail

msg = Message('test subject', sender=app.config['MAIL_USERNAME'], recipients=[to]) # 注意[]必须有,否则将会报错
msg.body = 'test body'
msg.html = 'test html'
with app.app_context():
    mail.send(msg)

3.配置成功后,可以使用程序来发送邮件

def send_mail(to, title):
    msg = Message(title, app.config['MAIL_USERNAME'], recipients=[to])
    msg.body = 'test body'
    msg.html = 'test html'
    thr = Thread(target=send_async_email, args=[app, msg])
    thr.start()
    return thr

def send_async_email(app, msg):
    with app.app_context():
        mail.send(msg)

创建新的线程thr来执行发送邮件操作,达到异步的效果,否则将可能出现停滞几秒,浏览器像无响应一样的状态。

 

参考文献:《基于python 的web应用开发》

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值