如何解决DJANGO电子邮件确认:[WinError 10061]无法建立连接,因为目标计算机主动拒绝了它
我正在尝试发送电子邮件验证码以确认帐户。
现在,问题是我想先向自己发送一封电子邮件,然后再发送另一封电子邮件。我已经关闭了防病毒软件,所以这应该不是问题。除此之外,我无法弄清楚我们做错了什么,因为它没有通过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很可能会按照此答案中的说明向您发送了严重安全警报。进一步检查this Stack Overflow post。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。