How do I resolve builtins.ConnectionRefusedError error in attempting to send email using flask-mail
Question: I am making a simple WebApp using Flask framework in python. It will take user
inputs for email and name from my website
([www.anshulbansal.esy.es](http://www.anshulbansal.esy.es)) and will check if
email exists in database (here database is supposed as dictionary for now)
then it will not work further, but if it doesn't exists in database, it will
send a random link to the submitted email and if the user clicks the link,
then its info will be added to my database.

It's almost done but this error in coming in my way. Check out this code:

    
    
    from flask import Flask, render_template, request, redirect, url_for
    from flask_mail import Mail, Message
    import random
    import string
    
    
    def random_generator(size=6, chars=string.ascii_letters + string.digits):
        return ''.join(random.choice(chars) for x in range(size))
    
    
    subscribers_d = {'anshul.bansal5@yahoo.com': 'Anshul Bansal', 'anshul.bansal3@yahoo.com': 'Bansal', 'anshul.bansal@yahoo.com': 'Anshul',}
    
    app = Flask(__name__)
    mail = Mail(app)
    
    app.config.update(
        MAIL_SERVER='smtp.gmail.com',
        MAIL_PORT=465,
        MAIL_USE_TLS = False,
        MAIL_USE_SSL=True,
        MAIL_USERNAME='anshul.bansal950@gmail.com',
        MAIL_PASSWORD="It's Secret"
    )
    
    
    @app.route('/')
    def index():
        return render_template("index.html")
    
    
    @app.route('/submit', methods=['POST'])
    def submit():
        if request.method == "POST":
            v_name = request.form['vname']
            v_email = request.form['vemail']
            return send_mail(v_name, v_email)
        else:
            return redirect(url_for("/"))
    
    random_link_sent = random_generator(20)
    
    
    @app.route("/")
    def send_mail(v_name, v_email):
        if v_email in subscribers_d:
            return "Oh! It seems that you have already registered."
        else:
            msg = Message('Confirm Subscription', sender=['anshul.bansal950@gmail.com'], recipients=[v_email])
            msg.html = "<h3>Confirm Subscription</h3>" \
               "<p>Hi! </p>" + v_name + "<p> , Please click on below link to subscribe</p>" \
                "Link: " + ' www.anshulbansal.esy.es/' + random_link_sent
            mail.send(msg)
            return 'Check Your Inbox For Confirmation Email'
    
    
    @app.route("/<random_link_sent>")
    def confirm(random_link_sent):
        return "You have registered on " + random_link_sent
        subscribers_d[v_email] = v_name
    
    
    if __name__ == "__main__":
        app.run(debug=True)
    

But this code is giving me a builtins.ConnectionRefusedError Error. But before
2-3 attempts of sending email were successful without any error. How do I
resolve it?

[Here is the screenshot of error](http://i.stack.imgur.com/cJNKS.png)


Answer: You should update configuration before you initialize Mail:

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

