我需要能够从托管在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.
]
我已验证登录凭据,并尝试了
587
和25
两个端口,但问题仍未解决。
我能够使用
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}")
我非常感谢以前遇到过类似问题的人提供的任何建议、见解或可能的解决方案。您的帮助将非常有价值。预先感谢您的支持!