Sending email from Django using a MS365 credentials

Image by Matthias Schild from Pixabay

Image by Matthias Schild from Pixabay

Using SMTP To send email from Django
Updated 3. May 2025

Sending emails using exchange SMTP credentials can be tricky when using MS365 user accounts. In this article, I will explain the steps needed to configure MS365, Entra ID, and Django to make it possible to send emails from your Django app.

Microsoft 365

Enable SMTP authentcation on user account

To authenticate to SMTP, the user account must enable SMTP authentication. This is done in the MS365 admin panel. Find the active user by navigating to MS 365 Admin Center > Users > Active users and clicking on the user you want to authenticate as.

Bilde / Image

Find the user account.

This will open a side panel. In this panel, select the Email tab, and then click on the "Manage Email Apps" link

Bilde / Image

Open the "Manage Email Apps" panel

Finally, you must check the "Authenticated SMTP" option.

Bilde / Image

Make sure that SMTP is enabled.

Microsoft Entra

Microsoft considers SMTP authentication to be insecure. So it will block any login attempts using it, despite having SMTP authentication enabled on the user account.

To authenticate using SMTP credentials, you, unfortunately, have to disable "security defaults" in Entra. This is done by navigating to Entra > Identity > Overview > Properties.

Then, at the bottom of the page, you can find the Mandage Security defaults link.

Bilde / Image

Where to find the option to disable Security Defaults

Django

When MS365 and Entra are configured, you can configure Django to send emails using your MS365 account.

First, add the needed information as fields in your .env file.

.env
...
EMAIL_HOST=smtp.office365.com
EMAIL_PORT=587
EMAIL_HOST_USER=noreply@haxor.no
EMAIL_HOST_PASSWORD="superDuperSecretPassword"
EMAIL_USE_TLS=True
...

Then, those settings need to be added to your project config.

yourproject/settings.py
...
# Email settings
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = os.getenv("EMAIL_HOST")
EMAIL_PORT = os.getenv("EMAIL_PORT")
EMAIL_HOST_USER = os.getenv("EMAIL_HOST_USER")
EMAIL_HOST_PASSWORD = os.getenv("EMAIL_HOST_PASSWORD")
EMAIL_USE_TLS = os.getenv("EMAIL_USE_TLS", 'False').lower() in ('true', '1', 't')
DEFAULT_FROM_EMAIL = os.getenv("EMAIL_HOST_USER")
...

Please note the DEFAULT_FROM_EMAIL. This does not need to be the same as your MS365 account name, but for the sake of simplicity, it can be set to this address.

To test your configuration, run the following command in your terminal.

python3 manage.py sendtestemail testmail@haxor.com

If you get no error messages, you have successfully sent your first email using SMTP and your MS365 account.