6

我在小型 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'

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

@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'

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

4

1 回答 1

1

I am using gmail SMTP for Flask-Mail but with the following settings. I use SSL with port 465. May be this will work for you ?

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'
于 2013-04-08T16:41:56.617 回答