How to get all messages in Outlook using Microsoft Graph API?

Vladimir Sumilin 6 Reputation points


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:

I found a good video about how to connect to Microsoft Graph:

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  
                }).OrderBy("receivedDateTime desc").Top(1000).GetAsync();  
            var messagesCollectionRequest = new UserMessagesCollectionRequest(nextPageLink, _graphServiceClient, null);  
            pagedMessages = await messagesCollectionRequest.GetAsync();  
        return (Messages: pagedMessages, NextLink: GetNextLink(pagedMessages));  
Microsoft Graph Applications API
Microsoft Graph Applications API
A Microsoft API that enables you to manage these resources and actions related to applications in Azure Active Directory.
566 questions
Microsoft Graph Mail API
Microsoft Graph Mail API
A Microsoft API that supports accessing data in users' primary mailboxes and in shared mailboxes.
812 questions
0 comments No comments
{count} vote

2 answers

Sort by: Most helpful
  1. HarmeetSingh7172 4,786 Reputation points

    Hello @Vladimir Sumilin

    Thanks for reaching out.

    You can use List Messages Graph API to get all messages in your Outlook. Depending on the page size and mailbox data, getting messages from a mailbox can incur multiple requests. The default page size is 10 messages. You can use $top to customize the page size, within the range of 1 and 1000. You can improve the operation response time by using $select to specify the exact properties you need in response body.

    You should fine-tune the values for $select and $top, especially when you must use a larger page size, as returning a page with hundreds of messages each with a full response payload may trigger the gateway timeout (HTTP 504) and it may occur together with 503. Refer this document to know details.

    To get the next page of messages, simply apply the entire URL returned in @odata.nextLink to the next get-messages request. Following this, you can get all the messages.

    Please note that as a best practice, your application should always handle responses effectively, especially the responses which are paged in nature. Use the @odata.nextLink property to obtain the next paged set of results, until all pages of the result set have been read. The final page will not contain an @odata.nextLink property. You should include the entire URL in the @odata.nextLink property in your request for the next page of results, treating the entire URL as an opaque string.

    Hope this helps.

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

    1 person found this answer helpful.

  2. Zehui Yao_MSFT 4,001 Reputation points Microsoft Vendor

    Hi @Vladimir Sumilin , you can use endpoint /me/mailFolders/{id}/messages to get emails from other folders. Hope that can help. Best Wishes.



    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments