Django发送电子邮件:SMTPServerDisconnected:连接意外关闭

时间:2015-03-11 07:05:44

标签: python django email

你好我想发送电子邮件激活使用django registration redux。

这是我的setting.py

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'

ACCOUNT_ACTIVATION_DAYS = 3
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'blahblah@gmail.com'
EMAIL_HOST_PASSWORD = 'blahpassword'
EMAIL_PORT = 465
EMAIL_USE_SSL = True

LOGIN_REDIRECT_URL = '/'

当我尝试

  

python manage.py shell

     

来自django.core.mail导入send_mail

     

send_mail('测试','这是测试',' youremail@gmail.com',[' toemail@gmail.com&# 39;])

我收到这样的错误:

    Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/home/apsijogja/djangogirls/myvenv/local/lib/python2.7/site-packages/django/core/mail/__init__.py", line 62, in send_mail
    return mail.send()
  File "/home/apsijogja/djangogirls/myvenv/local/lib/python2.7/site-packages/django/core/mail/message.py", line 286, in send
    return self.get_connection(fail_silently).send_messages([self])
  File "/home/apsijogja/djangogirls/myvenv/local/lib/python2.7/site-packages/django/core/mail/backends/smtp.py", line 92, in send_messages
    new_conn_created = self.open()
  File "/home/apsijogja/djangogirls/myvenv/local/lib/python2.7/site-packages/django/core/mail/backends/smtp.py", line 50, in open
    self.connection = connection_class(self.host, self.port, **connection_params)
  File "/usr/lib/python2.7/smtplib.py", line 249, in __init__
    (code, msg) = self.connect(host, port)
  File "/usr/lib/python2.7/smtplib.py", line 310, in connect
    (code, msg) = self.getreply()
  File "/usr/lib/python2.7/smtplib.py", line 361, in getreply
    raise SMTPServerDisconnected("Connection unexpectedly closed")
SMTPServerDisconnected: Connection unexpectedly closed
你可以帮我解决这个问题吗?

6 个答案:

答案 0 :(得分:6)

要使用端口465,您需要调用smtplib.SMTP_SSL()。 目前,它调用smtplib.SMTP().. 所以,将您的PORT从 465 更改为 587

如果你想使用PORT 465,

您的EMAIL_BACKEND = 'django_smtp_ssl.SSLEmailBackend'

`EMAIL_PORT=465`  

您需要安装django_smtp_ssl

否则你可以保持,

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'

EMAIL_PORT=465

答案 1 :(得分:2)

请查看此链接:https://code.djangoproject.com/ticket/9575 并尝试通过shell发送,它应该工作

答案 2 :(得分:0)

在我的情况下使用int colorIndex = planeIndex % PLANE_COLORS_RGBA.length; colorRgbaToFloat(mPlaneColor, PLANE_COLORS_RGBA[colorIndex]); GLES20.glUniform4fv(mLineColorUniform, 1, mPlaneColor, 0); GLES20.glUniform4fv(mDotColorUniform, 1, mPlaneColor, 0); 解决了这个问题。你可以试试这个。

答案 3 :(得分:0)

对于python3.0 +

我找到了一种解决方法,或者也许这是解决方案,django电子邮件包中存在一些问题。

1)pip安装sendgrid-django

2)将它们与其他电子邮件配置一起添加到settings.py中

  EMAIL_BACKEND = 'sgbackend.SendGridBackend'
  SENDGRID_API_KEY = "YOUR SENDGRID API KEY"
  EMAIL_PORT = 465

3)发送邮件的代码示例(您可能有所不同):

from django.core.mail import EmailMessage
send_email = EmailMessage(
    subject=subject,
    body=body,
    from_email=from_email,
    to=to_email,
    reply_to=reply_to,
    headers=headers,

)
send_email.send(fail_silently=fail_silently)

您肯定必须对代码进行更改,就像我刚才展示的那样。

仅用于调试目的-sendgrid mail send using telnet

答案 4 :(得分:0)

像我自己一样,如果您部署的django应用程序位于heroku上并使用https表示您需要端口465,则不要忘记在此处将以下相同的值添加到heroku Config Vars中。它引发了“连接意外关闭”错误,直到我不得不添加它为止。

# settings.py

EMAIL_HOST = 'smtp.sendgrid.net'
EMAIL_HOST_USER = 'apikey'
EMAIL_HOST_PASSWORD = os.environ.get('EMAIL_HOST_PASSWORD')                   
                     # replace here with the long SendGrid API secret code 
                     # xyxyxyxyzzzyz on heroku's Config Vars
EMAIL_PORT =  465 
EMAIL_USE_SSL = True

否则,将无法在localhost或开发服务器上进行调试(使用http下的587端口),但不能在使用https的465端口下进行部署

答案 5 :(得分:0)

我正在使用AWS SES并遇到相同的问题。

使用端口 587 为我工作。设置与@ User0511完全相同