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邮箱的配置方面出错,但具体找不出在哪里。求教原因,谢谢!
You should first go to your QQ mailbox to enable the smtp service. The operation process is [QQ Mail Home Page]->[Settings]->[Account]->[POP3/SMTP Service]. After following the prompts to turn on this service, you will get a string of codes. Remember this string of codes. . Then when configuring, the username is your QQ mailbox account, and the password is filled in with the string of numbers just now.