小编典典

Django发送电子邮件

django

但是我已经尝试了一天多了,以获取与Django一起使用的电子邮件。

我收到此错误消息:[Errno 111] Connection refused当我尝试发送电子邮件时

这是我创建电子邮件并尝试在其视图中发送的地方:

try:
    msg = EmailMessage(subject, message, from_email, [receiver])
    msg.content_subtype = "html"
    msg.send()

我的设置文件如下:

EMAIL_HOST = "localhost"
DEFAULT_FROM_EMAIL = "myemail@gmail.com"
EMAIL_PORT = 25
EMAIL_BACKEND = "django.core.mail.backends.smtp.EmailBackend"

我尝试过使用进行测试发送python -m smtpd -n -c DebuggingServer localhost:1025并获得了成功,但是当最终归结为真正却没有成功时。

当我尝试从外壳执行send_mail时,我得到此回溯:

>>> from django.core.mail import send_mail
>>> send_mail('Test', 'Test', 'myemail@gmail.com', ['myemail@gmail.com'])
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/usr/local/lib/python2.6/dist-packages/django/core/mail/__init__.py", line 61, in send_mail
    connection=connection).send()
  File "/usr/local/lib/python2.6/dist-packages/django/core/mail/message.py", line 251, in send
    return self.get_connection(fail_silently).send_messages([self])
  File "/usr/local/lib/python2.6/dist-packages/django/core/mail/backends/smtp.py", line 79, in send_messages
    new_conn_created = self.open()
  File "/usr/local/lib/python2.6/dist-packages/django/core/mail/backends/smtp.py", line 42, in open
    local_hostname=DNS_NAME.get_fqdn())
  File "/usr/lib/python2.6/smtplib.py", line 239, in __init__
    (code, msg) = self.connect(host, port)
  File "/usr/lib/python2.6/smtplib.py", line 295, in connect
    self.sock = self._get_socket(host, port, self.timeout)
  File "/usr/lib/python2.6/smtplib.py", line 273, in _get_socket
    return socket.create_connection((port, host), timeout)
  File "/usr/lib/python2.6/socket.py", line 561, in create_connection
    raise error, msg
error: [Errno 111] Connection refused

我似乎对此一无所获。任何帮助或建议,将不胜感激。谢谢

另外,如果您还有其他想看的东西,只需评论一下即可。


阅读 611

收藏
2020-03-26

共1个答案

小编典典

你是否要使用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

然后尝试通过以下方法测试(django <1.4)

python manage.py shell
>>> from django.core.mail import send_mail
>>> send_mail('test email', 'hello world', to=['test@email.com'])

如果使用django 1.4,请使用以下命令:

python manage.py shell
>>> from django.core.mail import send_mail
>>> send_mail('test email', 'hello world', 'your@email.com', ['test@email.com'])

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

2020-03-26