微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Django 发送电子邮件错误:ConnectionRefusedError:[Errno 61] 连接被拒绝

如何解决Django 发送电子邮件错误:ConnectionRefusedError:[Errno 61] 连接被拒绝

当我遇到此错误时,我正尝试使用 send_mail() 中的 django.core.mail 函数通过 Django 发送电子邮件

ConnectionRefusedError: [Errno 61] Connection refused

这是我的代码,直接来自文档:

from django.core.mail import send_mail

send_mail(
    'Subject here','Here is the message.','from@example.com',['to@example.com'],fail_silently=False,)

我将 from@example.com 替换为电子邮件 ID 之一,并将 to@example.com 替换为我的其他电子邮件 ID 之一。

当我运行 runserver 时,我收到这个奇怪的错误

$ python3 manage.py runserver

Exception in thread django-main-thread:
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/threading.py",line 954,in _bootstrap_inner
    self.run()
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/threading.py",line 892,in run
    self._target(*self._args,**self._kwargs)
  File "/Users/adithraghav/Documents/Work/env/lib/python3.9/site-packages/django/utils/autoreload.py",line 64,in wrapper
    fn(*args,**kwargs)
  File "/Users/adithraghav/Documents/Work/env/lib/python3.9/site-packages/django/core/management/commands/runserver.py",line 118,in inner_run
    self.check(display_num_errors=True)
  File "/Users/adithraghav/Documents/Work/env/lib/python3.9/site-packages/django/core/management/base.py",line 419,in check
    all_issues = checks.run_checks(
  File "/Users/adithraghav/Documents/Work/env/lib/python3.9/site-packages/django/core/checks/registry.py",line 76,in run_checks
    new_errors = check(app_configs=app_configs,databases=databases)
  File "/Users/adithraghav/Documents/Work/env/lib/python3.9/site-packages/django/core/checks/urls.py",line 40,in check_url_namespaces_unique
    all_namespaces = _load_all_namespaces(resolver)
  File "/Users/adithraghav/Documents/Work/env/lib/python3.9/site-packages/django/core/checks/urls.py",line 57,in _load_all_namespaces
    url_patterns = getattr(resolver,'url_patterns',[])
  File "/Users/adithraghav/Documents/Work/env/lib/python3.9/site-packages/django/utils/functional.py",line 48,in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/Users/adithraghav/Documents/Work/env/lib/python3.9/site-packages/django/urls/resolvers.py",line 598,in url_patterns
    patterns = getattr(self.URLconf_module,"urlpatterns",self.URLconf_module)
  File "/Users/adithraghav/Documents/Work/env/lib/python3.9/site-packages/django/utils/functional.py",line 591,in URLconf_module
    return import_module(self.URLconf_name)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/importlib/__init__.py",line 127,in import_module
    return _bootstrap._gcd_import(name[level:],package,level)
  File "<frozen importlib._bootstrap>",line 1030,in _gcd_import
  File "<frozen importlib._bootstrap>",line 1007,in _find_and_load
  File "<frozen importlib._bootstrap>",line 986,in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>",line 680,in _load_unlocked
  File "<frozen importlib._bootstrap_external>",line 790,in exec_module
  File "<frozen importlib._bootstrap>",line 228,in _call_with_frames_removed
  File "/Users/adithraghav/Documents/Work/freekode/freekode/urls.py",line 22,in <module>
    from users import views as user_views
  File "/Users/adithraghav/Documents/Work/freekode/users/views.py",line 7,in <module>
    send_mail(
  File "/Users/adithraghav/Documents/Work/env/lib/python3.9/site-packages/django/core/mail/__init__.py",line 61,in send_mail
    return mail.send()
  File "/Users/adithraghav/Documents/Work/env/lib/python3.9/site-packages/django/core/mail/message.py",line 284,in send
    return self.get_connection(fail_silently).send_messages([self])
  File "/Users/adithraghav/Documents/Work/env/lib/python3.9/site-packages/django/core/mail/backends/smtp.py",line 102,in send_messages
    new_conn_created = self.open()
  File "/Users/adithraghav/Documents/Work/env/lib/python3.9/site-packages/django/core/mail/backends/smtp.py",line 62,in open
    self.connection = self.connection_class(self.host,self.port,**connection_params)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/smtplib.py",line 253,in __init__
    (code,msg) = self.connect(host,port)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/smtplib.py",line 339,in connect
    self.sock = self._get_socket(host,port,self.timeout)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/smtplib.py",line 310,in _get_socket
    return socket.create_connection((host,port),timeout,File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/socket.py",line 843,in create_connection
    raise err
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/socket.py",line 831,in create_connection
    sock.connect(sa)
ConnectionRefusedError: [Errno 61] Connection refused

我正在使用 Django 3.2Python 3.9.2。 我真的很感激一些帮助。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。