首页 > 解决方案 > 身份验证出错时,烧瓶邮件引发异常

问题描述

首先,我要感谢大家。

我有一个关于烧瓶邮件的快速问题。

我想在发送电子邮件时遇到异常或捕获错误。

from flask import Flask, request
from flask_mail import Mail, Message
import os, sys

app = Flask(__name__)

mail= Mail(app)
app.config['MAIL_SERVER']='smtp.gmail.com'
app.config['MAIL_PORT'] = 465
app.config['MAIL_DEBUG'] = True
app.config['MAIL_USERNAME'] = 'xyz@gmail.com'
app.config['MAIL_PASSWORD'] = 'njhkjkjj'
app.config['MAIL_USE_TLS'] = False
app.config['MAIL_USE_SSL'] = True
mail = Mail(app)

@app.route('/email')
def get_email():
    if mail.connect() == None:
        print("Email setup/password is incorrect. Input your email creadentials in python app..")
    else:
        msg = Message('visitor blocked', sender = 'abc@gmail.com', recipients = ['xyz@gmail.com'])
        msg.body = "Hello, this is email test."
        mail.send(msg)
        print("Email setup is ready. Email is sent..")

我试图得到一个错误,并在密码不正确的地方使用上面的块代码引发错误消息。但它最终有一个例外。

smtplib.SMTPAuthenticationError
smtplib.SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8  https://support.google.com/mail/?p=BadCredentials o23sm485224lfr.302 - gsmtp')

任何关于可以改进的帮助或建议都会有所帮助。谢谢你。

标签: pythonemailflask-mail

解决方案


推荐阅读