Search Emails on Microsoft 365 Server
The following example shows how to use GemBox.Email to search for email messages on the Microsoft 365 (Office 365) Server that match various criteria in your C# and VB.NET applications.
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>");
// Search for messages in "Inbox" folder whose subject contains "Some subject" text.
var messages = graphClient.SearchMessages("Inbox", "contains(subject, 'Some subject')");
Console.WriteLine("Number of messages with \"Some subject\" in subject:");
Console.WriteLine(messages.Count);
// Search for messages in "Inbox" folder that were received this year.
messages = graphClient.SearchMessages("Inbox", $"receivedDateTime ge {DateTime.Now.Year}-01-01");
Console.WriteLine("Number of messages received this year:");
Console.WriteLine(messages.Count);
}
}
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>")
' Search for messages in "Inbox" folder whose subject contains "Some subject" text.
Dim messages = graphClient.SearchMessages("Inbox", "contains(subject, 'Some subject')")
Console.WriteLine("Number of messages with ""Some subject"" in subject:")
Console.WriteLine(messages.Count)
' Search for messages in "Inbox" folder that were received this year.
messages = graphClient.SearchMessages("Inbox", $"receivedDateTime ge {Date.Now.Year}-01-01")
Console.WriteLine("Number of messages received this year:")
Console.WriteLine(messages.Count)
End Sub
End Module
To search for specific emails, you can use either the GraphClient.SearchMessages
or the GraphClient.SearchMessagesById
methods.