5

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,

When I send the e-mail I get the following error:

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\Jaime\Miniconda3\envs\mydjango\lib\site- 
packages\django\core\mail\message.py", line 291, in send
return self.get_connection(fail_silently).send_messages([self])
File "C:\Users\Jaime\Miniconda3\envs\mydjango\lib\site- 
packages\django\core\mail\backends\smtp.py", line 103, in send_messages
new_conn_created = self.open()
File "C:\Users\Jaime\Miniconda3\envs\mydjango\lib\site- 
packages\django\core\mail\backends\smtp.py", line 63, in open
self.connection = self.connection_class(self.host, self.port, 
**connection_params)
File "C:\Users\Jaime\Miniconda3\envs\mydjango\lib\smtplib.py", line 251, 
in __init__
(code, msg) = self.connect(host, port)
File "C:\Users\Jaime\Miniconda3\envs\mydjango\lib\smtplib.py", line 336, 
in connect
self.sock = self._get_socket(host, port, self.timeout)
File "C:\Users\Jaime\Miniconda3\envs\mydjango\lib\smtplib.py", line 307, 
in _get_socket
self.source_address)
File "C:\Users\Jaime\Miniconda3\envs\mydjango\lib\socket.py", line 727, 
in create_connection
raise err
File "C:\Users\Jaime\Miniconda3\envs\mydjango\lib\socket.py", line 716, 
in create_connection
sock.connect(sa)
ConnectionRefusedError: [WinError 10061] No connection could be made 
because 
the target machine actively refused it

Here is what I've tried:

1. import socket socket.getaddrinfo('hotmail-com.olc.protection.outlook.com', 80) socket.getaddrinfo('smtp.hotmail.com', 8000) socket.getaddrinfo('smtp.hotmail.com', 587)

  1. Turn off Firewall / Antivirus
  2. Run the code in python shell and in Django app
  3. I've got the smtp server out of a nslookup MX query

I am running this test code in the python shell:

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 = "******@hotmail"
email = EmailMessage(
    mail_subject, "hello", to=[to_email]
)
email.send()

Expected Result:

For now I want to send e-mails from localhost Django app. In the future I will upload the code to pythonanywhere.com server.

Neuron
  • 5,141
  • 5
  • 38
  • 59
Jaime38130
  • 167
  • 1
  • 1
  • 14

2 Answers2

4

Here is my Django code to send e-mails:

  1. I was missing configurations in settings.py

    settings.py:

    DEFAULT_FROM_EMAIL = 'admin@********.com'
    SERVER_EMAIL = 'admin@********.com'
    EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
    EMAIL_USE_TLS = True
    EMAIL_HOST = 'mail.********.com'
    EMAIL_HOST_USER = 'admin@********.com'
    EMAIL_HOST_PASSWORD = '*******'
    EMAIL_PORT = 587
    
  2. Windows configurations

    It had nothing to do with:

    • Firewall
    • Antivirus
  3. I was not sure about the name of the SMTP server

    • Make sure you have the correct credentials in settings.py
    • My hotmail didn't work in the end. I had to use an e.mail provided by host services.
  4. Python shell is different than Django shell I was testing the code in python shell. The correct thing to do was to call:

    python manage.py shell
    

    instead of just python

Sending the e.mail (After you have your settings.py file configured):

you can either open the Django shell and copy the code or write in the Django views.py file a specific view

from django.core.mail import EmailMessage

mail_subject = 'Active a sua conta.'
message = "message"
to_email = "jaimedcsilva@hotmail.com"

email = EmailMessage(
    mail_subject, message,to=[to_email]
)
email.send()

Worth mentioning:

  • Does not work with port 465
  • E.mail sometimes goes to junk folder
Neuron
  • 5,141
  • 5
  • 38
  • 59
Jaime38130
  • 167
  • 1
  • 1
  • 14
0

The EMAIL_* values need to be in your settings module, not in your script. Without those values, the EmailBackend class that handles sending the message defaults to trying to connect to localhost:25. Since you're not running an SMTP server on your local system, it's refusing the connection.

You don't need the EMAIL_BACKEND value because it's the default, but move the other EMAIL_* values into your settings module.

Nathan Vērzemnieks
  • 5,495
  • 1
  • 11
  • 23
  • I moved the EMAIL_* values to settings.py in the Django project. I tried the EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend' the e.mail is sent successfully to cmd. I also tried with EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend' and without, and in both cases I get a similar but different error message: [WinError 10060] 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 – Jaime38130 Mar 29 '19 at 16:36
  • That sounds like progress! Have you made sure you can actually send mail via this SMTP server? – Nathan Vērzemnieks Mar 29 '19 at 16:37
  • I have some more updates. As I was not actually sure about the previous SMTP server, I changed it to another, that I've used previously for the same purpose, but with PHP. The recommendation is to use port 465, it takes about 2 minutes untill I get the error: in getreply raise SMTPServerDisconnected("Connection unexpectedly closed") smtplib.SMTPServerDisconnected: Connection unexpectedly closed I also tried port 587, I get almost immediatly the following errror: in sendmail raise SMTPDataError(code, resp) smtplib.SMTPDataError: (550, b'"Message rejected by local rules"') – Jaime38130 Mar 29 '19 at 19:46
  • This is starting to sound like a different question! And I'm not sure what more to suggest. I'd recommend updating your question with your current code and error messages, or asking a new one. – Nathan Vērzemnieks Mar 29 '19 at 20:15
  • 1
    yes! I think it is better to create a new post. I ended up moving from python shell to Django again. I think I'm getting closer to get this working. just got another email from hostgator, testing it right now. Thanks – Jaime38130 Mar 29 '19 at 20:21
  • Does all this(win 10061 Error in Django) will depend on the OS we are using. Because In Linux I am not facing the issue but with Windows I am facing. Please suggest me. – Pallavi Konda Nov 06 '19 at 05:42