使用django从gmail账户发送电子邮件

ev7lccsx  于 2023-03-20  发布在  Go
关注(0)|答案(1)|浏览(175)

我一直在尝试使用我的Gmail帐户发送电子邮件,但接收器没有收到任何东西,为什么会这样?
views.py

from django.shortcuts import render
    from django.core.mail import send_mail

    def index(request):
        if request.method == "POST":
            receiver = request.POST.get("receiver")

            from_email = "myemailaddress"
            recipient_list = [receiver]
            subject = "Just sending message"
            message = "If you are seeing this then this possibly worked"

            send_mail(subject, message, from_email, recipient_list, fail_silently=False)

        return render(request, 'index.html')

settings.py

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
    EMAIL_HOST = 'smtp.gmail.com'
    EMAIL_PORT = 587
    EMAIL_USE_TLS = True
    EMAIL_HOST_USER = 'myemailaddress'
    EMAIL_HOST_PASSWORD = 'myemailpassword'

我如何处理这个问题

2izufjch

2izufjch1#

我想可能是因为谷歌不允许非2fa连接,所以你需要使用应用密码(谷歌帐户-〉安全-〉进入应用密码--〉生成新的邮件密码)
使用此新密码作为连接到gmail的密码。

相关问题