In the world of business, effective synchronization between Outlook and Salesforce for managing emails, contacts, and calendar events is essential. Salesforce and Outlook working together sounds excellent. Imagine your sales personnel don’t have to go from one platform to the other to check for emails, send follow ups, or log customer interactions. It would be a breakthrough, wouldn’t it?
That is exactly what this Outlook-Salesforce Connection using the Graph API does! With Microsoft Graph API we can establish a secure connection between Outlook and Salesforce which enables pulling of emails, sending of emails, and even calendar synchronization without any manual effort needed. This post will guide you in integrating Outlook-Salesforce to fetch emails.
Salesforce can also be integrated within Outlook using the code approach feature. For a complete guide, see Set Up the Integration with Outlook.
Let us focus here on integrating Outlook into Salesforce using REST API. We will go through each step one by one.
- Register an App in Microsoft Azure
- Set Up Outlook API Permissions
- Configure Salesforce Named Credentials
- Create an Apex Class for OAuth Token Retrieval
- Fetch Outlook Emails Using REST API
- Test Outlook Integration
1. Register an App in Microsoft Azure
To integrate MS Outlook into Salesforce, we must create an app in Microsoft Azure.

- Go to the Microsoft Azure Portal.
- Navigate to Azure Active Directory (Microsoft Entra Id) > App Registrations.
- Click on New Registration and enter:
- Name: Any meaningful name.
- Supported account types: Choose Accounts in any organizational directory (Any Microsoft Entra ID tenant – Multitenant) and personal Microsoft accounts (e.g. Skype, Xbox)
- Redirect URI: Choose Web and put your Salesforce domain (e.g.,
https://your-salesforce-instance.com
) in web url.
- Enter Register to create app.
- After registration, open the created app.
- Navigate Client credentials -> Client Secrets
- Click on New Client Secret
- Description- Enter SFAppSecret
- Expires – Select 365 days (12 Months)
- This will generate a client secret.
- It will generate a Secret Value and Secret ID. Secret Value will be used in Salesforce Auth Provider.
2. Set Up Outlook API Permissions
Microsoft Graph API provides access to Microsoft cloud services. Custom or external applications can use this API to connect data to enhance organization efficiency. We can provide specific API access to applications using the API permission feature.

- In the API Permissions section, click Add a Permission.
- Select Microsoft Graph and choose:
- Mail.Read
- Mail.ReadWrite
- Mail.Send
- User.Read
- Mail.ReadBaic
- Mail.ReadBasicAll
- Click Grant Admin Consent.
3. Configure Salesforce Named Credentials
Configure Named Credential in Salesforce to integrate Outlook API. We will use client secrets created in Step 1. We will first create an a Auth Provider and then we will create named credential.
3.1 Create External Credential
External Credentials hold all information to connect external systems. Let us create external credentials to hold Azure credentials.

- Navigate to Salesforce Setup -> Named Credential-> External Credential
- Label – Microsoft EC
- Name – MicrosoftEC
- Authentication Protocol – Custom
- Save this External Credential
- Add Principals
- Parameter Name – Microsoft Principal
- Sequence Number – 1
- *Identity Type – Named Principal
- Authentication Parameters
- Parameter 1
- Name – ClientSecret
- Value – Put secret value from the Azure Client Secret feature.
- Parameter 2
- Name – ClientID
- Value – Put Application (client) ID from Azure App
- Parameter 1
- Create a Permission Set
- Add this External Credential to External Credential Principal Access
- Assign this permission to required user.
- For the demo, assign this to yourself
3.2 Create Named Credential
Named Credential helps us authenticate external API. It holds all authentication parameters to integrate external app/API.

- Navigate to Salesforce Setup -> Named Credential
- Click New
- Label – Microsoft NC
- Name – MicrosoftNC
- Url – Put Url https://login.microsoftonline.com/{tenantId}/oauth2/v2.0/token. You can get TenantId from Azure Portal.
- *External Credential – Microsoft EC
- Allow Formulas in HTTP Header – Checked
- Allow Formulas in HTTP Body – Checked
- Generate Authorization Header – Unchecked
- Click Save
4. Create Apex Class to get Authentication Token
Create an apex class to get a Microsoft Graph API access token. We have used external Credentials parameters to pass secure authentication parameters. You can check our other post Accessing External Credential Parameters of Named Credential in Salesforce Apex to retrieve parameter values from external credentials.
5. Fetch Outlook Emails Using REST API
Create an apex class OutlookEmailService to fetch all emails for a specific user. We need the azure portal user-id. It can be retrieved from the user page.
This class uses the above-created OutlookAuthService class to get a token. We are fetching emails and putting them into a custom Outlook message object. You can use messages based on your requirements.
6. Test Outlook Integration
We can test outlook integration by calling OutlookEmailService.getEmails. This will insert email information in the Outlook Message object. You can use your logic to handle email information.
Summary
Integrating Outlook with Salesforce using REST API allows seamless email synchronization, contact management, and event handling. We can use Microsoft Graph API to authenticate, fetch Outlook emails, and send messages programmatically.
References
- Accessing External Credential Parameters of Named Credential in Salesforce Apex
- Use the Microsoft Graph API
Related Posts
- Generic Apex class for Calling External System
- How to Correctly Publish Platform Event Using Salesforce Apex
- Best Practices to Avoid Hardcoding in Apex for Cleaner Salesforce Code
- Publish Platform Events from ASP.NET
- Uploading Files to Microsoft One Drive using Apex
- Automating data synchronization between Salesforce and Amazon Seller
- AWS Signature 4 Signing in Salesforce
- Download the S3 File in Salesforce using AWS Signature Version 4.0
- Use Named Credential to Upload File in S3