Reaven Smith
Reaven Smith

Reputation: 359

trying to get unread message count with Microsoft graph on multiple emails

I want to be able to get the number of unread emails for an specific name in my tenant.

Something like: https://graph.microsoft.com/v1.0/me/mailFolders/Inbox/messages?$filter=isRead ne false&$count=true]

but be able to change the me for an specific user, the above works in graph explorer for my signing user.

I tried this example here. Which bring me a list of users and was trying to modified it without success.

the app have api permission for microsoft graph to read mail in all mailboxes.

Upvotes: 0

Views: 9891

Answers (3)

OwenD
OwenD

Reputation: 21

I may be misunderstanding your question, but I think your trouble is you're replacing me in the URI with the user identifier. If that's correct, you need to replace it with user/useridentifier instead.

That would make the query:

https://graph.microsoft.com/v1.0/user/[email protected]/mailFolders/Inbox/messages?$filter=isRead ne false&$count=true

Upvotes: 1

Pallav Chanana
Pallav Chanana

Reputation: 667

Just pass UserID in GET API

{{UserId}} - username/mailid/principalName
    
 https://graph.microsoft.com/v1.0/users/{{UserId}}/mailFolders/Inbox/messages?$filter=isRead ne true&$count=true

Upvotes: 1

Jeremy Thake MSFT
Jeremy Thake MSFT

Reputation: 2138

To access a users mailbox other than the signed in user. You will need to call the api with an application (app-only) token. This will require requesting application permissions (not delegate permissions) for Mail.Read.

More information on getting access without a user context is available here https://learn.microsoft.com/en-us/graph/auth-v2-service?context=graph%2Fapi%2F1.0&view=graph-rest-1.0

The request would be GET https://graph.microsoft.com/v1.0/users/{id | userPrincipalName}/messages

You can use the id from calling GET https://graph.microsoft.com/v1.0/users

or use the upn which is their email address in most cases that you may already have.

More documentation is available here https://learn.microsoft.com/en-us/graph/api/user-list-messages?view=graph-rest-1.0&tabs=http

You can actually test these request calls in our Postman collections. Graph Explorer is purely for on-behalf of flow request calls. Setup instructions are here https://learn.microsoft.com/en-us/graph/use-postman

Upvotes: 2

Related Questions