title: How to fix ConnectionRefusedError: [WinError 10061] the target machine actively refused it? (Django e.mails)
body: <p>I am using Django to send e-mails. I am getting a common network error apparently, but not any of the answers I've read solved. I have a problem with the socket I believe,</p>
  <p>When I send the e-mail I get the following error:</p>
  <pre><code>Traceback (most recent call last):
  File &quot;&lt;stdin&gt;&quot;, line 1, in &lt;module&gt;
  File &quot;C:\Users\Jaime\Miniconda3\envs\mydjango\lib\site- 
  packages\django\core\mail\message.py&quot;, line 291, in send
  return self.get_connection(fail_silently).send_messages([self])
  File &quot;C:\Users\Jaime\Miniconda3\envs\mydjango\lib\site- 
  packages\django\core\mail\backends\smtp.py&quot;, line 103, in send_messages
  new_conn_created = self.open()
  File &quot;C:\Users\Jaime\Miniconda3\envs\mydjango\lib\site- 
  packages\django\core\mail\backends\smtp.py&quot;, line 63, in open
  self.connection = self.connection_class(self.host, self.port, 
  **connection_params)
  File &quot;C:\Users\Jaime\Miniconda3\envs\mydjango\lib\smtplib.py&quot;, line 251, 
  in __init__
  (code, msg) = self.connect(host, port)
  File &quot;C:\Users\Jaime\Miniconda3\envs\mydjango\lib\smtplib.py&quot;, line 336, 
  in connect
  self.sock = self._get_socket(host, port, self.timeout)
  File &quot;C:\Users\Jaime\Miniconda3\envs\mydjango\lib\smtplib.py&quot;, line 307, 
  in _get_socket
  self.source_address)
  File &quot;C:\Users\Jaime\Miniconda3\envs\mydjango\lib\socket.py&quot;, line 727, 
  in create_connection
  raise err
  File &quot;C:\Users\Jaime\Miniconda3\envs\mydjango\lib\socket.py&quot;, line 716, 
  in create_connection
  sock.connect(sa)
  ConnectionRefusedError: [WinError 10061] No connection could be made 
  because 
  the target machine actively refused it
  </code></pre>
  <p>Here is what I've tried:</p>
  <p>1.
  import socket
  socket.getaddrinfo('hotmail-com.olc.protection.outlook.com', 80)
  socket.getaddrinfo('smtp.hotmail.com', 8000)
  socket.getaddrinfo('smtp.hotmail.com', 587)</p>
  <ol start="2">
  <li>Turn off Firewall / Antivirus</li>
  <li>Run the code in python shell and in Django app</li>
  <li>I've got the smtp server out of a nslookup MX query</li>
  </ol>
  <p><strong>I am running this test code in the python shell:</strong></p>
  <pre><code>from django.conf import settings
  from django.core.mail import EmailMessage
  settings.configure()
  
  from django.core.mail import send_mail
  
  import socket
  socket.getaddrinfo('hotmail-com.olc.protection.outlook.com', 80)
  
  EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
  EMAIL_USE_TLS = True
  EMAIL_HOST = 'hotmail-com.olc.protection.outlook.com'
  EMAIL_HOST_USER = '*******@hotmail.com'
  EMAIL_HOST_PASSWORD = '******'
  EMAIL_PORT = 587
  
  mail_subject = 'Activate your blog account.'
              
  
  to_email = &quot;******@hotmail&quot;
  email = EmailMessage(
      mail_subject, &quot;hello&quot;, to=[to_email]
  )
  email.send()
  </code></pre>
  <p><strong>Expected Result:</strong></p>
  <p>For now I want to send e-mails from localhost Django app.
  In the future I will upload the code to pythonanywhere.com server.</p>