Python Flask Mail - ConnectionRefusedError: [Errno 61] Connection refused

158 Views Asked by At

I am creating a second python application and I am using flask-mail with gmail. For some reasons, I am getting an error while my 1st application which I did some months back works even after I ran it again now. Here is the code and the error. #Note: The password from gmail is via gmail 3rd party access generate password. I also changed the password and email in this code to avoid giving unauthorised access.

  File "/Users/vei/opt/anaconda3/lib/python3.8/site-packages/flask/app.py", line 2464, in __call__
    return self.wsgi_app(environ, start_response)
  File "/Users/vei/opt/anaconda3/lib/python3.8/site-packages/flask/app.py", line 2450, in wsgi_app
    response = self.handle_exception(e)
  File "/Users/vei/opt/anaconda3/lib/python3.8/site-packages/flask/app.py", line 1867, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/Users/vei/opt/anaconda3/lib/python3.8/site-packages/flask/_compat.py", line 39, in reraise
    raise value
  File "/Users/vei/opt/anaconda3/lib/python3.8/site-packages/flask/app.py", line 2447, in wsgi_app
    response = self.full_dispatch_request()
  File "/Users/vei/opt/anaconda3/lib/python3.8/site-packages/flask/app.py", line 1952, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/Users/vei/opt/anaconda3/lib/python3.8/site-packages/flask/app.py", line 1821, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/Users/vei/opt/anaconda3/lib/python3.8/site-packages/flask/_compat.py", line 39, in reraise
    raise value
  File "/Users/vei/opt/anaconda3/lib/python3.8/site-packages/flask/app.py", line 1950, in full_dispatch_request
    rv = self.dispatch_request()
  File "/Users/vei/opt/anaconda3/lib/python3.8/site-packages/flask/app.py", line 1936, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/Users/vei/Documents/Personal/Training/Programming/Intensive_tutorial/Extension_wtf/app.py", line 33, in contact
    mail.send(msg)
  File "/Users/vei/opt/anaconda3/lib/python3.8/site-packages/flask_mail.py", line 491, in send
    with self.connect() as connection:
  File "/Users/vei/opt/anaconda3/lib/python3.8/site-packages/flask_mail.py", line 144, in __enter__
    self.host = self.configure_host()
  File "/Users/vei/opt/anaconda3/lib/python3.8/site-packages/flask_mail.py", line 158, in configure_host
    host = smtplib.SMTP(self.mail.server, self.mail.port)
  File "/Users/vei/opt/anaconda3/lib/python3.8/smtplib.py", line 255, in __init__
    (code, msg) = self.connect(host, port)
  File "/Users/vei/opt/anaconda3/lib/python3.8/smtplib.py", line 339, in connect
    self.sock = self._get_socket(host, port, self.timeout)
  File "/Users/vei/opt/anaconda3/lib/python3.8/smtplib.py", line 310, in _get_socket
    return socket.create_connection((host, port), timeout,
  File "/Users/vei/opt/anaconda3/lib/python3.8/socket.py", line 808, in create_connection
    raise err
  File "/Users/vei/opt/anaconda3/lib/python3.8/socket.py", line 796, in create_connection
    sock.connect(sa)
ConnectionRefusedError: [Errno 61] Connection refused

I have changed the application config but no progress

from flask import Flask, render_template, request, url_for
from form import CourseForm 
from flask_mail import Mail, Message


app = Flask(__name__)
mail = Mail(app)

app.config["SECRET_KEY"] = "vndbnhjbd1383789a9QA=u292gfdj7v"
app.config['MAIL_SERVER'] = 'smtp.gmail.com'
app.config['MAIL_PORT'] =  465
app.config['MAIL_USERNAME'] = '[email protected]'
app.config['MAIL_PASSWORD'] = '7rwbcs7asbqq'
app.config['MAIL_USE_TLS'] = False
app.config['MAIL_USE_SSL'] = True


@app.route('/', methods = ['POST', 'GET'])
def index():

    return "This is a test"

@app.route('/contact', methods = ['POST', 'GET'])
def contact():

    form = CourseForm()

    if request.method == 'POST':

        msg = Message("Flask Email Test", recipients=['[email protected]'])
        msg.body = "Testing the solution"

        mail.send(msg)

        return '<p> Sent </p>'

    

    return render_template('index.html', form = form)


0

There are 0 best solutions below