需要帮助通过具有自定义域的 Office 365 SMTP 发送电子邮件

Need Help Sending Email Through Office 365 SMTP with Custom Domain

提问人:Nawaraj 提问时间:10/17/2023 最后编辑:Eugene AstafievNawaraj 更新时间:10/18/2023 访问量:61

问:

我需要能够向托管在 office365 中的用户发送电子邮件。我的项目的电子邮件后端设置文件如下所示[email protected]

# Email settings
EMAIL_BACKEND = "django.core.mail.backends.smtp.EmailBackend"
EMAIL_HOST = "smtp.office365.com"
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_HOST_USER = "[email protected]"
EMAIL_HOST_PASSWORD = "App_password"

我目前面临的具体错误消息是 [Authentication unsuccessful, the request did not meet the criteria to be authenticated successfully. Contact your administrator.]

我已经验证了登录凭据,并尝试了两个端口和 ,但问题仍未解决。58725

**然而**

我能够使用电子邮件地址成功发送电子邮件smtp-mail.outlook.com@outlook.com

EMAIL_BACKEND = "django.core.mail.backends.smtp.EmailBackend"
EMAIL_HOST = "smtp-mail.outlook.com"
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_HOST_USER = "[email protected]"
EMAIL_HOST_PASSWORD = "App_password"

我认为这与它没有任何关系,而是发送电子邮件的功能。

def send_email_verification(user):
    site_url = "https://www.example.com"
    print("Trying to send email")

    if user.is_active:
        return
    else:
        print("sending email")
        confirmation_token = default_token_generator.make_token(user)  # unique token
        activation_link = f"{site_url}/users/api/users/activate/{user.id}/{confirmation_token}"   # activation link to be sent in email

        subject = "Verify your Email address"
        from_email = settings.EMAIL_HOST_USER
        to_email = user.email


        # Load the email template and render it with the activation link
        html_content = render_to_string('email_verification.html', {'activation_link' : activation_link})
        text_content = strip_tags(html_content)         # This strips the html, so people will have the text.


        # Create the email, message and send it.
        msg = EmailMultiAlternatives(subject, text_content, from_email, [to_email])
        msg.attach_alternative(html_content, "text/html")

        try:
            msg.send()
            print(f"Email successfully sent to {user.email}")
            print(f"confirmation toke = {confirmation_token}")
        except Exception as e:
            print(f"an error  has occured while sending email : {e}")

我将非常感谢那些以前可能遇到过类似问题的人的任何建议、见解或可能的解决方案。您的帮助将非常有价值。提前感谢您的支持!

Django 身份验证 电子邮件 SMTP Office365

评论


答:

-1赞 Dmitry Streblechenko 10/18/2023 #1

Office 365 不再允许基本身份验证。请参阅 https://learn.microsoft.com/en-us/exchange/clients-and-mobile-in-exchange-online/authenticated-client-smtp-submission