Overview #
Starting from version Keycloak 26+, Keycloak supports OAuth2-based SMTP authentication. This configuration is essential when using mail providers that have deprecated basic authentication (e.g., Microsoft Entra ID / Gmail).
This document provides step-by-step instructions to configure Keycloak 26 for sending emails over SMTP using OAuth 2.0 in Office365.
✅ Prerequisites #
Before starting, ensure you have:
- A Keycloak 26+ instance deployed ).
- A valid OAuth2 client application configured on your email provider:
- For Microsoft: Entra ID → App Registrations.
🔧 Step-by-Step Configuration #
Create App with OAuth2 in Entra ID #
- Sign in to the Microsoft Entra ID.
- Click App registrations on the left menu.
- Click New registration.
- Register the Application with the following data:
- Name: Keycloak SMTP OAuth.
- Supported account types: My organization only.
- Click Register to finish.
- Create a Client Secret
- In the newly created app, go to Certificates & secrets.
- Under Client secrets, click + New client secret.
- Add a description like SMTP OAuth for Keycloak, choose expiry duration, and click Add.
- Copy the client secret value immediately (it won’t be shown again).
- Assign API Permissions
- Go to API Permissions → Click + Add a permission.
- Choose APIs my organization uses.
- Search for and select
Office 365 Exchange Online. - Click Application permissions.
- Expand the section and select the following:
IMAP.AccessAsAppMail.ReadMail.ReadWriteMail.SendMailbox.Settings.ReadWritePOP.AccessAsAppSMTP.SendAsApp
- Click Add Permissions.
- Back on the permissions page, click Grant admin consent.
🔒Store the client secret securely (e.g., Vault, Kubernetes Secret).
🧭 Configure SMTP with OAuth2 in Keycloak #
- Log in to the Keycloak Admin Console.
- Navigate to Realm Settings from the left-hand menu.
- Click the Email tab.
- Fill in the following fields in the Email settings page:
| Field | Value |
|---|---|
| Host | smtp.office365.com |
| Port | 587 |
| Encryption | StartTLS |
| Authentication | token |
| Username | support@reframe-data.io |
| Auth Token URL | https://login.microsoftonline.com/<tenant-id>/oauth2/v2.0/token |
| Auth Token Scope | https://outlook.office365.com/.default |
| Auth Token ClientId | <application-client-id> |
| Auth Token Client Secret | <application-client-secret-value> |
- Save the Email Settings.
- Click the Test Connection button and verify that test email is send.