flask-mail gmail: connection refused

匿名 (未验证) 提交于 2019-12-03 01:10:02

问题:

I'm getting the following error when I attempt to use flask-mail to send an email through my gmail account.

error: [Errno 10061] No connection could be made because the target machine actively refused it

I've tried configuring flask-mail in various ways, but so far I always get this error.

Here are some sample configurations I've tried:

  1. app = Flask(__name__) mail = Mail(app)  app.config.update(dict(     DEBUG = True,     MAIL_SERVER = 'smtp.gmail.com',     MAIL_PORT = 465,     MAIL_USE_TLS = False,     MAIL_USE_SSL = True,     MAIL_USERNAME = 'my_username@gmail.com',     MAIL_PASSWORD = 'my_password', )) 
  2. app = Flask(__name__) mail = Mail(app)  app.config.update(dict(     DEBUG = True,     MAIL_SERVER = 'smtp.gmail.com',     MAIL_PORT = 587,     MAIL_USE_TLS = True,     MAIL_USE_SSL = False,     MAIL_USERNAME = 'my_username@gmail.com',     MAIL_PASSWORD = 'my_password', )) 
  3. This configuration is from the flask mega-tutorial (http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-xi-email-support)

    app = Flask(__name__) mail = Mail(app)  app.config.update(dict(     DEBUG = True,     # email server     MAIL_SERVER = 'smtp.googlemail.com',     MAIL_PORT = 465,     MAIL_USE_TLS = False,     MAIL_USE_SSL = True,     MAIL_USERNAME = 'my_username',     MAIL_PASSWORD = 'my_password',      # administrator list     ADMINS = ['my_username@gmail.com'] )) 

Has anyone else experienced a similar problem?

回答1:

As far as I can tell there is nothing wrong with this configuration. The only problem is that your application is not using it. You should update configuration before you initialize Mail:

app = Flask(__name__)  app.config.update(dict(     DEBUG = True,     MAIL_SERVER = 'smtp.gmail.com',     MAIL_PORT = 587,     MAIL_USE_TLS = True,     MAIL_USE_SSL = False,     MAIL_USERNAME = 'my_username@gmail.com',     MAIL_PASSWORD = 'my_password', ))  mail = Mail(app) 


回答2:

In addition to zero323's answer, adding the configuration before creating a Mail object should help, but if it gives an SMTPAuthentication error with a gmail server, then just for testing purpose one may allow less secure apps to login for a while - https://myaccount.google.com/security#signin



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!