Listen for Mail Change Notification Using Microsoft Graph API in C# ASP.NET Core

Sruthi Kurup 0 Reputation points
2024-01-03T11:09:59.5433333+00:00

How do I develop an ASP.NET Core application that listens to change notification upon receiving a new mail based on some condition? I've already created a sample app by referring to this YouTube tutorial: https://www.youtube.com/watch?v=fThiCZmIcMQ&list=PLWZJrkeLOrbbOve1DVVQsauZX2LN3IEHL&index=12.

However, I'm encountering two challenges:

  1. I need to get the user credentials (Client ID, Tenant ID, and Application Secret) through an API, then create a subscription for the particular user, and listen to the notification when a new mail arrives, and give the response back to the application user, who has the MS account linked with.
  2. The user will input the credentials on a different organization.

What I have done so far:

  1. Created an Office 365 account on trial.
  2. Registered an app in Azure Portal (entraid) with which I set up my supported accounts to Multitenant.
  3. Configured ngork and setup it to listen to my application port.
  4. Created an ASP.NET Core 7 application and updated the AppSettings with accurate admin credentials (Tenant ID, Client ID, Client Secret, ngork URL, etc.).

I also referred to this GitHub page, but a lot of breaking changes are there in the Graph API, so it gets difficult to update to my application: https://github.com/microsoftgraph/aspnetcore-webhooks-sample

Please help me to handle challenge 1 and 2 and resolve the issue.

Outlook | Windows | Classic Outlook for Windows | For business
Microsoft 365 and Office | Install, redeem, activate | For business | Windows
Microsoft Security | Microsoft Entra | Microsoft Entra ID
Microsoft Security | Microsoft Graph
{count} votes

1 answer

Sort by: Most helpful
  1. Sourabh Gupta 800 Reputation points Microsoft External Staff
    2024-01-06T11:53:37.4133333+00:00

    Hi Sruthi Kurup, Thanks for reaching out!

    Could you please try use the following subscription object while creating the new subscription.

    					DateTimeOffset expirationDateTime = DateTimeOffset.UtcNow.AddDays(2);
    
                        var Subscription = new Subscription
                        {
                            ChangeType = "created",
                            NotificationUrl = $"{notificationHost}/api/{endpoint where notification will come}/",
                            Resource = $"/users/{payload.UserId}//messages",
                            ExpirationDateTime = expirationDateTime,
                            ClientState = "my notification service",
                        };
                        var CreatedSubscription = await graphClient.Subscriptions.Request().AddAsync(Subscription);
    
    

    Also, you need to renew this subscription every 2 days based upon what is the expiry time. Feel free to reply again in case you need more specific help.

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment"


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.