请问如何修复此 SMTPConnectError 错误?

Please how do I fix this SMTPConnectError Error?

我正在尝试向用户发送电子邮件以通过激活 link 验证 his/her 帐户,但是当我尝试注册用户时出现以下错误。我尝试使用 send_mail 方法,但它仍然给出相同的错误。请提供任何帮助,我们将不胜感激。

我的错误

Traceback (most recent call last):
  File "C:\Users\Donald\PycharmProjects\to_doApp\venv\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
    response = get_response(request)
  File "C:\Users\Donald\PycharmProjects\to_doApp\venv\lib\site-packages\django\core\handlers\base.py", line 181, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\Donald\PycharmProjects\to_doApp\venv\lib\site-packages\django\views\decorators\csrf.py", line 54, in wrapped_view
    return view_func(*args, **kwargs)
  File "C:\Users\Donald\PycharmProjects\to_doApp\store\accounts\views.py", line 44, in register
    send_email.send()
  File "C:\Users\Donald\PycharmProjects\to_doApp\venv\lib\site-packages\django\core\mail\message.py", line 284, in send
    return self.get_connection(fail_silently).send_messages([self])
  File "C:\Users\Donald\PycharmProjects\to_doApp\venv\lib\site-packages\django\core\mail\backends\smtp.py", line 102, in send_messages
    new_conn_created = self.open()
  File "C:\Users\Donald\PycharmProjects\to_doApp\venv\lib\site-packages\django\core\mail\backends\smtp.py", line 62, in open
    self.connection = self.connection_class(self.host, self.port, **connection_params)
  File "C:\Users\Donald\AppData\Local\Programs\Python\Python39\lib\smtplib.py", line 258, in __init__
    raise SMTPConnectError(code, msg)

Exception Type: SMTPConnectError at /accounts/register/
Exception Value: (421, b'service not available (connection refused, too many connections)')


我的views.py

@csrf_exempt
def register(request):
    if request.method == 'POST':
        form = RegistrationForm(request.POST)
        if form.is_valid():
            first_name = form.cleaned_data['first_name']
            last_name = form.cleaned_data['last_name']
            phone_number = form.cleaned_data['phone_number']
            email = form.cleaned_data['email']
            password = form.cleaned_data['password']
            username = email.split('@')[0]
            user = Account.objects.create_user(first_name=first_name, last_name=last_name, email=email, username=username, password=password)
            user.phone_number = phone_number

            #  USER ACTIVATION WITH TOKEN TO EMAIL ACCOUNT
            current_site = get_current_site(request)
            mail_subject = 'Activate your account'
            message = render_to_string('accounts/verification_email.html', {
                'user': user,
                'domain': current_site,
                'uid': urlsafe_base64_encode(force_bytes(user.pk)),  # Encode the users primary key
                'token': default_token_generator.make_token(user),
            })
            to_email = email
            send_email = EmailMessage(mail_subject, message, to=[to_email])
            send_email.send()
            user.save()
            messages.success(request, 'Registration Successful')
            return redirect('register')
    else:
        form = RegistrationForm()

    context = {'form': form}
    return render(request, 'accounts/register.html', context)


我的settings.py

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = 'my-email'
EMAIL_HOST_PASSWORD = 'my-password'
EMAIL_USE_TLS = True

我假设您为此使用 gmail。 Google 似乎不鼓励开发人员从 gmail 帐户进行群发邮件,所以如果您遇到错误,我不会感到惊讶。如果您使用太多,Gmail 将不可避免地阻止您这样做。

我建议尝试 MailJet 或 SendInBlue 等服务,它们专为交易电子邮件而设计,并为此提供更好的支持和错误记录。至少它会帮助您将问题缩小到提供者。

我推荐 http://www.sendinblue.com - 它设置起来很快,我从他们那里得到了很好的客户服务。与他们一起设置免费的个人测试帐户来诊断此类问题是值得的。