简体   繁体   English

Django send_mail返回SMTPConnectError

[英]Django send_mail returns SMTPConnectError

I am trying to send an email using Django send_email, smtp and gmail. 我正在尝试使用Django send_email,smtp和gmail发送电子邮件。 However, the code returns SMTPConnectError (-1, b") 但是,代码返回SMTPConnectError(-1,b“)

My settings.py file: 我的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

My views.py file: 我的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)

The traceback: 追溯:

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")

When I change the EMAIL_BACKEND to: 当我将EMAIL_BACKEND更改为:

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

it works fine. 它工作正常。 Also, it doesn't show the error when I use dummy backend. 此外,当我使用虚拟后端时,它不会显示错误。

I have tried using SSL instead of TLS, allowed less secure apps in gmail, turned off firewall and also tried other stackoverflow posts related to sending email via django but nothing worked. 我尝试过使用SSL代替TLS,允许gmail中安全性较低的应用程序,关闭防火墙,还尝试了其他与通过django发送电子邮件相关的stackoverflow帖子,但没有任何效果。 I don't know what the error is. 我不知道错误是什么。 Please help me. 请帮我。 Thanks in advance. 提前致谢。

Gmail account need this: 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. then test its works by: 然后测试其作品:

python manage.py shell python manage.py shell

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

If you're not using a gmail account and still getting problems then just try add the EMAIL_HOST_USER and EMAIL_HOST_PASSWORD to what you have. 如果您没有使用Gmail帐户但仍然遇到问题,那么只需尝试将EMAIL_HOST_USER和EMAIL_HOST_PASSWORD添加到您拥有的内容中即可。 If you still have issues maybe your network is blocking you. 如果您仍有问题,可能是您的网络阻止了您。 Firewalls on your OS or router. 操作系统或路由器上的防火墙。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM