django 这些[Errno 111]连接被拒绝是什么样的错误?如何解决?

zysjyyx4  于 2023-03-09  发布在  Go
关注(0)|答案(2)|浏览(228)

我尝试使用django send_mail发送邮件,但当我尝试从本地发送邮件时,它可以正常工作,但生产时它显示错误**"[Errno 111] Connection refused”**

6jjcrrmo

6jjcrrmo1#

setting.py

 - EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
   EMAIL_HOST = 'smtp.gmail.com'
   EMAIL_PORT = 587 
   EMAIL_USE_TLS = True
   EMAIL_HOST_USER = "dudhpokharicommunityhomestay@gmail.com"
   EMAIL_HOST_PASSWORD = "............."

views.py

from django.conf import settings
from django.core.mail import send_mail
 - def Booking(request):
       if request.method =="POST":
           check_in_date = request.POST['check_in_date']
           check_out_date = request.POST['check_out_date']
           person = request.POST['persons']
           email = request.POST['email']
           phone_number  = request.POST['mobile']
           query_form_data = BookingInquery(check_in_date= check_in_date, 
                                           check_out_date =check_out_date,
                                           person =person,
                                           email_address =email,
                                           phone_number = phone_number
                                           )
           query_form_data.save()
           subject = "Booking Inquery Form"
           message = f"Inquery form detais check in date:"+ check_in_date + 
                    ",check out date:"+ check_out_date +", Email:" +email + ", 
                     phone number:" +phone_number + "total person:"+person 
           email_from = settings.EMAIL_HOST_USER
           recipient_list = [email_from]
           send_mail(subject, message, email_from, recipient_list)
   
   
           messages.success(request,"booking inquery send successfully..")
           return redirect('app:index')
wqnecbli

wqnecbli2#

好吧,问题是GMAIL已经停止支持安全性较低的应用程序,因此您无法像往常一样通过GMAIL SMTP发送邮件。一个解决方案是(如果您想使用Gmail SMTP)在您的Gmail帐户上有2FA,并创建一个“应用程序密码”,并在字段中使用该密码:EMAIL_HOST_PASSWORD
这样,连接将被接受,你可以通过Gmail的SMTP发送邮件。现在Gmail的SMTP服务器只是拒绝连接,因为他们删除了对“不太安全的应用程序”的支持。
您也可以根据您的方便程度移动到其他SMTP服务器

相关问题