Django电子邮件错误:引发错误socket.error:[Errno 111]连接被拒绝[重复]
Posted
技术标签:
【中文标题】Django电子邮件错误:引发错误socket.error:[Errno 111]连接被拒绝[重复]【英文标题】:Django Email Error: raise err socket.error: [Errno 111] Connection refused [duplicate] 【发布时间】:2016-01-08 07:01:28 【问题描述】:我看到很多地方都在问这个问题,但到目前为止还没有解决方案对我有用,或者我可能无法理解到底该怎么做(新手特征)。如果可以的话,请帮忙。
电子邮件设置/settings.py:
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_USE_TLS = True
EMAIL_HOST_USER = 'xyz@gmail.com'
EMAIL_HOST_PASSWORD = 'pwd'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
我在从 Django Python Shell 发送测试电子邮件时收到错误:
首先我运行了发送邮件命令 - 给了我 Error1: django.core.exceptions.ImproperlyConfigured:
>>> from django.core.mail import send_mail
>>> send_mail('Test', 'Test', 'xyz@gmail.com', ['abc@hotmail.com'], fail_silently=False)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/django_admin1/venv-tile-prod/lib/python2.7/site- packages/Django-1.8.2-py2.7.egg/django/core/mail/__init__.py", line 56, in send_mail
fail_silently=fail_silently)
File "/home/django_admin1/venv-tile-prod/lib/python2.7/site-packages/Django-1.8.2-py2.7.egg/django/core/mail/__init__.py", line 37, in get_connection
klass = import_string(backend or settings.EMAIL_BACKEND)
File "/home/django_admin1/venv-tile-prod/lib/python2.7/site-packages/Django-1.8.2-py2.7.egg/django/conf/__init__.py", line 48, in __getattr__
self._setup(name)
File "/home/django_admin1/venv-tile-prod/lib/python2.7/site-packages/Django-1.8.2-py2.7.egg/django/conf/__init__.py", line 42, in _setup
% (desc, ENVIRONMENT_VARIABLE))
django.core.exceptions.ImproperlyConfigured: Requested setting EMAIL_BACKEND, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
为了克服 Error1- 我运行了以下命令
>>> from django.conf import settings
>>> settings.configure()
其次,我再次运行了发送邮件 - Gave Error2: Emails not going out! socket.error: [Errno 111] 连接被拒绝
>>> from django.core.mail import send_mail
>>> send_mail('Test', 'Test', 'xyz@gmail.com', ['abc@hotmail.com'], fail_silently=False)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/django_admin1/venv-tile-prod/lib/python2.7/site-packages/Django-1.8.2-py2.7.egg/django/core/mail/__init__.py", line 62, in send_mail
return mail.send()
File "/home/django_admin1/venv-tile-prod/lib/python2.7/site-packages/Django-1.8.2-py2.7.egg/django/core/mail/message.py", line 303, in send
return self.get_connection(fail_silently).send_messages([self])
File "/home/django_admin1/venv-tile-prod/lib/python2.7/site-packages/Django-1.8.2-py2.7.egg/django/core/mail/backends/smtp.py", line 100, in send_messages
new_conn_created = self.open()
File "/home/django_admin1/venv-tile-prod/lib/python2.7/site-packages/Django-1.8.2-py2.7.egg/django/core/mail/backends/smtp.py", line 58, in open
self.connection = connection_class(self.host, self.port, **connection_params)
File "/usr/local/lib/python2.7/smtplib.py", line 251, in __init__
(code, msg) = self.connect(host, port)
File "/usr/local/lib/python2.7/smtplib.py", line 311, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "/usr/local/lib/python2.7/smtplib.py", line 286, in _get_socket
return socket.create_connection((host, port), timeout)
File "/usr/local/lib/python2.7/socket.py", line 571, in create_connection
raise err
socket.error: [Errno 111] Connection refused
【问题讨论】:
【参考方案1】:所有这一切都是在我尝试从正常的 python 提示符发送电子邮件时调用的:
(venv)[user@server django_project]$ python
电子邮件从未发送过。实际上它们必须从
运行(venv)[user@server django_project]$ python manage.py shell
>>> from django.core.mail import send_mail
>>> send_mail('Test', 'This is test email', 'xyz@gmail.com', ['recipientemail@hotmail.com'], fail_silently=False)
这为我解决了这个问题。电子邮件完美地发出了。所以我的电子邮件设置没有问题,我发送它的方式有问题。
【讨论】:
以上是关于Django电子邮件错误:引发错误socket.error:[Errno 111]连接被拒绝[重复]的主要内容,如果未能解决你的问题,请参考以下文章
使用 Django 引发默认错误 on_delete PROTECT