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

**URL:** <https://forum.djangoproject.com/t/email-backend-does-not-work-give-me-error-no-connection-could-be-made-because-the-target-machine-actively-refused-it/20654>\
**Category:** Deployment\
**Created:** [May 1, 2023, 1:28pm UTC](https://forum.djangoproject.com/t/email-backend-does-not-work-give-me-error-no-connection-could-be-made-because-the-target-machine-actively-refused-it/20654 "2023-05-01T13:28:14Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![ahmedesmail07](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/ahmedesmail07/32/12298_2.png) [@ahmedesmail07](https://forum.djangoproject.com/u/ahmedesmail07)\
**Post date:** [May 1, 2023, 1:28pm UTC](https://forum.djangoproject.com/t/email-backend-does-not-work-give-me-error-no-connection-could-be-made-because-the-target-machine-actively-refused-it/20654/1 "2023-05-01T13:28:14Z")

</div>

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/](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

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [May 1, 2023, 2:17pm UTC](https://forum.djangoproject.com/t/email-backend-does-not-work-give-me-error-no-connection-could-be-made-because-the-target-machine-actively-refused-it/20654/2 "2023-05-01T14:17:41Z")

</div>

There may be two separate issues here.

First, when you set

> [@ahmedesmail07](#):
>
> EMAIL\_BACKEND = ‘django.core.mail.backends.smtp.EmailBackend’

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:

> [@ahmedesmail07](#):
>
> if i go to the admin panel the user will be shown but if i tried to login it will refuse it

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.

---

<div class="post-metadata">

**Author:** ![ahmedesmail07](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/ahmedesmail07/32/12298_2.png) [@ahmedesmail07](https://forum.djangoproject.com/u/ahmedesmail07)\
**Post date:** [May 1, 2023, 8:50pm UTC](https://forum.djangoproject.com/t/email-backend-does-not-work-give-me-error-no-connection-could-be-made-because-the-target-machine-actively-refused-it/20654/3 "2023-05-01T20:50:59Z")

</div>

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](http://smtp.sendgrid.com)’

EMAIL\_FROM = ‘kickfundingapp@gmail.com’

EMAIL\_HOST\_USER = ‘apikey’

EMAIL\_HOST\_PASSWORD = ‘SG.EA\_vT8IyTBiOVPjzyK8tGg.c6QnLq4dPY9seJKX0oWo5qwTKN1lSEOBBcjg0ptDAUM’

EMAIL\_PORT = 587

EMAIL\_USE\_TLS = True

---

<div class="post-metadata">

**Author:** ![JustFox](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/justfox/32/13621_2.png) [@JustFox](https://forum.djangoproject.com/u/JustFox)\
**Post date:** [May 8, 2023, 9:52pm UTC](https://forum.djangoproject.com/t/email-backend-does-not-work-give-me-error-no-connection-could-be-made-because-the-target-machine-actively-refused-it/20654/4 "2023-05-08T21:52:44Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [May 8, 2023, 9:53pm UTC](https://forum.djangoproject.com/t/email-backend-does-not-work-give-me-error-no-connection-could-be-made-because-the-target-machine-actively-refused-it/20654/5 "2023-05-08T21:53:55Z")

</div>

@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.

---

<div class="post-metadata">

**Author:** ![ahmedesmail07](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/ahmedesmail07/32/12298_2.png) [@ahmedesmail07](https://forum.djangoproject.com/u/ahmedesmail07)\
**Post date:** [May 8, 2023, 9:54pm UTC](https://forum.djangoproject.com/t/email-backend-does-not-work-give-me-error-no-connection-could-be-made-because-the-target-machine-actively-refused-it/20654/6 "2023-05-08T21:54:18Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [May 8, 2023, 9:55pm UTC](https://forum.djangoproject.com/t/email-backend-does-not-work-give-me-error-no-connection-could-be-made-because-the-target-machine-actively-refused-it/20654/7 "2023-05-08T21:55:57Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![jerch](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/jerch/32/13571_2.png) [@jerch](https://forum.djangoproject.com/u/jerch)\
**Post date:** [May 8, 2023, 10:05pm UTC](https://forum.djangoproject.com/t/email-backend-does-not-work-give-me-error-no-connection-could-be-made-because-the-target-machine-actively-refused-it/20654/8 "2023-05-08T22:05:16Z")

</div>

From the sendgrid’s [smtp docs page](https://docs.sendgrid.com/for-developers/sending-email/getting-started-smtp):

> 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.

---

<div class="post-metadata">

**Author:** ![JustFox](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/justfox/32/13621_2.png) [@JustFox](https://forum.djangoproject.com/u/JustFox)\
**Post date:** [May 8, 2023, 10:06pm UTC](https://forum.djangoproject.com/t/email-backend-does-not-work-give-me-error-no-connection-could-be-made-because-the-target-machine-actively-refused-it/20654/9 "2023-05-08T22:06:26Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [May 8, 2023, 10:11pm UTC](https://forum.djangoproject.com/t/email-backend-does-not-work-give-me-error-no-connection-could-be-made-because-the-target-machine-actively-refused-it/20654/10 "2023-05-08T22:11:22Z")

</div>

> [@jerch](#):
>
> Seems sendgrid accepts only unencrypted connections on port 587.

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

---

<div class="post-metadata">

**Author:** ![jerch](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/jerch/32/13571_2.png) [@jerch](https://forum.djangoproject.com/u/jerch)\
**Post date:** [May 8, 2023, 10:13pm UTC](https://forum.djangoproject.com/t/email-backend-does-not-work-give-me-error-no-connection-could-be-made-because-the-target-machine-actively-refused-it/20654/11 "2023-05-08T22:13:14Z")

</div>

Woops, kinda overread the TLS notion there 😅

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [May 8, 2023, 10:14pm UTC](https://forum.djangoproject.com/t/email-backend-does-not-work-give-me-error-no-connection-could-be-made-because-the-target-machine-actively-refused-it/20654/12 "2023-05-08T22:14:01Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![JustFox](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/justfox/32/13621_2.png) [@JustFox](https://forum.djangoproject.com/u/JustFox)\
**Post date:** [May 8, 2023, 11:30pm UTC](https://forum.djangoproject.com/t/email-backend-does-not-work-give-me-error-no-connection-could-be-made-because-the-target-machine-actively-refused-it/20654/13 "2023-05-08T23:30:38Z")

</div>

locally on my pc. there is no smtp blocking

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [May 9, 2023, 12:04am UTC](https://forum.djangoproject.com/t/email-backend-does-not-work-give-me-error-no-connection-could-be-made-because-the-target-machine-actively-refused-it/20654/14 "2023-05-09T00:04:38Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![gcain](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/gcain/32/13626_2.png) [@gcain](https://forum.djangoproject.com/u/gcain)\
**Post date:** [May 9, 2023, 6:44am UTC](https://forum.djangoproject.com/t/email-backend-does-not-work-give-me-error-no-connection-could-be-made-because-the-target-machine-actively-refused-it/20654/15 "2023-05-09T06:44:12Z")

</div>

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

> **[sendgrid](https://pypi.org/project/sendgrid/)**
>
> Twilio SendGrid library for Python

Eg.

```auto
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.

---

<div class="post-metadata">

**Author:** ![ahmedesmail07](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/ahmedesmail07/32/12298_2.png) [@ahmedesmail07](https://forum.djangoproject.com/u/ahmedesmail07)\
**Post date:** [May 9, 2023, 11:53am UTC](https://forum.djangoproject.com/t/email-backend-does-not-work-give-me-error-no-connection-could-be-made-because-the-target-machine-actively-refused-it/20654/16 "2023-05-09T11:53:46Z")

</div>

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 | SendGrid Docs | Twilio](https://sendgrid.com/docs/for-developers/sending-email/sender-identity/) to see the Sender Identity requirements’)

| Request Method: | POST |
| --- | --- |
| Request URL: | [http://127.0.0.1:8000/api/dj-rest-auth/registration/](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 | SendGrid Docs | Twilio](https://sendgrid.com/docs/for-developers/sending-email/sender-identity/) to see the Sender Identity requirements’) |
| but i checked my single sender was verified | |

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [May 9, 2023, 12:58pm UTC](https://forum.djangoproject.com/t/email-backend-does-not-work-give-me-error-no-connection-could-be-made-because-the-target-machine-actively-refused-it/20654/17 "2023-05-09T12:58:22Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [May 9, 2023, 12:59pm UTC](https://forum.djangoproject.com/t/email-backend-does-not-work-give-me-error-no-connection-could-be-made-because-the-target-machine-actively-refused-it/20654/18 "2023-05-09T12:59:42Z")

</div>

> [@gcain](#):
>
> Sendgrid has a package/library that you use, I don’t believe it works via SMTP like a typical mail server.

That is not an accurate statement. See [SMTP Service | SMTP Relay | Start for Free | SendGrid](https://sendgrid.com/solutions/email-api/smtp-service/)

---

<div class="post-metadata">

**Author:** ![ahmedesmail07](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/ahmedesmail07/32/12298_2.png) [@ahmedesmail07](https://forum.djangoproject.com/u/ahmedesmail07)\
**Post date:** [May 9, 2023, 1:08pm UTC](https://forum.djangoproject.com/t/email-backend-does-not-work-give-me-error-no-connection-could-be-made-because-the-target-machine-actively-refused-it/20654/19 "2023-05-09T13:08:42Z")

</div>

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](http://smtp.sendgrid.net)”

EMAIL\_FROM = “[kickfundingapp@gmail.com](mailto:kickfundingapp@gmail.com)”

EMAIL\_HOST\_USER = “apikey”

EMAIL\_HOST\_PASSWORD = “SG.INQQHB4eTTmNNPzoyoN9kA.XbHXTeskI-7o9KujIYHwnU05P2mjOomUdVmk9NPaXXk”

EMAIL\_PORT = 587

EMAIL\_USE\_TLS = True

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [May 9, 2023, 1:14pm UTC](https://forum.djangoproject.com/t/email-backend-does-not-work-give-me-error-no-connection-could-be-made-because-the-target-machine-actively-refused-it/20654/20 "2023-05-09T13:14:34Z")

</div>

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

[Next page](https://forum.djangoproject.com/t/email-backend-does-not-work-give-me-error-no-connection-could-be-made-because-the-target-machine-actively-refused-it/20654.md?page=2)
