Search Emails on Exchange Server
The following example shows how to use GemBox.Email to search for email messages on the Exchange Server that match various criteria using Advanced Query Syntax (AQS) in your C# and VB.NET applications.
using GemBox.Email;
using GemBox.Email.Exchange;
using System;
class Program
{
static void Main()
{
// If using the Professional version, put your serial key below.
ComponentInfo.SetLicense("FREE-LIMITED-KEY");
var exchangeClient = new ExchangeClient("<HOST> (e.g. https://outlook.office365.com/EWS/Exchange.asmx)");
exchangeClient.Authenticate("<USERNAME>", "<PASSWORD>");
// Search for messages in "Inbox" folder whose subject contains "Some subject" text.
var messageInfos = exchangeClient.SearchMessages("Inbox", "subject:\"Some subject\"");
Console.WriteLine("Number of messages with \"Some subject\" in subject:");
Console.WriteLine(messageInfos.Count);
// Search for messages in "Inbox" folder that were received this year.
messageInfos = exchangeClient.SearchMessages("Inbox", "received:\"this year\"");
Console.WriteLine("Number of messages received this year:");
Console.WriteLine(messageInfos.Count);
}
}
Imports GemBox.Email
Imports GemBox.Email.Exchange
Imports System
Module Program
Sub Main()
' If using the Professional version, put your serial key below.
ComponentInfo.SetLicense("FREE-LIMITED-KEY")
Dim exchangeClient As New ExchangeClient("<HOST> (e.g. https://outlook.office365.com/EWS/Exchange.asmx)")
exchangeClient.Authenticate("<USERNAME>", "<PASSWORD>")
' Search for messages in "Inbox" folder whose subject contains "Some subject" text.
Dim messageInfos = exchangeClient.SearchMessages("Inbox", "subject:""Some subject""")
Console.WriteLine("Number of messages with ""Some subject"" in subject:")
Console.WriteLine(messageInfos.Count)
' Search for messages in "Inbox" folder that were received this year.
messageInfos = exchangeClient.SearchMessages("Inbox", "received:""this year""")
Console.WriteLine("Number of messages received this year:")
Console.WriteLine(messageInfos.Count)
End Sub
End Module
To search for specific emails, you can use either the ExchangeClient.SearchMessages
or the ExchangeClient.SearchMessagesById
methods.