app.config['MAIL_SERVER'] = 'smtp.qq.com'
app.config['MAIL_PORT'] = 465
app.config['MAIL_USE_TLS'] = True
app.config['MAIL_USERNAME'] = os.environ.get('MAIL_USERNAME')
app.config['MAIL_PASSWORD'] = os.environ.get('MAIL_PASSWORD')
在环境中配置(所在平台为windows,假设我邮箱为6666@qq.com, 密码也为6666):
set MAIL_USERNAME = 6666 #only an example, int type
set MAIL_PASSWORD = 6666 #int type
然后进入python的shell输入
from flask.ext.mail import Message
from hello import mail #hello就是我的model(.py文件)
msg = Message('subject', sender='6666@qq.com', recipients=['6666@qq.com'])
msg.body = 'body content'
msg.html = '<h1>Why it doesn't work?</h1> body'
with app.app_context():
mail.send(msg)
运行后一直无反应,没结果出现。将mail_server换成mx1.qq.com后直接出现TimeoutError,10060
感觉应该是qq邮箱的配置方面出错,但具体找不出在哪里。求教原因,谢谢!
你应该先到QQ邮箱里面开启smtp服务。操作过程为【QQ邮箱首页】->【设置】->【账户】->【POP3/SMTP服务】,按提示把这个服务开启后,你会得到一串码,记住这串码。然后配置的时候username为你的QQ邮箱账户,密码填上刚才那串码即可。