【问题标题】:flask-mail : ConnectionRefusedError when trying to send an email from flask server烧瓶邮件:尝试从烧瓶服务器发送电子邮件时出现 ConnectionRefusedError
【发布时间】:2019-05-31 14:24:40
【问题描述】:

我正在做一个 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)

谢谢:)

【问题讨论】:

    标签: python flask mailing flask-mail


    【解决方案1】:

    您应该在初始化 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 访问不太安全的应用程序。

    【讨论】:

    • 谢谢你的回答,似乎有改变,因为错误现在不一样了TypeError: getaddrinfo() argument 1 must be string or None我正在尝试解决这个问题
    • 没关系我发现我的类型错误,这是关于我的烧瓶邮件配置行之后的逗号
    • 任何时候,很高兴它有帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-27
    • 1970-01-01
    • 1970-01-01
    • 2019-04-01
    相关资源
    最近更新 更多