0

when i execute this code it shows ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it

import os
import smtplib
from flask import *  
from flask_mail import *  

app = Flask(__name__)  
mail = Mail(app)
app.config['MAIL_SERVER']='smtp.gmail.com'  
app.config['MAIL_PORT']=465
app.config['MAIL_USERNAME'] = os.environ.get('User'  )
app.config['MAIL_PASSWORD'] = os.environ.get('Password' ) 
app.config['MAIL_USE_TLS'] = True  
app.config['MAIL_USE_SSL'] = True  

@app.route('/')  
def index():    
    msg = Message('HEllo',sender=["joyciimmaculate@gmail.com"], recipients = ["joyciimmaculate@gmail.com"])  
    msg.body = "hi bulk sms"  
    msg.html ="<img src="/static/deepa.jpg" style='width:500px;height:228px;'>"
    mail.send(msg)  
    return "mail sent"```


  • Did you try to put `mail = Mail(app)` after the last line of configuration `app.config['MAIL_USE_SSL'] = False`? – CCebrian Feb 19 '20 at 17:08

1 Answers1

0

Based on the description of the error, this is not a problem with your Python code, but rather that the network connection to smtp.gmail.com is being blocked/rejected (possibly by a firewall or some other network issue).

Note that if you are running this on a home machine, most ISPs will actively block outbound SMTP connections to anything except the ISP's own SMTP servers (some hosting providers also do the same), to prevent spammers from exploiting the service. You may need to change your settings to use your ISP's outbound mail host instead of connecting directly to smtp.gmail.com.

Foogod
  • 310
  • 2
  • 6
  • thank you....but how should i do that bcos i'm totally new to python –  Feb 24 '20 at 02:56
  • I don't think this issue has anything to do with Python at all. This is a problem with your network and/or ISP. I suspect you may be able to fix it by changing what MAIL_SERVER you're trying to connect to, but you'll need to find out from your ISP or hosting provider what mail server you're allowed to connect to. – Foogod Feb 24 '20 at 17:15