Send Mail Auth Error

Hello,

I am wanting my django project to send email when a form is submitted. I have created an account with a license in O365 and believe I have the settings and view code correct but I am getting the following error.

smtplib.SMTPAuthenticationError: (535, b’5.7.139 Authentication unsuccessful, the request did not meet the criteria to be authenticated successfully. Contact your administrator. [XXXXXXXXX.XXXXXXXX.XXXX.outlook.com]’)

It is not saying the credentials are incorrect so I am not sure what exactly it is looking for.

The relevant parts of settings and views are below

settings.py

...
AUTHENTICATION_BACKENDS = ('nilmon.auth.EmailBackend',)
# Email Configuration 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@domain.com'
EMAIL_HOST_PASSWORD = 'password'
DEFAULT_FROM_EMAIL = 'email@domain.com'

views.py

from django.conf import settings
from django.core.mail import send_mail

def email (request):
...
    subject = 'This is a subject'
    message = 'This is a cool message'
    email_from = settings.EMAIL_HOST_USER
    recipient_list = ['user@domain.com']
    send_mail(subject, message, email_from, recipient_list)
...

I have yet to find anything that talks about the criteria the needs to be met to authenticate. Am I on the right track here or should I be doing something else entirely?

Have you seen this Q&A? Looks like there are a few idea in there regarding Azure / Active Directory settings.

@CodenameTim That is perfect. The answer in the link provided “#4 Exclude the user from a conditional access policy that block legacy authentication” solved my problem.