SMTPServerDisconnected:连接意外关闭

时间:2018-10-19 18:06:45

标签: python flask flask-mail

这里是我用来发送重置密码的邮件的主烧瓶邮件代码,以防万一用户忘记了密码,但是当我运行并对其进行编码时,我看到了 smtplib.SMTPServerDisconnected:连接意外关闭

下面是 __ init __。py 文件代码

import os
from flask_mail import Mail
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_bcrypt import Bcrypt
from flask_login import LoginManager

app = Flask(__name__)

app.config['SECRET_KEY'] = '7f09c01a4f942b0812be4cb86e065f77'
app.config['SQLALCHEMY_DATABASE_URI']='sqlite:///site.db'

db=SQLAlchemy(app)
bcrypt=Bcrypt(app)
login_manager = LoginManager(app)
login_manager.login_view='login'
login_manager.login_message_category='info'

app.config['MAIL_SERVER'] = 'smtp.googlemail.com'
app.config['MAIL_PORT'] = 587
app.config['MAIL_USE_TLS'] = True
app.config['MAIL_USERNAME'] = 'sender@gmail.com'
app.config['MAIL_PASSWORD'] = 'password'

mail = Mail(app)

from index import routes

routes.py 文件

import os
import binascii
from PIL import Image
from flask import render_template,url_for, flash, redirect, request, abort
from index import app,db,bcrypt,mail
from index.forms import (RegistrationForm,LoginForm,UpdateAccountForm,PostForm,
                        RequestResetForm,ResetPasswordForm)
from index.models import User, Post
from flask_login import login_user, current_user, logout_user, login_required
from flask_mail import Message

token = user.get_reset_token()
    msg = Message('Password Reset Request',
                  sender="ketansuthar899@gmail.com",
                  recipients=[user.email])
    msg.body = '''To reset your password, visit the following link:{}
If you did not make this request then simply ignore this email and no changes will be made.
'''.format((url_for('reset_token', token=token, _external=True)))

    mail.send(msg)

这是get_reset_token方法:

from datetime import datetime
from index import db, login_manager,app
from itsdangerous import TimedJSONWebSignatureSerializer as Serializer
from flask_login import UserMixin

def get_reset_token(self, expires_sec=1800):
        s = Serializer(app.config['SECRET_KEY'], expires_sec)
        return s.dumps({'user_id': self.id}).decode('utf-8')

我还在StackOverflow上看到了下面的代码,但这也没起作用

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

0 个答案:

没有答案