Graph console client

Retrieves a list of mail messages using MS Graph API and writes it to console.

This console sample connects to a Microsoft 365 (Exchange Online) mailbox using Microsoft Graph API and lists most recent messages in the Inbox folder.

Please note that before you can access Microsoft 365 mailboxes using this sample (or your own application), you have:

  • Register your application at Microsoft Azure Portal.
  • Retrieve a suitable OAuth 2.0 access token to authorize the Graph client connection.

The required steps are described in the following articles:


// obtain the access token (see the articles above for details)
string accessToken = GetMyAccessToken();

// connect to a mailbox at Microsoft 365 server and retrieve list of messages
using (GraphClient client = new GraphClient())
    // debug log (enable if needed)
    //client.LogWriter = new FileLogWriter("graph.log", LogLevel.Debug);

    // connect to the server ( is used by default)

    // authenticate using the OAuth 2.0 access token

    // get list of recent messages in the 'Inbox' folder
    Console.WriteLine("Listing folder contents...");
    GraphMessageCollection messageList = client.GetMessageList(
        new GraphPageView(0, MaxMessageCount));

    // write message list to the console
    foreach (GraphMessageInfo message in messageList)
        Console.WriteLine("{0:yyyy-MM-dd} {1}: {2}",
            message.ReceivedDate, message.From, message.Subject);