Search Method


Searches the currently selected mailbox for messages which meet specified condition.

Message numbers (or UIDs) are returned as array of numbers. Lower bound of the array is 1.

By default, this method just returns message numbers or UIDs for all messages in the mailbox. To search for specific items, specify non-empty search Condition.

Condition is a set of search criteria in IMAP4 format. Some popular search keys are listed below:

Full list of allowed keys is described in RFC 3501 (INTERNET MESSAGE ACCESS PROTOCOL - VERSION 4rev1).

When multiple keys are specified, the result is the intersection (AND function) of all the messages that match those keys. For example, the criteria DELETED FROM "SMITH" SINCE 1-Feb-1994 refers to all deleted messages from Smith that were placed in the mailbox since February 1, 1994. A search key can also be a parenthesized list of one or more search keys (e.g., for use with the OR and NOT keys).

In all search keys that use strings, a message matches the key if the string is a substring of the field. The matching is case-insensitive.


arrIndices = ObjectName.Search(AsUID, [Condition], [Charset])  
Parameters:  
AsUID As Boolean If True, returned array will be array of UIDs (unique-IDs). Otherwise, message numbers will be returned  
Condition As String (optional) Search condition. Default is "ALL" (all messages in the currently selected mailbox returned)  
Charset as String (optional) Charset of the string that appear in search criteria. If not specified, default (US-ASCII) charset is assumed  
Return value As Variant Array of Variant/Long, each value is UID or message number of the message. If error has occurred, return value is Empty  

Usage example:

' This sample searches Inbox and retrieves recent messages only
Dim Mailer, SearchResults, Message, I
'Using visual basic to create object
Set Mailer = CreateObject("MailBee.IMAP4")
'Using ASP to create object
'Set Mailer = Server.CreateObject("MailBee.IMAP4")
'In ASP use Response.Write instead of MsgBox
Mailer.EnableLogging = True
Mailer.LogFilePath = "C:\imap4_log.txt"
Mailer.ClearLog
Mailer.LicenseKey = "put your license key here"
If Mailer.Connect("mailserver.com", 143, "MyName", "MyPassword") Then
  If Mailer.SelectMailbox("Inbox") Then
    SearchResults = Mailer.Search(False, "RECENT") ' Get list of message numbers of recent messages
    If Not IsEmpty(SearchResults) Then
      For I = LBound(SearchResults) To UBound(SearchResults)
        Set Message = Mailer.RetrieveSingleMessage(SearchResults(I), False)
        If Not Message Is Nothing Then
          MsgBox Message.BodyText
        End If
      Next
    End If
  End If
  Mailer.Disconnect
End If

See Also:

Envelope.UID Property

RetrieveEnvelopes Method
RetrieveEnvelopesEx Method
RetrieveSingleMessage Method


Copyright 2002-2010, AfterLogic Corporation. All rights reserved.