/delphi-smtp-with-oauth2

Sending emails through Exchange Online using SMTP with OAuth2 can be a robust, secure alternative to traditional username/password authentication.

Primary LanguagePascalMozilla Public License 2.0MPL-2.0

GitHub issues GitHub forks GitHub top language GitHub stars

Secure Delphi SMTP With OAuth2

Requirements for FPCUnit test project

  • Lazarus IDE
  • Add required package for FPCUnit to the project
  • 64 Bit OpenSSL DLLs from Indy
  • Indy Sockets

Configuration

  • Add tenant id, client id and client secret in unit oauth2tokenprovider

const
TokenEndpoint = 'https://login.microsoftonline.com/{tenant}/oauth2/v2.0/token';
ClientID = '{client-id}';
ClientSecret = '{client-secret}';

  • Set recipient address and name in unit smtpoauth2client

AddressItem := IdMessage.Recipients.Add;
AddressItem.Address := 'recipient@example.com';
AddressItem.Name := 'Recipient';

  • Set authorized user and password (the auth token) in smtpoauth2client

UserPass := TIdUserPassProvider.Create(IdSMTP);
UserPass.Username := 'user@example.com';
UserPass.Password := OAuth2Token;