flask_mail使用qq郵箱服務器發(fā)郵件時有可能會遇到如下報錯
原因是我們使用了Mail中的默認設置
smtplib.SMTPAuthenticationError: (535, b'Login Fail. Please enter your authorization code to login. More information in http:/
/service.mail.qq.com/cgi-bin/help?subtype=1&&id=28&&no=1001256')
我們可以看flask_mail中Mail源碼:
state = _Mail(
app.config.get('MAIL_SERVER', '127.0.0.1'),
app.config.get('MAIL_USERNAME'),
app.config.get('MAIL_PASSWORD'),
app.config.get('MAIL_PORT', 25),
app.config.get('MAIL_USE_TLS', False),
app.config.get('MAIL_USE_SSL', False),
app.config.get('MAIL_DEFAULT_SENDER'),
int(app.config.get('MAIL_DEBUG', app.debug)),
app.config.get('MAIL_MAX_EMAILS'),
app.config.get('MAIL_SUPPRESS_SEND', app.testing))
Mail中的默認端口:25
然而qq郵箱已經(jīng)取消了25端口的非ssl驗證,要使用465的ssl加密驗證
因此有兩種解決方法:
方法一:
將配置中設置使用465端口,并開啟SSL
配置代碼如下:
app = Flask(__name__)
app.config['MAIL_DEBUG'] = True # 開啟debug,便于調試看信息
app.config['MAIL_SUPPRESS_SEND'] = False # 發(fā)送郵件,為True則不發(fā)送
app.config['MAIL_SERVER'] = 'smtp.qq.com' # 郵箱服務器
app.config['MAIL_PORT'] = 465 # 端口
app.config['MAIL_USE_SSL'] = True # 重要,qq郵箱需要使用SSL
app.config['MAIL_USE_TLS'] = False # 不需要使用TLS
app.config['MAIL_USERNAME'] = '*******@qq.com' # 填郵箱
app.config['MAIL_PASSWORD'] = '**************' # 填授權碼
app.config['MAIL_DEFAULT_SENDER'] = '*******@qq.com' # 填郵箱,默認發(fā)送者
manager = Manager(app)
mail = Mail(app)
方法一完美解決。
方法二:
可以進入qq郵箱進行設置
進入郵箱——>設置——>賬戶

image.png
另外,方法一的授權碼,也是在這張圖片的標記處生成。