EMAIL BACKEND does not work give me error : ( No connection could be made because the target machine actively refused it)

I have the following error when trying to signup new user but if i change the following line : EMAIL_BACKEND = ‘django.core.mail.backends.smtp.EmailBackend’
to : EMAIL_BACKEND = ‘django.core.mail.backends.console.EmailBackend’
it’s running correctly and sending me the urls in the console
the error :

ConnectionRefusedError at /accounts/signup/

[WinError 10061] No connection could be made because the target machine actively refused it

Request Method: POST
Request URL: http://127.0.0.1:8000/accounts/signup/
Django Version: 4.2
Exception Type: ConnectionRefusedError
Exception Value: [WinError 10061] No connection could be made because the target machine actively refused it
Exception Location: C:\Users\20155\AppData\Local\Programs\Python\Python311\Lib\socket.py, line 836, in create_connection
Raised during: allauth.account.views.SignupView

Keep in your mind it gives me an error but it still working if i go to the admin panel the user will be shown but if i tried to login it will refuse it

1 Like

There may be two separate issues here.

First, when you set

what are your other EMAIL_ settings in your settings file? That connection refused error is frequently caused by not being able to connect to the smtp server.

The other issue:

Might be caused by the user object not being created correctly. You’ll want to check the user object in the admin in more detail to ensure that it’s not just shown, but that all the data is correct.

This is my email setting :

ACCOUNT_AUTHENTICATION_METHOD = “username_email”

ACCOUNT_EMAIL_REQUIRED = True

ACCOUNT_EMAIL_VERIFICATION = “mandatory”

EMAIL_BACKEND = ‘django.core.mail.backends.smtp.EmailBackend’

EMAIL_HOST = ‘smtp.sendgrid.com

EMAIL_FROM = ‘kickfundingapp@gmail.com’

EMAIL_HOST_USER = ‘apikey’

EMAIL_HOST_PASSWORD = ‘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’

EMAIL_PORT = 587

EMAIL_USE_TLS = True

faced with the same error.
I can’t solve the error

@JustFox - Are you also using sendgrid, or are you using a different email provider? If you’re using a different service for sending emails, I suggest you open up a new topic.

i wish anyone can help us with this cause i tried different email providers such sendgrid and mailgun and still unworking

Check your Windows firewall settings to ensure that your Django application can create an outbound connection on port 587. Also, where is this server being hosted? Are you running it locally? Or is this hosted elsewhere?

From the sendgrid’s smtp docs page:

SendGrid accepts unencrypted and TLS connections on ports 25, 587, & 2525. You can also connect via SSL on port 465.

Seems sendgrid accepts only unencrypted connections on port 587. You could also try SSL with port 465.

I have a different service provider. but any provider does not work. win 10/11 firewall is disabled

How do you draw that conclusion from the docs? You just said that SendGrid accepts unencrypted and TLS connections on those ports.

Woops, kinda overread the TLS notion there :sweat_smile:

Same question for you as my follow-up to the original poster - where is your system hosted? Are they doing anything to block outbound smtp?

locally on my pc. there is no smtp blocking

What I would suggest is that you start with a small self-contained python script to send emails, and run it to try and send an email. It’s going to be easier to test and debug than running everything through Django. Once you’ve got that working, then it becomes easy to configure Django appropriately.

Sendgrid has a package/library that you use, I don’t believe it works via SMTP like a typical mail server.

Eg.

from sendgrid.helpers.mail import Attachment, Content, Email, Mail
import sendgrid

        html_content = "This is a test email."
        sg = sendgrid.SendGridAPIClient(apikey=settings.SENDGRID_API_KEY)

        from_email = Email("Test User <user@example.com>")
        to_email = Email("your_email@example.com")
        content = Content("text/html", html_content)
        mail = Mail(from_email, "Subject line.", to_email, content)

        try:
            _ = sg.client.mail.send.post(request_body=mail.get())
        except Exception as e:
            print(e)

I just extracted that from an old project, it is not designed to be copy/pasted.

Excuse me for jumping in, I was just was lurking and realised I could possibly help.

i tried as you said sir but i got this error :

SMTPDataError at /api/dj-rest-auth/registration/

(550, b’The from address does not match a verified Sender Identity. Mail cannot be sent until this error is resolved. Visit Sender Identity | Twilio to see the Sender Identity requirements’)

Request Method: POST
Request URL: http://127.0.0.1:8000/api/dj-rest-auth/registration/
Django Version: 4.2
Exception Type: SMTPDataError
Exception Value: (550, b’The from address does not match a verified Sender Identity. Mail cannot be sent until this error is resolved. Visit Sender Identity | Twilio to see the Sender Identity requirements’)
but i checked my single sender was verified

Excellent! Not only is this error message telling you exactly what’s wrong, it’s also telling you what you need to do to fix it.

That is not an accurate statement. See SMTP Service | SMTP Relay | Start for Free | SendGrid

but my single sender was confirmed and my current address is the same in the settings of the provider and this is my code :

ACCOUNT_EMAIL_REQUIRED = True

ACCOUNT_EMAIL_VERIFICATION = ‘mandatory’

LOGIN_ON_EMAIL_CONFIRMATION = True

EMAIL_BACKEND = “django.core.mail.backends.smtp.EmailBackend”

EMAIL_HOST = “smtp.sendgrid.net

EMAIL_FROM = “kickfundingapp@gmail.com

EMAIL_HOST_USER = “apikey”

EMAIL_HOST_PASSWORD = “SG.INQQHB4eTTmNNPzoyoN9kA.XbHXTeskI-7o9KujIYHwnU05P2mjOomUdVmk9NPaXXk”

EMAIL_PORT = 587

EMAIL_USE_TLS = True

How are you sending the email here? (Please post the actual code you are using for this test.)