flask-mail:尝试从烧瓶服务器发送电子邮件时出现ConnectionRefusedError

时间:2019-05-31 14:24:40

标签: python flask mailing flask-mail

我正在做一个Web应用程序,唯一的用户是管理员,他们可能每周一次从服务器接收邮件。目前,我只是想从click命令发送邮件以测试模块,但我总是收到'ConnectionRefusedError [WinError 10061] ...

我查看了问题几乎相同的不同帖子,但没有一个解决了我的问题。

这是我的烧瓶配置

from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_mail import Mail ,Message
from flask_login import LoginManager
from apscheduler.schedulers.background import BackgroundScheduler
import os.path

def mkpath(p):
    return os.path.normpath(
        os.path.join(
            os.path.dirname(__file__),
            p
        )
    )

app = Flask(__name__)
app.debug = True
app.config['SECRET_KEY'] = "847b5600-f639-4d07-803e-de716f4e89b7"
app.config['SQLALCHEMY_DATABASE_URI'] = (
    'sqlite:///'+mkpath('./app.db')
)
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False

UPLOAD_FOLDER_CSV = "static/tmp/"
ALLOWED_EXTENSION = set(['csv', 'xls'])
UPLOAD_FOLDER_FLUX = "static/flux/"

MAIL_SERVER = 'smtp.gmail.com',
MAIL_PORT = 465,
MAIL_USE_TLS = False,
MAIL_USE_SSL = True,
MAIL_USERNAME = 'mymail@gmail.com',
MAIL_PASSWORD = 'mygmailpassword',

db = SQLAlchemy(app)
login_manager = LoginManager(app)
scheduler = BackgroundScheduler()
mail = Mail(app)

if __name__ == '__main__':
    app.run(debug=True)

这是我要对其进行测试的命令:

def sendmail():
    msg = Message(
                  recipients=["anothermail@gmail.com"])

    msg.body = "testing"
    mail.connect()
    mail.send(msg)

谢谢:)

1 个答案:

答案 0 :(得分:0)

您应该在初始化Mail之前更新配置:

app.config['MAIL_SERVER']='smtp.gmail.com'
app.config['MAIL_PORT'] = 465
app.config['MAIL_USERNAME'] = 'mymail@gmail.com'
app.config['MAIL_PASSWORD'] = 'mygmailpassword'
app.config['MAIL_USE_TLS'] = False
app.config['MAIL_USE_SSL'] = True

mail = Mail(app)

然后,您应该允许安全性较低的应用通过this link进行访问。