Frederico Eglesias
Frederico Eglesias

Reputation: 55

Get all messages in Outlook using Microsoft Graph API

I have a question about Microsoft Graph API for C# code. Is it possible to get all messages which I have in Outlook?

I found a good article, but I'm still not getting an answer to my question. The article:

Get all email message using Microsoft Graph API in c#

(UPDATED) I found a good video about how to connect to Microsoft Graph: https://www.youtube.com/watch?v=acnFrkBL1kE&ab_channel=Microsoft365Developer

I have still the same question. I can get 1000 emails for call. Is there any way to get all messages from my Outlook? They should be from all my folders.

My new code for call 1000 messages:

public async Task<(IEnumerable<Message> Messages, string NextLink)> GetUserMessagesPage(
        string nextPageLink = null, int top = 1000)
    {
        IUserMessagesCollectionPage pagedMessages;

        if(nextPageLink == null)
        {
                pagedMessages = await _graphServiceClient.Me.Messages.Request().Select(msg => new
                {
                    msg.Subject,
                    msg.BodyPreview,
                    msg.ReceivedDateTime
                }).OrderBy("receivedDateTime desc").Top(1000).GetAsync();
        }
        else
        {
            var messagesCollectionRequest = new UserMessagesCollectionRequest(nextPageLink, _graphServiceClient, null);
            pagedMessages = await messagesCollectionRequest.GetAsync();
        }
        return (Messages: pagedMessages, NextLink: GetNextLink(pagedMessages));
    }

(UPDATED) I have tried also this:

pagedMessages = await _graphServiceClient.Users["[email protected]"].Messages.Request().Select(msg => new { msg.Subject}).Top(1000).GetAsync();

                messages.AddRange(pagedMessages.CurrentPage);
                while (pagedMessages.NextPageRequest != null)
                {
                    pagedMessages = await pagedMessages.NextPageRequest.GetAsync();
                    messages.AddRange(pagedMessages.CurrentPage);
                }

It was mentioned here: https://github.com/microsoftgraph/microsoft-graph-docs/blob/main/api-reference/beta/api/user-list-messages.md

Upvotes: 3

Views: 3160

Answers (1)

SpruceMoose
SpruceMoose

Reputation: 10320

From: Microsoft Graph REST API - Get message Documentation

There are two scenarios where an app can get a message in another user's mail folder:

  • If the app has application permissions, or,
  • If the app has the appropriate delegated permissions from one user, and another user has shared a mail folder with that user, or, has given delegated access to that user. See details and an example.

Permissions

One of the following permissions is required to call this API. To learn more, including how to choose permissions, see Permissions.

  1. Delegated (work or school account) - Permissions: Mail.ReadBasic, Mail.Read

  2. Delegated (personal Microsoft account) - Permissions: Mail.ReadBasic, Mail.Read

  3. Application - Permissions: Mail.ReadBasic.All, Mail.Read

Upvotes: 1

Related Questions