smtplib.SMTPServerDisconnected: Connection unexpectedly closed错误

使用sendgrid 邮件服务器报错,
配置如下:
MAIL_SERVER = os.getenv(“MAIL_SERVER”)
MAIL_PORT = 587
MAIL_USE_TLS = True
MAIL_USERNAME = os.getenv(“MAIL_USERNAME”)
MAIL_PASSWORD = os.getenv(“MAIL_PASSWORD”)
MAIL_DEFAULT_SENDER = (“Lin”, MAIL_USERNAME)

.env文件
MAIL_SERVER=smtp.sendgrid.net
MAIL_USERNAME=bot@example.com
MAIL_PASSWORD=SG.klPkIDnATVCiKkFoE71x4g…

错误如下:

127.0.0.1 - - [06/Dec/2019 23:37:33] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [06/Dec/2019 23:37:35] "GET /technique/post/143 HTTP/1.1" 200 -
127.0.0.1 - - [06/Dec/2019 23:37:45] "POST /technique/post/143 HTTP/1.1" 302 -
127.0.0.1 - - [06/Dec/2019 23:37:45] "GET /technique/post/143 HTTP/1.1" 200 -
send: 'ehlo [10.198.75.60]\r\n'
reply: b'250-smtp.sendgrid.net\r\n'
reply: b'250-8BITMIME\r\n'
reply: b'250-PIPELINING\r\n'
reply: b'250-SIZE 31457280\r\n'
reply: b'250-AUTH PLAIN LOGIN\r\n'
reply: b'250 AUTH=PLAIN LOGIN\r\n'
reply: retcode (250); Msg: b'smtp.sendgrid.net\n8BITMIME\nPIPELINING\nSIZE 31457280\nAUTH PLAIN LOGIN\nAUTH=PLAIN LOGIN'
send: 'AUTH PLAIN AGJvdEBleGFtcGxlLmNvbQBTRy5rbFBrSURuQVRWQ2lLa0ZvRTcxeDRnLl95UWxZWjF0ZHpzRzZqQjJYWkh1WXNWaURrOVVyVmVVRVducy01UWxTUEE=\r\n'
reply: b'535 Authentication failed: Bad username / password\r\n'
reply: retcode (535); Msg: b'Authentication failed: Bad username / password'
send: 'AUTH LOGIN Ym90QGV4YW1wbGUuY29t\r\n'
Exception in thread Thread-12:
Traceback (most recent call last):
  File "D:\soft\Python3.7.0\Lib\threading.py", line 917, in _bootstrap_inner
    self.run()
  File "D:\soft\Python3.7.0\Lib\threading.py", line 865, in run
    self._target(*self._args, **self._kwargs)
  File "E:\my_project\PersonalBlog\PersonalBlog\emails.py", line 14, in _send_async_mail
    mail.send(message)
  File "e:\my_project\personalblog\pipenv_venv\personalblog-x-jrgtqq\lib\site-packages\flask_mail.py", line 491, in send
    with self.connect() as connection:
  File "e:\my_project\personalblog\pipenv_venv\personalblog-x-jrgtqq\lib\site-packages\flask_mail.py", line 144, in __enter__
    self.host = self.configure_host()
  File "e:\my_project\personalblog\pipenv_venv\personalblog-x-jrgtqq\lib\site-packages\flask_mail.py", line 165, in configure_host
    host.login(self.mail.username, self.mail.password)
  File "D:\soft\Python3.7.0\Lib\smtplib.py", line 721, in login
    initial_response_ok=initial_response_ok)
  File "D:\soft\Python3.7.0\Lib\smtplib.py", line 631, in auth
    (code, resp) = self.docmd("AUTH", mechanism + " " + response)
  File "D:\soft\Python3.7.0\Lib\smtplib.py", line 421, in docmd
    return self.getreply()
  File "D:\soft\Python3.7.0\Lib\smtplib.py", line 394, in getreply
    raise SMTPServerDisconnected("Connection unexpectedly closed")
smtplib.SMTPServerDisconnected: Connection unexpectedly closed

看报错是密码错了,可以重新生成一个密钥试试看。另外可以把发信函数贴上来看看(纯文本,不要贴图)。

上面的错误采用send_new_comment_email函数时产生,更换API_KEY依旧报错。

def _send_async_mail(app, message):
    with app.app_context():
        mail.send(message)


def send_mail(subject, to, html):
    app = current_app._get_current_object()
    message = Message(subject, recipients=[to], html=html)
    thr = Thread(target=_send_async_mail, args=[app, message])
    thr.start()


def send_new_comment_email(post):
    post_url = url_for("blog.show_post", post_id=post.id, post_father="index", _external=True) + "#comments"
    send_mail(subject="New comment", to=current_app.config["PERSONALBLOG_EMAIL"],
              html="<p>New comment in post <i>%s<i>, click the link below to check:</p>"
                   "<p><a href='%s'>%s</a></p>"
                   "<p><small style='color: #868e96'>Do not reply this email.</small></p>"
                   % (post.title, post_url, post_url))


def send_new_reply_email(comment):
    post_url = url_for("blog.show_post", post_id=comment.post_id, post_father="index", _external=True) + "#comments"
    send_mail(subject="New reply", to=comment.email,
              html="<p>New reply for the comment you left in post <i>%s</i>, click the link below to check:</p>"
                   "<p><a href='%s'>%s</a></p>"
                   "<p><small style='color: #868e96'>Do not reply this emails.</small></p>"
                   % (comment.post.title, post_url, post_url))

已解决此问题,走投无路之下,进sendgrid看了下具体配置项,MAIL_USERNAME仅能为apikey,设置错误,实在抱歉。

1 个赞