Flask_mail连接被拒绝

时间:2019-12-31 02:49:02

标签: python flask flask-mail

我正在尝试发送电子邮件,但显示如下错误。谁能告诉我怎么了?以上是原始代码。我已经尝试过TLS 465SMTP 587。但结果相同,连接被拒绝。

这里是代码:

    from threading import Thread
    from flask import Flask
    from flask_mail import Mail, Message
    from config import config
    from instance.config import *

    app = Flask(__name__)


    def send_async_email(msg):
        mail = Mail(app) 
        with app.app_context():
            mail.send(msg)
    def send_email(to, subject, template, **kwargs):
        app.config.from_object(config['default'])
        app.config.from_pyfile('config.py')

        msg = Message(MAIL_SUBJECT_PREFIX + ' ' + subject,
                      sender=MAIL_SENDER, recipients=[to],html='templates/email/inform.html')
        thrmail = Thread(target=send_async_email, args=[msg])
        thrmail.start()
        return thrmail

这是错误:

Exception in thread Thread-1:                                             
    Traceback (most recent call last):                                        
      File "/usr/lib/python3.6/threading.py", line 916, in _bootstrap_inner   
        self.run()                                                            
      File "/usr/lib/python3.6/threading.py", line 864, in run                
        self._target(*self._args, **self._kwargs)                             
      File "/data/evsim/pub_api/e_mail.py", line 13, in send_async_email      
        mail.send(msg)                                                        
      File "/usr/local/lib/python3.6/dist-packages/flask_mail.py", line 491, in send                                                                    
        with self.connect() as connection:                                    
      File "/usr/local/lib/python3.6/dist-packages/flask_mail.py", line 144, in __enter__                                                               
        self.host = self.configure_host()                                     
      File "/usr/local/lib/python3.6/dist-packages/flask_mail.py", line 158, in configure_host                                                          
        host = smtplib.SMTP(self.mail.server, self.mail.port)                 
      File "/usr/lib/python3.6/smtplib.py", line 251, in __init__             
        (code, msg) = self.connect(host, port)                                
      File "/usr/lib/python3.6/smtplib.py", line 336, in connect              
        self.sock = self._get_socket(host, port, self.timeout)                
      File "/usr/lib/python3.6/smtplib.py", line 307, in _get_socket          
        self.source_address)                                                  
      File "/usr/lib/python3.6/socket.py", line 724, in create_connection     
        raise err                                                             
      File "/usr/lib/python3.6/socket.py", line 713, in create_connection     
        sock.connect(sa)                                                      
    ConnectionRefusedError: [Errno 111] Connection refused                    

2 个答案:

答案 0 :(得分:0)

首先,使用flask-mail发送邮件,您应该拥有这些内容

1.smtp服务器-如果您有像postfix这样的本地smtp服务器,没有问题,或者您必须测试邮件服务器,请转到此链接并创建免费帐户以获取免费的smtp服务器ip和配置详细信息.. https://mailtrap.io

  1. 您必须在您的防火墙中打开smtp端口。

在配置了那些尝试使用正确的配置详细信息运行您的程序之后

答案 1 :(得分:0)

以下配置对我有用。请尝试使用此配置:

MAIL_SERVER='smtp.gmail.com'
MAIL_PORT=465
MAIL_USE_TLS = False
MAIL_USE_SSL= True
MAIL_USERNAME = 'yourmail@gmail.com'
MAIL_PASSWORD = 'yourpassword'

也不要忘记允许“安全性较低的应用”访问您的帐户。请参阅此link