DJANGO EMAIL CONFIRMATION: [WinError 10061] 由于目标机器主动拒绝,无法建立连接
Posted
技术标签:
【中文标题】DJANGO EMAIL CONFIRMATION: [WinError 10061] 由于目标机器主动拒绝,无法建立连接【英文标题】:DJANGO EMAIL CONFIRMATION: [WinError 10061] No connection could be made because the target machine actively refused it 【发布时间】:2021-02-08 06:43:53 【问题描述】:我正在尝试发送电子邮件验证码以确认帐户。
现在,问题是我想先给自己发一封电子邮件,然后再给我的另一封电子邮件。我已经关闭了我的防病毒软件,所以这应该不是问题。除此之外,我无法弄清楚我做错了什么,因为它没有在 gmail 帐户上发送电子邮件。请指出我做错了什么。我什至应用了this 线程提到的所有修复。
views.py
from django.core.mail import send_mail
from django.shortcuts import render,redirect
from django.contrib import messages,auth
from django.contrib.auth.models import User # this table already exists in django we import it
from django.contrib.auth import authenticate, login
from django.conf import settings
from django.core.mail import EmailMessage
def register(request):
if request.method=='POST':
fname = request.POST['fname']
lname=request.POST['lname'] #request.post[id of the input field]
email = request.POST['email']
password = request.POST['pass']
password2 = request.POST['confirmpass']
agree=request.POST.get('agree')
if fname == '':
messages.warning(request, 'Please enter First name!')
return redirect('register')
if lname == '':
messages.warning(request, 'Please enter Last name!')
return redirect('register')
if email == '':
messages.warning(request, 'Please enter Email!')
return redirect('register')
if password == '':
messages.warning(request, 'Please enter Password!')
return redirect('register')
if password2 == '':
messages.warning(request, 'Please enter Confirm Password!')
return redirect('register')
if ('agree' not in request.POST):
messages.warning(request, 'Please agree to our terms and conditions!')
return redirect('register')
if password==password2:
if User.objects.filter(username=email):
messages.error(request,"Email Already Exists")
return redirect('register')
else:
user=User.objects.create_user(username=email,first_name=fname,last_name=lname,password=password) #these are postgres first_name,last_name
user.save()
messages.success(request,"Successfully Registered")
subject = 'Site Contact Form'
contact_message = "%s : %s via %s "%(fname, lname, email)
from_email = settings.EMAIL_HOST_USER
to_email = [from_email, 'myotheremail@gmail.com']
send_mail(subject, contact_message, from_email, to_email, fail_silently=False)
return redirect('login')
else:
messages.error(request,"Passwords don't match")
return redirect('register')
else:
return render(request, 'signupdiv.html')
settings.py
EMAIL_HOSTS = 'smtp.gmail.com'
EMAIL_HOST_USER = 'myemail@gmail.com'
EMAIL_HOST_PASSWORD = '*****'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
DEFAULT_FROM_EMAIL = EMAIL_HOST_USER
urls.py
from django.urls import path
from . import views
urlpatterns = [
path('login', views.login, name='login'),
path('register', views.register, name='register'),
path('logout', views.logout, name='logout'),
]
当我注册时,数据成功存储到数据库中(如预期的那样)。但它在代码的 view.py 中给出了 send_email(params) 错误。以下是错误:
ConnectionRefusedError at /account/register
[WinError 10061] No connection could be made because the target machine actively refused it
Request Method: POST
Request URL: http://127.0.0.1:8000/account/register
Django Version: 3.1.2
Exception Type: ConnectionRefusedError
Exception Value:
[WinError 10061] No connection could be made because the target machine actively refused it
Exception Location: C:\Users\User\AppData\Local\Programs\Python\Python37\lib\socket.py, line 716, in create_connection
Python Executable: C:\Users\User\Desktop\pfa\venv\Scripts\python.exe
Python Version: 3.7.5
Python Path:
['C:\\Users\\User\\Desktop\\pfa',
'C:\\Users\\User\\AppData\\Local\\Programs\\Python\\Python37\\python37.zip',
'C:\\Users\\User\\AppData\\Local\\Programs\\Python\\Python37\\DLLs',
'C:\\Users\\User\\AppData\\Local\\Programs\\Python\\Python37\\lib',
'C:\\Users\\User\\AppData\\Local\\Programs\\Python\\Python37',
'C:\\Users\\User\\Desktop\\pfa\\venv',
'C:\\Users\\User\\Desktop\\pfa\\venv\\lib\\site-packages']
Server time: Sun, 25 Oct 2020 23:17:08 +0000
【问题讨论】:
【参考方案1】:你错过了一点小事。
在settings.py
应该是EMAIL_HOST = 'smtp.gmail.com'
不是EMAIL_HOSTS = 'smtp.gmail.com'
【讨论】:
不敢相信这么愚蠢的错误让我哭了 3 个小时 XD 谢谢!!【参考方案2】:两件事:
一个,就像 Mohab 说的,应该是 EMAIL_HOST = 'smtp.gmail.com'
,而不是 EMAIL_HOSTS = 'smtp.gmail.com'
。修复它,它可能会起作用。
但如果这不起作用,则意味着 Google 正在阻止您的电子邮件地址被用于发送自动邮件。 You have to enable less secure apps in Gmail settings。不是最安全的选择,但没关系。注意:如果您长时间不发送自动电子邮件,Google 会自动关闭此设置。
如果这是问题所在,那么 Google 很可能会向您发送重要安全警报,其中包含与此答案中相同的说明。更多信息请查看this Stack Overflow post。
【讨论】:
以上是关于DJANGO EMAIL CONFIRMATION: [WinError 10061] 由于目标机器主动拒绝,无法建立连接的主要内容,如果未能解决你的问题,请参考以下文章
LeetCode(数据库)- Confirmation Rate
在更新面板中使用 bootstrap-confirmation