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

Vladimir Sumilin 6 Reputation points
2022-11-09T14:46:15.29+00:00

2

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: https://stackoverflow.com/questions/64455925/get-all-email-message-using-microsoft-graph-api-in-c-sharp

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));  
    }  
Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
10,582 questions
0 comments No comments
{count} vote

6 answers

Sort by: Most helpful
  1. HarmeetSingh7172 4,811 Reputation points
    2022-11-09T22:56:01.167+00:00

    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 5,831 Reputation points
    2022-11-21T09:54:58.923+00:00

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

    262488-image.png

    262641-image.png


    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

  3. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

  4. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

  5. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more