List and manipulate messages on Microsoft 365 Server

The following example shows how you can use GemBox.Email to list the first 10 messages from an Microsoft 365 (Office 365) server, print the body of unread messages, and mark them as read.

using GemBox.Email;
using GemBox.Email.Graph;
using System;

class Program
{
    static void Main()
    {
        // If using the Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY");

        // Create a new Graph client.
        var graphClient = new GraphClient();
        graphClient.Authenticate("<OAUTH2.0-TOKEN>");
        graphClient.ImpersonateUser("<USER-ID-OR-EMAIL>");

        // List the first ten messages in the Inbox folder.
        var messages = graphClient.ListMessages("Inbox", 0, 10);

        foreach (var message in messages)
        {
            if (!message.IsRead)
            {
                // Get the full mail message.
                var mailMessage = graphClient.GetMessage(message.MessageId);
                Console.WriteLine("From: " + string.Join(", ", mailMessage.From));
                Console.WriteLine(mailMessage.BodyHtml ?? mailMessage.BodyText);
                Console.WriteLine();

                // Mark message as read.
                graphClient.MarkMessageAsRead(message.MessageId);
            }
        }
    }
}
Imports GemBox.Email
Imports GemBox.Email.Graph
Imports System

Module Program

    Sub Main()

        ' If using the Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY")

        ' Create a new Graph client.
        Dim graphClient = New GraphClient()
        graphClient.Authenticate("<OAUTH2.0-TOKEN>")
        graphClient.ImpersonateUser("<USER-ID-OR-EMAIL>")

        ' List the first ten messages in the Inbox folder.
        Dim messages = graphClient.ListMessages("Inbox", 0, 10)

        For Each message In messages
            If Not message.IsRead Then
                ' Get the full mail message.
                Dim mailMessage = graphClient.GetMessage(message.MessageId)
                Console.WriteLine("From: " & String.Join(", ", mailMessage.From))
                Console.WriteLine(If(mailMessage.BodyHtml, mailMessage.BodyText))
                Console.WriteLine()

                ' Mark message as read.
                graphClient.MarkMessageAsRead(message.MessageId)
            End If
        Next

    End Sub
End Module
Mail messages listed with GemBox.Email
Screenshot of mail messages listed with GemBox.Email

You can use the GraphClient.ListMessages method to list messages and the GraphClient.GetMessage method to obtain more information about a given message.

You can manipulate existing messages by using one of the following methods:

See also


Next steps

GemBox.Email is a .NET component that enables you to read, write, receive, and send emails from your .NET applications using one simple API.

Download Buy