https://blog.habarisoft.com/2025/08/secure-delphi-smtp-with-oauth2/Sending emails through Exchange Online using OAuth2 can be a robust, secure alternative to traditional username/password authentication. This post outlines how to achieve this in Object Pascal using the Indy components TIdHTTP and TIdSMTP, broken down into three practical steps.
- Configure Exchange Online and Register an Entra ID App
- Acquire an OAuth2 Access Token Using Indy TIdHTTP
- Send Email Using Indy TIdSMTP with OAuth2 Access Token
Example FPCUnit project on GitHub.
Disclaimer: I have not yet been able to verify the Exchange Online configuration steps today, as I currently do not have access to an Exchange Online license.
Update - 2025-08-13* An updated version with two fixes is now available on GitHub.
* Sending of emails using Indy TIdSMTP with the retrieved OAuth2 access token has been tested with success with the provided code.