Python Django电子邮件不工作-smtplib.SMTPServerDisconnected:连接意外关闭

Python Django电子邮件不工作-smtplib.SMTPServerDisconnected:连接意外关闭,python,django,email,sendgrid,Python,Django,Email,Sendgrid,我使用的是发送电子邮件的标准。以下是我的设置.py中的相关字段: EMAIL_HOST = 'smtp.sendgrid.net' EMAIL_HOST_USER = 'myusername' EMAIL_HOST_PASSWORD = 'mypassword' EMAIL_PORT = 465 EMAIL_USE_SSL = True DEFAULT_FROM_EMAIL = 'admin@mysite.com' 我正在测试通过执行以下命令在shell中发送电子邮件: send_mail('

我使用的是发送电子邮件的标准。以下是我的
设置.py
中的相关字段:

EMAIL_HOST = 'smtp.sendgrid.net'
EMAIL_HOST_USER = 'myusername'
EMAIL_HOST_PASSWORD = 'mypassword'
EMAIL_PORT = 465
EMAIL_USE_SSL = True
DEFAULT_FROM_EMAIL = 'admin@mysite.com'
我正在测试通过执行以下命令在shell中发送电子邮件:

send_mail('test','test','email@email.com',['to@email.com'])
但是,它会返回此错误回溯:

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/Users/zorgan/Desktop/app/lib/python3.5/site-packages/django/core/mail/__init__.py", line 62, in send_mail
    return mail.send()
  File "/Users/zorgan/Desktop/app/lib/python3.5/site-packages/django/core/mail/message.py", line 348, in send
    return self.get_connection(fail_silently).send_messages([self])
  File "/Users/zorgan/Desktop/app/lib/python3.5/site-packages/django/core/mail/backends/smtp.py", line 104, in send_messages
    new_conn_created = self.open()
  File "/Users/zorgan/Desktop/app/lib/python3.5/site-packages/django/core/mail/backends/smtp.py", line 71, in open
    self.connection.login(force_str(self.username), force_str(self.password))
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/smtplib.py", line 720, in login
    initial_response_ok=initial_response_ok)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/smtplib.py", line 630, in auth
    (code, resp) = self.docmd("AUTH", mechanism + " " + response)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/smtplib.py", line 420, in docmd
    return self.getreply()
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/smtplib.py", line 393, in getreply
    raise SMTPServerDisconnected("Connection unexpectedly closed")
smtplib.SMTPServerDisconnected: Connection unexpectedly closed
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
文件“/Users/zorgan/Desktop/app/lib/python3.5/site packages/django/core/mail/__init__.py”,第62行,在发送邮件中
返回邮件。发送()
文件“/Users/zorgan/Desktop/app/lib/python3.5/site packages/django/core/mail/message.py”,第348行,在send中
返回self.get\u连接(以静默方式失败)。发送消息([self])
文件“/Users/zorgan/Desktop/app/lib/python3.5/site packages/django/core/mail/backends/smtp.py”,第104行,在send_messages中
新建连接创建=self.open()
文件“/Users/zorgan/Desktop/app/lib/python3.5/site packages/django/core/mail/backends/smtp.py”,第71行,打开
self.connection.login(force_str(self.username)、force_str(self.password))
文件“/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/smtplib.py”,第720行,登录
初始响应(正常=初始响应(正常)
文件“/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/smtplib.py”,第630行,在auth中
(代码,resp)=self.docmd(“AUTH”,机制+“”+响应)
docmd中的文件“/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/smtplib.py”,第420行
返回self.getreply()
getreply中的第393行文件“/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/smtplib.py”
升起SMTPServerDisconnected(“连接意外关闭”)
smtplib.SMTPServerDisconnected:连接意外关闭
知道我为什么会犯这个错误吗

PS:这是我的本地开发服务器

电子邮件端口=465 电子邮件\u使用\u SSL=True

改变这个

电子邮件端口=587 电子邮件\u使用\u TLS=True

你可以查看这个链接

2021年更新 Sendgrid现在要求使用而不是用户名/密码:

自2020年第4季度起,需要双因素身份验证,所有Twilio SendGrid API终结点将拒绝新的API请求和SMTP 通过Basic使用用户名和密码进行的配置 认证

因此,您需要至少具有
邮件发送>完全访问权限
权限,然后调整Django配置,如下所示:

EMAIL_HOST_USER = 'apikey' # this is exactly the value 'apikey'
EMAIL_HOST_PASSWORD = SENDGRID_API_KEY
:

settings.py

SENDGRID_API_KEY = os.getenv('SENDGRID_API_KEY')

EMAIL_HOST = 'smtp.sendgrid.net'
EMAIL_HOST_USER = 'apikey' # this is exactly the value 'apikey'
EMAIL_HOST_PASSWORD = SENDGRID_API_KEY
EMAIL_PORT = 587
EMAIL_USE_TLS = True
from django.core.mail import send_mail
send_mail('Subject here', 'Here is the message.', 'from@example.com', ['to@example.com'], fail_silently=False)
utils.py

SENDGRID_API_KEY = os.getenv('SENDGRID_API_KEY')

EMAIL_HOST = 'smtp.sendgrid.net'
EMAIL_HOST_USER = 'apikey' # this is exactly the value 'apikey'
EMAIL_HOST_PASSWORD = SENDGRID_API_KEY
EMAIL_PORT = 587
EMAIL_USE_TLS = True
from django.core.mail import send_mail
send_mail('Subject here', 'Here is the message.', 'from@example.com', ['to@example.com'], fail_silently=False)

使用这些配置,它对我来说运行良好:
EMAIL\u PORT=587
EMAIL\u use\u TLS=True
@Zorgan您是否使用
django-sendgrid-v5
包发送电子邮件?@ABDULNIYASPM不,我不是关于sendgrid文档的其他重要细节,我没有注意到,无论访问凭据如何,用户都将始终是apikey(文字):EMAIL\u HOST\u user='apikey'