Send emails via Microsoft 365 using OAuth2 — no passwords, no Basic Auth. Follow this guide exactly to avoid common pitfalls.
My Mailer)From the App Registration overview page, copy and save:
Client IDTenant ID (same as Directory ID)SMTP.SendAsApp535 5.7.3 Authentication Unsuccessful.
| Location | Object ID type | Use? |
|---|---|---|
| App Registrations -> your app | App Registration Object ID | ❌ Wrong |
| Enterprise Applications -> your app | Enterprise Application Object ID | ✅ Correct |
Windows — Open PowerShell as Administrator:
Install-Module -Name ExchangeOnlineManagement
Linux / Mac:
# Ubuntu/Debian sudo apt-get install -y powershell # Fedora/RHEL sudo dnf install -y powershell # Any distro via snap sudo snap install powershell --classic pwsh # launch PowerShell
Connect to your tenant (browser popup — sign in as Global Admin):
Connect-ExchangeOnline -Organization YOUR_TENANT_ID
Register your app as an Exchange Service Principal:
New-ServicePrincipal -AppId YOUR_CLIENT_ID -ObjectId YOUR_ENTERPRISE_APPS_OBJECT_ID
Copy the ObjectId from the output — this is your Service Principal ID.
Grant mailbox access:
Add-MailboxPermission -Identity "yourmail@yourtenant.onmicrosoft.com" -User YOUR_SERVICE_PRINCIPAL_ID -AccessRights FullAccess
| Field | Value |
|---|---|
| From Email | yourmail@yourtenant.onmicrosoft.com |
| Client ID | From Step 2 |
| Client Secret | From Step 3 |
| Tenant ID | From Step 2 |
| Error | Cause | Fix |
|---|---|---|
535 5.7.3 Authentication Unsuccessful |
Exchange Service Principal not registered | Complete Part 2 fully |
535 5.7.3 even after Part 2 |
Wrong ObjectId used | Redo Step 5 using Enterprise Applications Object ID |
550 5.7.501 Spam abuse detected |
Trial tenant IP reputation issue | Use a paid M365 subscription or raise a Microsoft support ticket |
| Token fetch fails | Wrong OAuth2 scope | Scope must be https://outlook.office365.com/.default |
XOAUTH2 not supported |
Missing auth mechanism config | Set mail.smtp.auth.mechanisms=XOAUTH2 |
| Client secret not working | Secret expired or copied incorrectly | Generate a new secret and copy it immediately |
OAuth2 Client Credentials Flow — designed for server-to-server communication. No manual login required, no passwords over the wire, only short-lived tokens that auto-refresh silently.
smtp.office365.com:587 with STARTTLSXOAUTH2 — token, not password