按书上的内容结果报出服务器拒绝
于是换qq邮箱服务器,结果报如下错误:
smtplib.SMTPAuthenticationError:(535,b'Error:\xc7\xeb\xca\xb9\xd3\xc3\xca\xda\xc8\xa8\xc2\xeb\xb5\xc7\xc2\xbc\xa1\xa3\xcf\xea\xc7\xe9\xc7\xeb\xbf\xb4: http://service.mail.qq.com/cgi-bin/help?subtype=1&&id=28&&no=1001256')
原因在于邮箱密码不能直接输入自己的密码:
因此找到MAIL_PASSWORRD的正确值,以下是详细方法:
然后往下拉,找到下面的,点击开启,然后按步骤发送短信,会恢复一串字符,然后写入MAIL_PASSWORRD处。
以下是完整简短实例代码:
from flask import Flask
from flask_mail import Mail, Message
app = Flask(__name__)
app.config.update(
DEBUG = True,
MAIL_SERVER='smtp.qq.com',
MAIL_PROT=25,
MAIL_USE_TLS = True,
MAIL_USE_SSL = False,
MAIL_USERNAME = '2531412**4@qq.com',
MAIL_PASSWORD = 'x**********if',
MAIL_DEBUG = True
)
mail = Mail(app)
@app.route('/')
def index():
msg = Message("test ",sender='2531412**4@qq.com', recipients=['187****7785@163.com'])#改成你自己的邮箱,并且第一处必须与上面配置的相同。
msg.body = "This is a first email"
msg.html = 'HTML body'
with app.app_context():
mail.send(msg)
print("Mail sent successful!")
return "Mail sent successful!"
if __name__ == "__main__":
app.run()