A complete developer guide to Microsoft 365 email integration. Learn how to set up Azure App Registration, Microsoft Graph API, OAuth authentication, IMAP, SMTP, Client Secrets, Tenant IDs, and Exchange Online step by step from scratch.

Email integrations are one of the most common requirements in modern SaaS applications. Whether you're building a CRM, support system, email client, marketing platform, or automation tool, you'll eventually need to connect Microsoft 365 email accounts.
Unfortunately, Microsoft's ecosystem consists of several moving parts:
For first-time developers, understanding how these services fit together can be overwhelming.
In this guide, I'll walk through the complete setup process and explain exactly where each key, secret, ID, and configuration value comes from.
Before starting, it's important to understand the relationship between Microsoft's services.
Microsoft 365
Your application never talks directly to Outlook.
Instead:
Application
↓
OAuth 2.0
↓
Microsoft Entra ID
↓
Microsoft Graph API
↓
Exchange Online Mailbox
For development and testing purposes, Microsoft 365 Business Basic is usually sufficient.
Benefits include:
After signup, Microsoft automatically creates a tenant domain such as:
yourcompany.onmicrosoft.com
and an administrator account:
Before touching Azure, verify the mailbox works.
Login:
Use:
Send and receive a test email.
If Outlook works, Exchange Online is properly configured.
Open:
Microsoft Entra Admin Center
Navigate:
Applications
→ App Registrations
→ New Registration
Application Name:
Mail Prototype
Supported Account Types:
Single Tenant
Redirect URI:
Web
http://localhost:3000/auth/callback
Click Register.
After registration, Azure provides:
Location:
App Registration
→ Overview
→ Application (client) ID
Example:
AZURE_CLIENT_ID=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
Location:
App Registration
→ Overview
→ Directory (tenant) ID
Example:
AZURE_TENANT_ID=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
Navigate:
Certificates & Secrets
→ New Client Secret
Provide:
Click Add.
Copy:
Value
Important:
Do NOT copy Secret ID.
Use only:
AZURE_CLIENT_SECRET=xxxxxxxxxxxxxxxxxxxx
Navigate:
API Permissions
→ Add Permission
→ Microsoft Graph
→ Delegated Permissions
Add:
openid
profile
offline_access
User.Read
Mail.Read
Mail.ReadWrite
Mail.Send
Grant Admin Consent.
Example:
AZURE_CLIENT_ID=
AZURE_CLIENT_SECRET=
AZURE_TENANT_ID=
AZURE_REDIRECT_URI=http://localhost:3000/auth/callback
Microsoft 365 IMAP Server:
Host: outlook.office365.com
Port: 993
Security: SSL/TLS
Example Node.js configuration:
{
host: 'outlook.office365.com',
port: 993,
tls: true
}
Microsoft 365 SMTP Server:
Host: smtp.office365.com
Port: 587
Security: STARTTLS
Example:
{
host: 'smtp.office365.com',
port: 587,
secure: false
}
Create login endpoint:
GET /auth/connect
Redirect user to Microsoft Login.
After authorization:
GET /auth/callback
Exchange authorization code for:
Profile Endpoint:
GET https://graph.microsoft.com/v1.0/me
Read Messages:
GET https://graph.microsoft.com/v1.0/me/messages
Send Email:
POST https://graph.microsoft.com/v1.0/me/sendMail
Cause:
AADSTS50011
Solution:
Ensure Azure Redirect URI exactly matches application configuration.
Cause:
Incorrect tenant or account type configuration.
Solution:
Verify supported account types and tenant configuration.
Cause:
Admin consent not granted.
Solution:
Grant admin consent after adding Graph permissions.
Microsoft's ecosystem can seem complicated initially, but once you understand the relationship between Microsoft 365, Exchange Online, Entra ID, OAuth, and Microsoft Graph, the setup becomes straightforward.
By following this guide, you'll have a fully functional email integration capable of reading emails, sending messages, synchronizing folders, and building modern SaaS applications powered by Microsoft 365.
Get the latest articles, tutorials, and updates delivered straight to your inbox. No spam, unsubscribe at any time.