Searches the specified mailbox for messages that match the given search criteria.

Namespace: S22.Imap
Assembly: S22.Imap (in S22.Imap.dll) Version: 3.6.0.0 (3.6.0.0)

Syntax

C#
IEnumerable<uint> Search(
	SearchCondition criteria,
	string mailbox = null
)

Parameters

criteria
Type: S22.Imap..::..SearchCondition
A search criteria expression; Only messages that match this expression will be included in the result set returned by this method.
mailbox (Optional)
Type: System..::..String
The mailbox that will be searched. If this parameter is omitted, the value of the DefaultMailbox property is used to determine the mailbox to operate on.

Return Value

An enumerable collection of unique identifiers (UIDs) which can be used with the GetMessage family of methods to download the mail messages.

Remarks

A unique identifier (UID) is a 32-bit value assigned to each message which uniquely identifies the message within the respective mailbox. No two messages in a mailbox share the same UID.

Examples

This example demonstrates how to use the search method to get a list of all unread messages in the mailbox.
CopyC#
ImapClient Client = new ImapClient("imap.gmail.com", 993, "My_Username",
    "My_Password", true, AuthMethod.Login);

// Get a list of unique identifiers (UIDs) of all unread messages in the mailbox.
IEnumerable<uint> uids = Client.Search( SearchCondition.Unseen() );

// Fetch the messages and print out their subject lines.
foreach(uint uid in uids) {
    MailMessage message = Client.GetMessage(uid);

    Console.WriteLine(message.Subject);
}

// Free up any resources associated with this instance.
Client.Dispose();

Examples

This example demonstrates how to perform a search using multiple search criteria.
CopyC#
ImapClient Client = new ImapClient("imap.gmail.com", 993, "My_Username",
    "My_Password", true, AuthMethod.Login);

// Get a list of unique identifiers (UIDs) of all messages sent before the 01.04.2012
// and that are larger than 1 Kilobyte.
IEnumerable<uint> uids = Client.Search( SearchCondition.SentBefore(new DateTime(2012, 4, 1))
    .And( SearchCondition.Larger(1024) ));

Console.WriteLine("Found " + uids.Count() + " messages");

// Free up any resources associated with this instance.
Client.Dispose();

Examples

The following example demonstrates how to perform a search using characters outside the ASCII range.
CopyC#
ImapClient Client = new ImapClient("imap.gmail.com", 993, "My_Username",
    "My_Password", true, AuthMethod.Login);

// Get a list of unique identifiers (UIDs) of all messages that have
// the Japanese expression "フーリエ変換" in their subject lines.
try {
    IEnumerable<uint> uids = Client.Search(SearchCondition.Subject("フーリエ変換"));

    Console.WriteLine("Found " + uids.Count() + " messages");
} catch(NotSupportedException e) {
    // If this exception is thrown, the server does not support searching for characters
    // outside the ASCII range.
    Console.WriteLine("The server does not support searching for non-ASCII values.");
}

// Free up any resources associated with this instance.
Client.Dispose();

Exceptions

ExceptionCondition
System..::..ArgumentNullExceptionThe criteria parameter is null.
S22.Imap..::..BadServerResponseExceptionThe search could not be completed. The message property of the exception contains the error message returned by the server.
System..::..ObjectDisposedExceptionThe ImapClient object has been disposed.
System.IO..::..IOExceptionThere was a failure writing to or reading from the network.
S22.Imap..::..NotAuthenticatedExceptionThe method was called in non-authenticated state, i.e. before logging in.
System..::..NotSupportedExceptionThe search values contain characters beyond the ASCII range and the server does not support handling non-ASCII strings.

See Also