DJANGO EMAIL CONFIRMATION:[WinError 10061] 无法建立连接,因为目标机器主动拒绝它
DJANGO EMAIL CONFIRMATION: [WinError 10061] No connection could be made because the target machine actively refused it
我正在尝试发送电子邮件验证码以确认帐户。
现在,问题是我正在尝试先向自己发送一封电子邮件,然后再发送另一封电子邮件。我已经关闭了我的防病毒软件,所以这应该不是问题。除此之外,我想不出我做错了什么,它没有在 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
你漏掉了一件小事。
在 settings.py
应该是EMAIL_HOST = 'smtp.gmail.com'
不是EMAIL_HOSTS = 'smtp.gmail.com'
两件事:
一个,像Mohab说的,应该是EMAIL_HOST = 'smtp.gmail.com'
,而不是EMAIL_HOSTS = 'smtp.gmail.com'
。解决这个问题,它可能会起作用。
但如果这不起作用,则意味着 Google 阻止您的电子邮件地址被用于发送自动邮件。 You have to enable less secure apps in Gmail settings。这不是最安全的选择,但没关系。注意:如果您长时间不发送自动电子邮件,Google 将自动关闭此设置。
如果这是问题所在,那么 Google 很可能会向您发送严重安全警报,其中的说明与此答案中的说明相同。检查 了解更多。
我正在尝试发送电子邮件验证码以确认帐户。
现在,问题是我正在尝试先向自己发送一封电子邮件,然后再发送另一封电子邮件。我已经关闭了我的防病毒软件,所以这应该不是问题。除此之外,我想不出我做错了什么,它没有在 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
你漏掉了一件小事。
在 settings.py
应该是EMAIL_HOST = 'smtp.gmail.com'
不是EMAIL_HOSTS = 'smtp.gmail.com'
两件事:
一个,像Mohab说的,应该是EMAIL_HOST = 'smtp.gmail.com'
,而不是EMAIL_HOSTS = 'smtp.gmail.com'
。解决这个问题,它可能会起作用。
但如果这不起作用,则意味着 Google 阻止您的电子邮件地址被用于发送自动邮件。 You have to enable less secure apps in Gmail settings。这不是最安全的选择,但没关系。注意:如果您长时间不发送自动电子邮件,Google 将自动关闭此设置。
如果这是问题所在,那么 Google 很可能会向您发送严重安全警报,其中的说明与此答案中的说明相同。检查