python smtplib.SMTPServerDisconnected:连接意外关闭

euoag5mw  于 2023-02-15  发布在  Python
关注(0)|答案(2)|浏览(639)

我尝试到执行下面的代码在终端上与django.core.mail发送_mail

send_mail('some title','some text','myemail@gmail.com',['otheremail@gmail.com'])

但执行后控制台显示此错误

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/home/adjtlikp/virtualenv/globalit-web/3.7/lib/python3.7/site-packages/django/core/mail/__init__.py", line 61, in send_mail
    return mail.send()
  File "/home/adjtlikp/virtualenv/globalit-web/3.7/lib/python3.7/site-packages/django/core/mail/message.py", line 284, in send
    return self.get_connection(fail_silently).send_messages([self])
  File "/home/adjtlikp/virtualenv/globalit-web/3.7/lib/python3.7/site-packages/django/core/mail/backends/smtp.py", line 102, in send_messages
    new_conn_created = self.open()
  File "/home/adjtlikp/virtualenv/globalit-web/3.7/lib/python3.7/site-packages/django/core/mail/backends/smtp.py", line 62, in open
    self.connection = self.connection_class(self.host, self.port, **connection_params)
  File "/opt/alt/python37/lib64/python3.7/smtplib.py", line 251, in __init__
    (code, msg) = self.connect(host, port)
  File "/opt/alt/python37/lib64/python3.7/smtplib.py", line 338, in connect
    (code, msg) = self.getreply()
  File "/opt/alt/python37/lib64/python3.7/smtplib.py", line 394, in getreply
    raise SMTPServerDisconnected("Connection unexpectedly closed")
smtplib.SMTPServerDisconnected: Connection unexpectedly closed

我的settings.py

EMAIL_USE_TLS = True  
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'   
EMAIL_HOST = 'smtp.gmail.com'  
EMAIL_HOST_PASSWORD = 'mypass'  
EMAIL_HOST_USER = 'myemail@gmail.com'   
EMAIL_PORT = 465 
DEFAULT_FROM_EMAIL = EMAIL_HOST_USER

我尝试更改EMAIL_PORT和EMAIL_HOST,但没有任何变化,有趣的是,在localhost中,代码工作完美,但当我使用主机时,错误出现在我面前

zysjyyx4

zysjyyx41#

我也有同样的情况。我的设置是:

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.sendgrid.net'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_HOST_USER = 'apikey'
EMAIL_HOST_PASSWORD = config('PASS')

我在SendGrid上还有一封已验证的电子邮件。我收到此错误的原因是我没有使用在SendGrid上验证的电子邮件。如果您使用其他电子邮件地址从您的网站发送电子邮件,也可能收到以下错误:
(550,b '发件人地址与已验证的发件人标识不匹配。在解决此错误之前,无法发送邮件。请访问https://sendgrid.com/docs/for-developers/sending-email/sender-identity/以查看发件人标识要求')
因此,简而言之,只使用一个现有的电子邮件,你已经验证了发送网格,然后如果代码的工作,检查收件箱/垃圾邮件的电子邮件,你使用。

9lowa7mx

9lowa7mx2#

错误显示的原因是,我输入了错误的端口EMAIL_USE_TLS在TLS的情况下,端口应该是25或587,在SSL的情况下,端口应该是465。

相关问题