Madhukara Hiriadka
Madhukara Hiriadka

Reputation: 21

.Net Core C# IMAP integration for outlook

I have a requirement where I need to automate clients outlook inbox, where I need to read the emails and process it. Client belongs to different company and he has different outlook/office 365 subscription.

I was going through IAMP OAuth2 flow with C#. It speaks about adding Azure App Registrations, permissions etc.

My questions is, since my client is not using Azure and if i configure all app registrations/permissions in my azure instance, can I access his email inbox using IMAP OAuth2 flow using my Azure App or does client have to do something from his end?

Regards

Madhu

Upvotes: 0

Views: 102

Answers (1)

Rukmini
Rukmini

Reputation: 15444

I agree with @Max, it is possible to access another tenant’s Outlook email inbox by configuring a multi-tenant app in Azure, but your client must explicitly consent to granting your app access to their data.

  • When you register an app in Azure AD as multi-tenant, it means the app can be accessed by users from other Azure AD tenants, not just yours.
  • Your app registration should be configured as multi-tenant in your Azure AD tenant (not the client’s), so your client can authenticate and grant permissions to your app.
  • And grant the API permissions for accessing a user's Outlook mailbox that is IMAP.AccessAsUser.Alll or Mail.Read.

When your client tries to use the app, they will authenticate via Azure AD. During this process, they will be prompted to consent to the permissions your app is requesting.

You can either use Office 365 Exchange Online API permissions or Microsoft Graph API permissions like below:

enter image description here

And retrieve the messages:

var scopes = new[] { "https://graph.microsoft.com/.default" };
var tenantId = "common";
var clientId = "ClientID";

var options = new InteractiveBrowserCredentialOptions
{
    TenantId = tenantId,
    ClientId = clientId,
    AuthorityHost = AzureAuthorityHosts.AzurePublicCloud,
    RedirectUri = new Uri("http://localhost"),
};

var interactiveCredential = new InteractiveBrowserCredential(options);

var graphClient = new GraphServiceClient(interactiveCredential, scopes);

try
{
    var messages = await graphClient.Users["UserID"].Messages.GetAsync(); ;
    foreach (var message in messages.Value)
    {
        Console.WriteLine($"Subject: {message.Subject}");
    }
}

catch (ODataError odataError)
{
    Console.WriteLine(odataError.Error.Code);
    Console.WriteLine(odataError.Error.Message);
}

enter image description here

Upvotes: 0

Related Questions