Send_mail gives me a timeout error.

2,253 views
Skip to first unread message

Mostapha Bouderka

unread,
May 5, 2021, 5:53:26 PM5/5/21
to Django users
Hi.
I'm working on a project with django and django rest framework.
In one of my views, I tried implementing a simple send_mail, but it always gives me a TimeOut error.
I already configured the settings (Email backend as smtp, email host, password, ...), and also alowed my email to accept 'less secured apps',  but it doesn't seem to work.
I also tried using basic python by importing and working with smtp directly but I always get the same error.

Here is the error:
A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 

Can someone please help?
Thanks in advance. 

Kasper Laudrup

unread,
May 5, 2021, 6:47:50 PM5/5/21
to django...@googlegroups.com
Hi Mostapha,

Thanks for taking your time to write a proper question with relevant
details. Not really related to the issue your facing, but a lot of
people could learn from that.

Considering the error you're seeing:

On 05/05/2021 23.53, Mostapha Bouderka wrote:
> A connection attempt failed because the connected party did not properly
> respond after a period of time, or established connection failed because
> connected host has failed to respond 
>

This error is coming from the operating system running your Django
application and is basically saying that it cannot contact the server
you want to use for sending your email.

This is what I would do to figure out the cause of the error:

Double check if you have really written the correct host and port in
your settings. We all make typos.

If you are absolutely certain about that, then I would try to connect to
the server in the lowest level way possible. Something like:

# telnet <host> <port>

Should work on most operating systems. Do that on the server/host that
is actually running your Django application. If you get the same error,
then you've come a bit closer to solving the issue.

If you do get the same error, then you problem is network related or
related to the mail server you're trying to reach. That has already
narrowed down your problem quite a bit.

Hope this makes some sense.

Additional information that might be useful to help you would be to know
which operating system is running your Django application and which kind
of mail server you're trying to reach.

Kind regards,

Kasper Laudrup

OpenPGP_signature

Mostapha Bouderka

unread,
May 6, 2021, 5:58:20 AM5/6/21
to django...@googlegroups.com
Hey Kasper, thanks for your reply.

So I'm still in development mode, and i'm using windows for the moment.
I'm trying to send mails to 'gmail', in port '465', the server address is 'smtp.gmail.com'.



--
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/f21Tmy8KyaI/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/4442cdb7-7b12-8617-e538-f1e1197dfb30%40stacktrace.dk.

Kasper Laudrup

unread,
May 6, 2021, 6:57:52 AM5/6/21
to django...@googlegroups.com
On 5/6/21 11:57 AM, Mostapha Bouderka wrote:
> Hey Kasper, thanks for your reply.
>
> So I'm still in development mode, and i'm using windows for the moment.
> I'm trying to send mails to 'gmail', in port '465', the server address
> is 'smtp.gmail.com <http://smtp.gmail.com>'.
>

So what happens if you try to run:

# telnet smtp.gmail.com 465

from a command line?

Kind regards,

Kasper Laudrup

Mostapha Bouderka

unread,
May 6, 2021, 10:56:22 AM5/6/21
to Django users
It returns a terminal with this at the top : '220 smtp.gmail.com ESMTP t8sm4659629wrs.25 - gsmtp'

Mostapha Bouderka

unread,
May 6, 2021, 11:10:55 AM5/6/21
to django...@googlegroups.com
Thanks Kasper, it works right now.
I changed the port to '587', and now it works perfectly.

Thank you for your assistance, and have a great day.

--
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/f21Tmy8KyaI/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users...@googlegroups.com.

Kasper Laudrup

unread,
May 6, 2021, 2:54:36 PM5/6/21
to django...@googlegroups.com
On 06/05/2021 17.10, Mostapha Bouderka wrote:
> Thanks Kasper, it works right now.
> I changed the port to '587', and now it works perfectly.
>

Good to hear.

In case you or someone else are wondering, port 465 is used for TLS/SSL
communication, whereas 587 is used for STARTTLS which is something
slightly different.

I can only assume that you tried to telnet to port 587 since you'll only
get a welcome banner like the one you showed if the communication is
initially plaintext. The communication will only be encrypted once you
issue a STARTTLS command on that port.

Using port 465 requires an initial SSL/TLS handshake (non-plaintext)
similar to HTTPS, so you'll not get any human readable output there.

To be honest I don't really understand why you would get a timeout error
in the first place. Sending a command like "STARTTLS" or similar to a
connection that expects a TLS handshake would most likely just make the
server go "I have no idea what you're talking about, goodbye", ie. close
the connection.

Anyway, glad it worked out for you and hope my debugging tips helped a
bit :-)

Kind regards,

Kasper Laudrup


OpenPGP_signature
Reply all
Reply to author
Forward
0 new messages