flask-mail gmail: connection refused flask-mail gmail: connection refused flask flask

flask-mail gmail: connection refused


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)


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


from flask import Flaskfrom flask_mail import Mail, Messageapp = Flask(__name__)app.config['MAIL_SERVER'] = 'smtp.gmail.com'app.config['MAIL_PORT'] = 587app.config['MAIL_USE_TLS'] = Trueapp.config['MAIL_USERNAME'] = 'youremail@gmail.com'app.config['MAIL_PASSWORD'] = 'your_email_password'mail = Mail(app)@app.route('/')def home():    msg = Message('mail title', sender='sender of the email', recipients=['recipient2gmail.com'])    msg.body = 'Body of the email to send'    return 'Mail Sent...'if __name__ == '__main__':    app.run()

Running the with the right configurations should enable send an email.Notice the mail is initialized after the configurations have been sent.

In case of any other errors, make sure your google account allows access from less secure applications.