配置Flask-Mail使用GMail [英] Configure Flask-Mail to use GMail

查看:244
本文介绍了配置Flask-Mail使用GMail的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试使用Flask-Mail将邮件发送到Gmail的SMTP服务器时,使用下面的设置,我得到 [Errno -2]名称或服务未知。如何修改我的配置以使用Gmail发送邮件?

When I try to send an email using Flask-Mail to Gmail's SMTP server using the settings below, I get [Errno -2] Name or service not known. How do I fix my configuration to send email with Gmail?

from flask import Flask, render_template, redirect, url_for
from flask_mail import Mail,  Message

app = Flask(__name__)
app.config.update(
    MAIL_SERVER='smtp@gmail.com',
    MAIL_PORT=587,
    MAIL_USE_SSL=True,
    MAIL_USERNAME = 'ri******a@gmail.com',
    MAIL_PASSWORD = 'Ma*****fe'
)

mail = Mail(app)

@app.route('/send-mail/')
def send_mail():
    msg = mail.send_message(
        'Send Mail tutorial!',
        sender='rishiahya@gmail.com',
        recipients=['rishiahya2007@msn.com'],
        body="Congratulations you've succeeded!"
    )
    return 'Mail sent'


推荐答案


  1. 服务器是smtp.gmail.com。
  2. 端口必须与安全类型用过的。如果对 MAIL_USE_TLS = True 使用STARTTLS,则使用 MAIL_PORT = 587如果直接在 MAIL_USE_SSL = True 中使用SSL / TLS,则使用。 > MAIL_PORT = 465

  3. 启用STARTTLS或SSL / TLS,
  4. 根据您的Google帐户的安全设置,您可能需要生成并使用应用程序密码而不是帐户密码。

  1. The server is "smtp.gmail.com".
  2. The port must match the type of security used.
    • If using STARTTLS with MAIL_USE_TLS = True, then use MAIL_PORT = 587.
    • If using SSL/TLS directly with MAIL_USE_SSL = True, then use MAIL_PORT = 465.
    • Enable either STARTTLS or SSL/TLS, not both.





MAIL_SERVER = 'smtp.gmail.com'
MAIL_PORT = 465
MAIL_USE_SSL = True
MAIL_USERNAME = 'username@gmail.com'
MAIL_PASSWORD = 'app password generated in step 3'

这篇关于配置Flask-Mail使用GMail的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆