2013-04-08 150 views
6

我在小型web應用程序中使用Flask-Mail。由於應用程序很小,我正在使用gmail發送電子郵件。按照文檔中的所有步驟操作後,運行應用程序以測試電子郵件功能。我繼續得到error: [Errno 111] Connection refused燒瓶郵件 - 連接拒絕[Errno 111]

這是我在我的config.py設置電子郵件:

MAIL_SERVER = 'smtp.gmail.com' 
MAIL_PORT = 587 
MAIL_USE_TLS = True 
MAIL_USERNAME = '[email protected]' 
MAIL_PASSWORD = 'some_password' 
DEFAULT_MAIL_SENDER = '[email protected]' 

這是我使用測試瓶-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=['[email protected]']) 

      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' 

我不太確定瞭解導致連接被拒絕的原因。如果有人能夠在正確的方向上推動我如何進一步測試或指出我犯了什麼錯誤,我將不勝感激。

回答

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 = '[email protected]' 
MAIL_PASSWORD = 'some_password' 
DEFAULT_MAIL_SENDER = '[email protected]' 
相關問題