CImapClient::SearchMailbox Method  
 
LONG SearchMailbox(
  LPCTSTR lpszCriteria,  
  LPCTSTR lpszCharacterSet,  
  UINT *lpnMessageIds,  
  LONG nMaxMessages  
);
LONG SearchMailbox(
  LPCTSTR lpszCriteria,  
  UINT *lpnMessageIds,  
  LONG nMaxMessages  
);

The SearchMailbox method searches the current mailbox for messages that match the specified criteria, returning matching message identifiers.

Parameters

lpszCriteria
A null-terminated string which specifies the search criteria.
lpszCharacterSet
A null-terminated string which specifies the character set to use when searching the mailbox. If this parameter is NULL or an empty string, the default US-ASCII character set will be used.
dwReserved
A reserved parameter which should be set to the value 0.
lpnMessageIds
A pointer to an array of unsigned integers that will contain the message identifiers of those messages which match the search criteria in the current mailbox. This parameter may be NULL, in which case the method will return the number of matching messages but will not return their identifiers.
nMaxMessages
The maximum number of message identifiers that may be stored in the lpnMessageIds array. If the lpnMessageIds parameter is NULL, this value should be zero.

Return Value

If the method succeeds, the return value is the number of messages that meet the search criteria in the current mailbox. If the method fails, the return value is IMAP_ERROR. To get extended error information, call GetLastError.

Remarks

The SearchMailbox method is used to search a mailbox for messages which match a given criteria and return a list of the matching message identifiers. The search criteria is composed of one or more search keywords and optional value to match against. String searches are not case sensitive and partial matches in the message are returned.

The following search keywords are recognized:

Keyword Description
ANSWERED Match those messages which have the IMAP_FLAG_ANSWERED flag set.
BCC address Match those messages which contain the specified address in the BCC header field.
BEFORE date Match those messages which were added to the mailbox prior to the specified date.
BODY string Match those messages where the body contains the specified string.
CC address Match those messages which contain the specified address in the CC header field.
DELETED Match those messages which have the IMAP_FLAG_DELETED flag set.
DRAFT Match those messages which have the IMAP_FLAG_DRAFT flag set.
FLAGGED Match those messages which have the IMAP_FLAG_URGENT flag set.
FROM address Match those messages which contain the specified address in the FROM header field.
HEADER field string Match those messages which contain the string in the specified header field. If no string is specified, then all messages which contain the header will be matched.
LARGER size Match those messages which are larger than the specified size in bytes.
NEW Match those messages which have the IMAP_FLAG_RECENT flag set, but not the IMAP_FLAG_SEEN flag.
OLD Match those messages which do not have the IMAP_FLAG_RECENT flag set.
ON date Match those messages which were added on the specified date.
RECENT Match those messages which have the IMAP_FLAG_RECENT flag set.
SEEN Match those messages which have the IMAP_FLAG_SEEN flag set.
SENTBEFORE date Match those messages whose Date header value is earlier than the specified date.
SENTON date Match those messages whose Date header value is the same as the specified date.
SENTSINCE date Match those messages whose Date header value is later than the specified date.
SINCE date Match those messages added to the mailbox after the specified date.
SMALLER size Match those messages which are smaller than the specified size in bytes.
SUBJECT string Match those messages whose Subject header contains the specified string.
TEXT string Match those messages whose headers or body contains the specified string.
TO address Match those messages which contain the specified address in the TO header field.
UID sequence Match those messages with unique identifiers in the sequence set.
UNANSWERED Match those messages which do not have the IMAP_FLAG_ANSWERED flag set.
UNDELETED Match those messages which do not have the IMAP_FLAG_DELETED flag set.
UNDRAFT Match those messages which do not have the IMAP_FLAG_DRAFT flag set.
UNFLAGGED Match those messages which do not have the IMAP_FLAG_URGENT flag set.
UNSEEN Match those messages which do not have the IMAP_FLAG_SEEN flag set.

In addition to the listed keywords, the keyword NOT may prefix a keyword to return those messages which do not match the search criteria. For example, "NOT TO user@domain.com" would return those messages which were not addressed to user@domain.com.

If multiple search keywords are specified, the result is the intersection of all those messages which meet the search criteria. For example, a search criteria of "DELETED SINCE 1-Jan-2003" would return all those messages which are marked for deletion and were added to the mailbox after 1 January 2003.

Those search keywords which expect dates must be specified in format dd-mmm-yyyy where the month is the three letter abbreviation for the month name. Note that the internal date the message was added to the mailbox is not the same as the value of the Date header field in the message.

The UID keyword expects a one or more unique message identifiers. These values may provided as comma separated list, or a range delimited by a colon. For example, "UID 23000:24000" would return all those messages who have UIDs ranging from 23000 through to 24000.

The message identifiers returned by this method are only valid until the mailbox is expunged or another mailbox is selected.

Requirements

Minimum Desktop Platform: Windows 7 Service Pack 1
Minimum Server Platform: Windows Server 2008 R2 Service Pack 1
Header File: cstools12.h
Import Library: csmapv12.lib
Unicode: Implemented as Unicode and ANSI versions

See Also