python smtp starttls()给我一个SMTPServerDisconnect错误,当试图通过django / python发送邮件时 [英] python smtp starttls() is giving me a SMTPServerDisconnect Error, when trying to send emails via django/python

查看:1663
本文介绍了python smtp starttls()给我一个SMTPServerDisconnect错误,当试图通过django / python发送邮件时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

跟随是我的python manage.py shell代码和出现的错误。我正在测试这个,因为django send_mail给了我同样的错误,所以我试图在shell中重现它

 >> >导入smtplib 
>>> server = smtplib.SMTP('smtp.gmail.com',587)
>>> server.set_debuglevel(1)
>>> server.ehlo()
发送:'ehlo PKL-FKHAN-LT.mgc.mentorg.com \r\''
回复:'250-smtp.gmail.com在您的服务中,[ 58.27.158.222] \r\\\
'
回覆:'250-SIZE 35882577 \r\''
回覆:'250-8BITMIME\r\''
回复:'250-STARTTLS'\\r\\\
'
回复:'250-ENHANCEDSTATUSCODES \r\''
回复:'250-PIPELINING \r\'
回覆:'250-CHUNKING \r\\\
'
回覆:'250 SMTPUTF8\r\'
回覆:retcode(250);消息:smtp.gmail.com在您的服务中,[58.27.158.222]
尺寸35882577
8BITMIME
STARTTLS
ENHANCEDSTATUSCODES
流水线
流动
SMTPUTF8
(250,'smtp.gmail.com在您的服务中,[58.27.158.222] \ nSIZE 35882577 \ n8BITMIME \
nSTARTTLS \ nnnnnCEDSTATUSCODES\\\
PIPELINING\\\
CHUNKING\\\
SMTPUTF8 ')
>>> server.starttls()
send:'STARTTLS \r\'
Traceback(最近一次调用的最后一个):
文件< console>,第1行,位于<模块>
文件C:\Python27\lib\smtplib.py,行645,在starttls
(resp,reply)= self.docmd(STARTTLS)
文件 C:\Python27\lib\smtplib.py,第394行,在docmd
中返回self.getreply()
文件C:\Python27\lib\smtplib.py ,第365行,在getreply
+ str(e))
SMTPServerDisconnected:连接意外关闭:[Errno 10054]一个存在的
g连接被远程主机强制关闭

我经历了所有的问题,并且在这一步之后出现了大部分问题,所以我必须在这里错过一些非常基本的东西。



编辑1以下是settings.py中的电子邮件设置

  EMAIL_BACKEND =django.core.mail.backends.smtp.EmailBackend
EMAIL_USE_TLS = True
EMAIL_PORT = 587
EMAIL_HOST ='smtp.gmail.com'
EMAIL_HOST_USER ='删除'#这是我的gmail帐户电子邮件
EMAIL_HOST_PASSWORD =已删除#这是gmail帐户密码
SERVER_EMAIL =与EMAIL_HOST_USER相同
DEFAULT_FROM_EMAIL = EMAIL_HOST_USER

编辑2:
我刚刚在使用python 3的另一台计算机上测试过它。这意味着我的工作流程中存在一些基本的错误。

解决方案

所以为了完成目的,我回答了我自己的问题。

这是一个ISP问题,不知道为什么gmail不能使用某个ISP,读取与gmail重新检查发件人IP有关的地方。



无论如何,如果有人在连接之前看到某种形式的错误,那么改变你的ISP的一个好主意......


Follow is my code from the python manage.py shell and the error that appears. I am testing this because django send_mail was giving me the same error so I tried to reproduce it in the shell

>>> import smtplib
>>> server = smtplib.SMTP('smtp.gmail.com', 587)
>>> server.set_debuglevel(1)
>>> server.ehlo()
send: 'ehlo PKL-FKHAN-LT.mgc.mentorg.com\r\n'
reply: '250-smtp.gmail.com at your service, [58.27.158.222]\r\n'
reply: '250-SIZE 35882577\r\n'
reply: '250-8BITMIME\r\n'
reply: '250-STARTTLS\r\n'
reply: '250-ENHANCEDSTATUSCODES\r\n'
reply: '250-PIPELINING\r\n'
reply: '250-CHUNKING\r\n'
reply: '250 SMTPUTF8\r\n'
reply: retcode (250); Msg: smtp.gmail.com at your service, [58.27.158.222]
SIZE 35882577
8BITMIME
STARTTLS
ENHANCEDSTATUSCODES
PIPELINING
CHUNKING
SMTPUTF8
(250, 'smtp.gmail.com at your service, [58.27.158.222]\nSIZE 35882577\n8BITMIME\
nSTARTTLS\nENHANCEDSTATUSCODES\nPIPELINING\nCHUNKING\nSMTPUTF8')
>>> server.starttls()
send: 'STARTTLS\r\n'
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "C:\Python27\lib\smtplib.py", line 645, in starttls
    (resp, reply) = self.docmd("STARTTLS")
  File "C:\Python27\lib\smtplib.py", line 394, in docmd
    return self.getreply()
  File "C:\Python27\lib\smtplib.py", line 365, in getreply
    + str(e))
SMTPServerDisconnected: Connection unexpectedly closed: [Errno 10054] An existin
g connection was forcibly closed by the remote host

I went through all the questions and most problems arise after this step, so I must be missing something very basic here.

EDIT 1 Following are the email setting in the settings.py

EMAIL_BACKEND = "django.core.mail.backends.smtp.EmailBackend"
EMAIL_USE_TLS = True
EMAIL_PORT = 587
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'removed' #this is my gmails account email
EMAIL_HOST_PASSWORD = "removed" #this is the gmails account password
SERVER_EMAIL = "same as the EMAIL_HOST_USER"
DEFAULT_FROM_EMAIL=EMAIL_HOST_USER

EDIT 2: I just tested it on a another computer using python 3. the same error. This means that there is some basic fault in my workflow.

解决方案

So for the purpose of completion, I am answering my own question.

It was an ISP issue, not sure why gmail wont work with some ISP, read somewhere that is has to do with gmail backchecking IP of the sender.

Anyhow if anyone is seeing some form of error before even connecting then its a good idea to change your ISP...

这篇关于python smtp starttls()给我一个SMTPServerDisconnect错误,当试图通过django / python发送邮件时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆