'Connection timed out when trying to send an email using the send_mail method

I am having trouble when it comes to sending emails using the send_mail method from the django.core.mail module.

The contents of the project email settings are as follows.

# --snip--
# Email Settings 
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'

EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = '[email protected]'
EMAIL_HOST_PASSWORD = 'myfakepassword'
EMAIL_PORT = 587
EMAIL_USE_TLS = False
EMAIL_USE_SSL = False
# --snip--

This is what I am doing in the Django shell.

Python 3.9.7 (default, Sep 10 2021, 14:59:43)
[GCC 11.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from django.core.mail import send_mail
>>> from django.conf import settings
>>> send_mail('Test Subject', 'Test message', settings.EMAIL_HOST_USER, ['[email protected]'], fail_silently=False)

After the send_mail method hangs for a long time, I am getting this Traceback below.

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/root/djangoenv/lib/python3.9/site-packages/django/core/mail/__init__.py", line 61, in send_mail
    return mail.send()
  File "/root/djangoenv/lib/python3.9/site-packages/django/core/mail/message.py", line 284, in send
    return self.get_connection(fail_silently).send_messages([self])
  File "/root/djangoenv/lib/python3.9/site-packages/django/core/mail/backends/smtp.py", line 102, in send_messages
    new_conn_created = self.open()
  File "/root/djangoenv/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 "/usr/lib/python3.9/smtplib.py", line 255, in __init__
    (code, msg) = self.connect(host, port)
  File "/usr/lib/python3.9/smtplib.py", line 341, in connect
    self.sock = self._get_socket(host, port, self.timeout)
  File "/usr/lib/python3.9/smtplib.py", line 312, in _get_socket
    return socket.create_connection((host, port), timeout,
  File "/usr/lib/python3.9/socket.py", line 844, in create_connection
    raise err
  File "/usr/lib/python3.9/socket.py", line 832, in create_connection
    sock.connect(sa)
TimeoutError: [Errno 110] Connection timed out

I also did a telnet command on the Gmail smtp server like so

# telnet smtp.gmail.com 25
Trying 2a00:1450:400c:c0c::6d...

But Getting this response from telnet

telnet unable to connect to remote host: Connection timed out

I have also enabled two-factor authentication on my Gmail account and I singing in using the App password. I also tried using the method of signing in using less secure apps but it is still the same thing.

So where could I be possibly going wrong?



Solution 1:[1]

The SMTP protocol makes use of a TCP socket connection to send the email. There can only be one TCP socket for each port on the client side.

In your case, Django will bind a TCP socket to the EMAIL_PORT you specified on the client side to send the email. That socket by default has an EMAIL_TIMEOUT of None according to the documentation, so the socket will persist indefinitely. You will be able to send the message the first time, but the second time you run your code you will get the above error since the port is already in use.

You should specify an EMAIL_TIMEOUT in your settings to close the sockets after some time interval. You should also free up the used TCP port before you try to send an email again following the steps of this thread (or just restart your PC).

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1