Hi @Madhvi Bansal ,
Thanks for reaching out.
As you are using client credential flow to authenticate an IMAP connection using OAuth, then you need to add application permissions under Office 365 Exchange Online
Make sure to grant admin consent for all the application permissions.
Once consent has been provided, the admin must register your AAD application's service principal in Exchange using powerShell by following commands:
Install-Module -Name ExchangeOnlineManagement -allowprerelease Import-module ExchangeOnlineManagement Connect-ExchangeOnline -Organization
Register Service Principal in Exchange:
1.New-ServicePrincipal -AppId <APPLICATION_ID> -ServiceId <OBJECT_ID> [-Organization <ORGANIZATION_ID>]
Make sure to use ObjectId from enterprise applications rather than object id of application registration. For the same application you registered in Application Registration. A corresponding application has been created in Enterprise Application as well. You need to pass object id from there while registering service principal in Exchange: [
2.Get-ServicePrincipal | fl
3.Add-MailboxPermission -Identity "firstname.lastname@example.org" -User <SERVICE_PRINCIPAL_ID> -AccessRights FullAccess
In the application, you need to use scope = 'https://outlook.office365.com/.default'
Once you get the access token, you can create and open a connection to read mails.
Hope this will help.
Please remember to "Accept Answer" if answer helped you.