如何使用Django电子邮件修复[WinError 10061]?
(否 由于目标计算机主动拒绝,因此可以建立连接
我正在使用Django发送电子邮件。我显然收到一个常见的网络错误,但是我所读的任何答案都没有解决。我相信我的插座有问题,
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
import socket
socket.getaddrinfo('hotmail-com.olc.protection.outlook.com', 80)
socket.getaddrinfo('smtp.hotmail.com', 8000)
socket.getaddrinfo('smtp.hotmail.com', 587)
关闭防火墙/防病毒
在python shell和Django应用中运行代码
我已经从nslookup MX查询中获取了smtp服务器
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()
现在,我想从localhost Django应用程序发送电子邮件。 将来,我会将代码上传到pythonanywhere.com服务器。
谢谢 海梅
答案 0 :(得分:0)
EMAIL_*
值需要在您的设置模块中,而不是在脚本中。如果没有这些值,则用于处理发送消息的EmailBackend
类将默认尝试连接到localhost:25
。由于您不在本地系统上运行SMTP服务器,因此它拒绝连接。
您不需要EMAIL_BACKEND
值,因为它是默认值,而是将其他EMAIL_*
值移到您的设置模块中。
答案 1 :(得分:0)
[已修复]这是我的Django代码,用于发送电子邮件:
(大约12小时后...)
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
与以下内容无关:
settings.py
中具有正确的凭据我正在测试python shell中的代码。 正确的做法是致电:
python manage.py shell
而不只是python
发送电子邮件
(配置好settings.py文件后)
您可以打开Django shell并复制代码或在Django中编写 `views.py`提供特定视图
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()
谢谢
海梅