【问题标题】:Flask-Mail - Connection Refused [Errno 111]Flask-Mail - 连接被拒绝 [Errno 111]
【发布时间】:2013-03-30 15:55:21
【问题描述】:

我在小型 Web 应用程序中使用 Flask-Mail。由于应用程序很小,我使用 gmail 发送电子邮件。完成文档中的所有步骤后,当我运行应用程序来测试电子邮件功能时。我不断收到error: [Errno 111] Connection refused

这是我在config.py 中的电子邮件设置:

MAIL_SERVER = 'smtp.gmail.com'
MAIL_PORT = 587
MAIL_USE_TLS = True
MAIL_USERNAME = 'some_user@gmail.com'
MAIL_PASSWORD = 'some_password'
DEFAULT_MAIL_SENDER = 'some_user@gmail.com'

这是我在views.py 中用来测试 Flask-Mail 的视图:

@app.route('/', methods=['POST', 'GET'])
def index():

    form = ContactForm()

    if request.method == 'POST':
        if form.validate():

            msg = Message('New Msg - Test',
                          recipients=['some_user@gmail.com'])

            msg.body = 'name=%s, email=%s \n Message:%s' % (form.name.data,
                                                            form.email.data,
                                                            form.message.data)

            mail.send(msg)

            flash('Message sent, I will contact you soon! Thanks')

            return redirect(url_for('index'))

    return render_template('index.html', form=form)

我测试了是否存在类似这样的防火墙问题:

In [2]: import socket

In [3]: sock = socket.socket()

In [4]: sock.connect(("smtp.gmail.com", 587))

In [5]: sock.send("EHLO\n")
Out[5]: 5

In [6]: sock.recv(1024)
Out[6]: '220 mx.google.com ESMTP b9sm23940776vee.3 - gsmtp\r\n250-mx.google.com at your service, [108.21.9.10]\r\n250-SIZE 35882577\r\n250-8BITMIME\r\n250-STARTTLS\r\n250 ENHANCEDSTATUSCODES\r\n'

我不太确定是什么导致连接被拒绝。如果有人能在正确的方向上推动我如何进一步测试或指出我犯错的地方,我将不胜感激。

【问题讨论】:

    标签: python python-2.7 flask flask-mail


    【解决方案1】:

    我正在为 Flask-Mail 使用 gmail SMTP,但具有以下设置。我使用端口 465 的 SSL。可能这对你有用吗?

    MAIL_SERVER = 'smtp.gmail.com'
    MAIL_PORT = 465
    MAIL_USE_TLS = False
    MAIL_USE_SSL = True
    MAIL_USERNAME = 'some_user@gmail.com'
    MAIL_PASSWORD = 'some_password'
    DEFAULT_MAIL_SENDER = 'some_user@gmail.com'
    

    【讨论】:

      猜你喜欢
      • 2012-06-07
      • 2019-12-13
      • 2020-11-22
      • 2014-09-14
      • 2021-06-08
      • 2021-08-23
      • 2016-11-12
      • 2011-08-13
      • 1970-01-01
      相关资源
      最近更新 更多