gpt4 book ai didi

python - Django send_mail 返回 SMTPConnectError

转载 作者:太空宇宙 更新时间:2023-11-03 12:06:10 25 4
gpt4 key购买 nike

我正在尝试使用 Django send_email、smtp 和 gmail 发送电子邮件。但是,代码返回 SMTPConnectError (-1, b")

我的 settings.py 文件:

ALLOWED_HOSTS = []
DEFAULT_FROM_EMAIL = 'mygmailid'
SERVER_EMAIL = 'mygmailid'
EMAIL_USE_TLS = True
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'mygmailid'
EMAIL_HOST_PASSWORD = 'myPassword'
EMAIL_PORT = 587

我的 views.py 文件:

from django.http import HttpResponse
from django.core.mail import send_mail
from smtplib import SMTPConnectError
#rest of the code in between
....
try:
send_mail('testmail', 'test content', settings.EMAIL_HOST_USER, ['arkaghosh024@gmail.com', fail_silently=False])
except SMTPConnectError as e:
return HttpResponse(e)

回溯:

Traceback (most recent call last):
File "<console>", line 1, in <module>
File "C:\Python34\lib\site-packages\django-1.8.2- py3.4.egg\django\core\mail\__
init__.py", line 62, in send_mail
return mail.send()
File "C:\Python34\lib\site-packages\django-1.8.2- py3.4.egg\django\core\mail\me
ssage.py", line 303, in send
return self.get_connection(fail_silently).send_messages([self])
File "C:\Python34\lib\site-packages\django-1.8.2-py3.4.egg\django\core\mail\ba
ckends\smtp.py", line 100, in send_messages
new_conn_created = self.open()
File "C:\Python34\lib\site-packages\django-1.8.2-py3.4.egg\django\core\mail\ba
ckends\smtp.py", line 58, in open
self.connection = connection_class(self.host, self.port, **connection_params
)
File "C:\Python34\lib\smtplib.py", line 244, in __init__
raise SMTPConnectError(code, msg)
smtplib.SMTPConnectError: (-1, b")

当我将 EMAIL_BACKEND 更改为:

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

它工作正常。此外,当我使用虚拟后端时,它不会显示错误。

我试过使用 SSL 而不是 TLS,允许 gmail 中安全性较低的应用程序,关闭防火墙,还尝试过其他与通过 django 发送电子邮件相关的 stackoverflow 帖子,但没有任何效果。我不知道错误是什么。请帮我。提前致谢。

最佳答案

Gmail 帐户需要:

EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'your-username@gmail.com'
EMAIL_HOST_PASSWORD = 'your-password'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
  1. 然后通过以下方式测试其作品:

python manage.py 外壳

from django.core.mail import send_mail
send_mail('testEmail', 'hi', 'Youremail@email.com', ['Emailtest@email.com'])

如果您没有使用 gmail 帐户并且仍然遇到问题,那么只需尝试将 EMAIL_HOST_USER 和 EMAIL_HOST_PASSWORD 添加到您拥有的帐户中。如果您仍然有问题,则可能是您的网络阻止了您。操作系统或路由器上的防火墙。

关于python - Django send_mail 返回 SMTPConnectError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31636107/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com