《Flask Web开发》第六章 电子邮件 error: [Errno 111] Connection refused
当前位置 : 澳门银河官网 > 澳门银河官网

《Flask Web开发》第六章 电子邮件 error: [Errno 111] Connection refused

《Flask Web开发》第六章 电子邮件,代码按照书上敲的,邮箱用的是163邮箱

import os
from flask import Flask, render_template, session, redirect, url_for, flash
from flask_script import Shell, Manager
from flask_bootstrap import Bootstrap
from flask_moment import Moment
from flask_wtf import Form
from wtforms import StringField, SubmitField
from wtforms.validators import Required
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate, MigrateCommand
from flask_mail import Mail

basedir = os.path.abspathos.path.dirname__file__

app = Flask__name__
app.config[SECRET_KEY] = zhao xiufeng
app.config[SQLALCHEMY_DATABASE_URI] = sqlite:/// + os.path.joinbasedir, data.sqlite
app.config[MAIL_SERVER] = stmp.163.com
app.config[MAIL_PORT] = 465
app.config[MAIL_USE_SSL] = True
app.config[MAIL_USERNAME] = AAA@163.com
app.config[MAIL_PASSWORD] = XXX
mail = Mailapp

manager = Managerapp
bootstrap = Bootstrapapp
moment = Momentapp
db = SQLAlchemyapp
migrate = Migrateapp,db

class Roledb.Model:
    __tablename__ = roles
    id = db.Columndb.Integer, primary_key=True
    name = db.Columndb.String64, unique=True

    users = db.relationshipUser, backref=role, lazy=dynamic

    def __repr__self:
        return <Role %r> % self.name

class Userdb.Model:
    __tablename__ = Users
    id = db.Columndb.Integer, primary_key=True
    username = db.Columndb.String64, unique=True, index=True

    role_id = db.Columndb.Integer, db.ForeignKeyroles.id

    def __repr__self:
        return <User %r> % self.username

class NameFormForm:
    name = StringFieldWhat is your name?, validators=[Required]
    submit = SubmitFieldSubmit

def make_shell_context:
    return dictapp=app, db=db, User=User, Role=Role
manager.add_command"shell", Shellmake_context=make_shell_context
manager.add_commanddb, MigrateCommand  

@app.route/, methods=[GET,POST]
def index:
    form = NameForm
    if form.validate_on_submit:
        user = User.query.filter_byusername=form.name.data.first
        if user is None:
            user = Userusername = form.name.data
            session[known] = False
            session[known] = True
        session[name] = form.name.data
        form.name.data = 
        return redirecturl_forindex
    return render_templateindex.html, form = form, 
    name = session.getname, known = session.getknown, False

def username:
    return render_templateuser.html, name=name

def page_not_founde:
    return render_template404.html,404

def internal_server_errore:
    return render_template500.html,500

if __name__ == __main__:

在python shell会话内容:

>>> from flask_mail import Message
>>> from hello import mail
>>> msg = Messagetest subject, sender=AAA@163.com, recipients=[BBB@163.com]
>>> msg.body = text body
>>> msg.html = <b>HTML</b> body
>>> with app.app_context:
...     mail.sendmsg


Traceback most recent call last:
  File "<console>", line 2, in <module>
  File "/usr/local/lib/python2.7/dist-packages/flask_mail.py", line 491, in send
    with self.connect as connection:
  File "/usr/local/lib/python2.7/dist-packages/flask_mail.py", line 144, in __enter__
    self.host = self.configure_host
  File "/usr/local/lib/python2.7/dist-packages/flask_mail.py", line 156, in configure_host
    host = smtplib.SMTP_SSLself.mail.server, self.mail.port
  File "/usr/lib/python2.7/smtplib.py", line 788, in __init__
    SMTP.__init__self, host, port, local_hostname, timeout
  File "/usr/lib/python2.7/smtplib.py", line 256, in __init__
    code, msg = self.connecthost, port
  File "/usr/lib/python2.7/smtplib.py", line 316, in connect
    self.sock = self._get_sockethost, port, self.timeout
  File "/usr/lib/python2.7/smtplib.py", line 793, in _get_socket
    new_socket = socket.create_connectionhost, port, timeout
  File "/usr/lib/python2.7/socket.py", line 571, in create_connection
    raise err
error: [Errno 111] Connection refused


已经发现问题了,app.config[MAIL_SERVER] = stmp.163.com写错了,正确的是app.config[MAIL_SERVER] = smtp.163.com