如何修复ConnectionRefusedError:[WinError 10061]目标计算机主动拒绝了它? (Django电子邮件) [英] How to fix ConnectionRefusedError: [WinError 10061] the target machine actively refused it? (Django e.mails)

查看:924
本文介绍了如何修复ConnectionRefusedError:[WinError 10061]目标计算机主动拒绝了它? (Django电子邮件)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


如何使用Django电子邮件修复[WinError 10061]?

(无法建立
连接,因为目标计算机主动拒绝了
it)





How to fix [WinError 10061] with Django e.mails?

(No connection could be made because the target machine actively refused it)



我正在使用Django发送电子邮件。很明显,我遇到了常见的网络错误,但我读过的所有答案都没有解决。我相信套接字有问题,

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,





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



这是我尝试过的操作:



1)



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)



2)



关闭防火墙/防病毒软件

2)

Turn off Firewall / Antivirus

在python shell和Django应用中运行代码

Run the code in python shell and in Django app

我已经将smtp服务器从nslookup MX查询


I've got the smtp server out of a nslookup MX query

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()





现在,我想从本地Django应用发送电子邮件。
以后,我会将代码上传到pythonanywhere.com服务器。

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

感谢
Jaime

Thanks Jaime

推荐答案


[已修复]这是我的Django代码,用于发送电子邮件:

(大约12小时后...)



[FIXED] Here is my Django code to send e.mails:

(about 12 hours later...)





1)我缺少以下配置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


与以下内容无关:


  • 防火墙

  • 防病毒




  • 请确保您在<$ c $中具有正确的凭据c> settings.py

  • 我的hotmail最终无法正常工作。我必须使用主机服务提供的电子邮件。



我正在python shell中测试代码。
正确的做法是致电:

I was testing the code in python shell. The correct thing to do was to call:

python manage.py shell

,而不只是 python


instead of just python


发送电子邮件




Sending the e.mail


(配置好settings.py文件后)

可以打开Django Shell并复制代码,或者在Django
`views.py`文件中写入特定视图

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()






  • 不适用于端口465

  • E。邮件有时会去垃圾lder





谢谢
Jaime




Thanks Jaime

这篇关于如何修复ConnectionRefusedError:[WinError 10061]目标计算机主动拒绝了它? (Django电子邮件)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆