1 from flask_mail import Mail,Message 2 from flask import Flask 3 import os 4 5 app=Flask(__name__) 6 app.config.update( 7 DEBUG = True, 8 MAIL_SERVER=‘smtp.qq.com‘, 9 MAIL_PROT=25, 10 MAIL_USE_TLS = True, 11 MAIL_USE_SSL = False, 12 MAIL_USERNAME = ‘[email protected]‘, 13 MAIL_PASSWORD = ‘xxxxxxxx‘, 14 #MAIL_PASSWORD = ‘xxxxxxxx‘, 15 MAIL_DEBUG = True 16 ) 17 mail=Mail(app) 18 19 20 @app.route(‘/‘) 21 def index(): 22 # sender 发送方哈,recipients 邮件接收方列表 23 msg = Message("Hi!This is a test ",sender=‘[email protected]‘, recipients=[‘[email protected]‘]) 24 # msg.body 邮件正文 25 msg.body = "This is a first email" 26 # msg.attach 邮件附件添加 27 # msg.attach("文件名", "类型", 读取文件) 28 # with app.open_resource("F:\2281393651481.jpg") as fp: 29 # msg.attach("image.jpg", "image/jpg", fp.read()) 30 31 mail.send(msg) 32 print ("OK") 33 return "Sent" 34 35 36 37 38 if __name__==‘__main__‘: 39 app.run(debug=True)
发送一直失败,查网上比较靠谱的方法参考两篇文章
http://blog.csdn.net/huochen1994/article/details/51282093
http://tieba.baidu.com/p/3870714637 这个帖子的最后一答
时间: 2024-12-15 13:36:49