MailClient.SearchMail Method


Search emails in IMAP4 mail server.
This method is obsoleted by SearchMailList

[Visual Basic 6.0]
Public Function SearchMail( condition As String _
) As  Variant
[Visual C++]
public: HRESULT SearchMail(
BSTR condition,
VARIANT* pVal
 );

Parameters

condition
Searching criteria.

Return Value

A MailInfo array presenting the emails.

Remarks

The SearchMail method searches the mailbox for messages that match the given searching criteria. Searching criteria consist of one or more search keys.

     The defined search keys are as follows.  Refer to the Formal
      Syntax section for the precise syntactic definitions of the
      arguments.

      <message set>  Messages with message sequence numbers
                     corresponding to the specified message sequence
                     number set

      ALL            All messages in the mailbox; the default initial
                     key for ANDing.

      ANSWERED       Messages with the \Answered flag set.

      BCC <string>   Messages that contain the specified string in the
                     envelope structure's BCC field.

      BEFORE <date>  Messages whose internal date is earlier than the
                     specified date.

      BODY <string>  Messages that contain the specified string in the
                     body of the message.

      CC <string>    Messages that contain the specified string in the
                     envelope structure's CC field.

      DELETED        Messages with the \Deleted flag set.

      DRAFT          Messages with the \Draft flag set.

      FLAGGED        Messages with the \Flagged flag set.

      FROM <string>  Messages that contain the specified string in the
                     envelope structure's FROM field.

      HEADER <field-name> <string>
                     Messages that have a header with the specified
                     field-name (as defined in [RFC-822]) and that
                     contains the specified string in the [RFC-822]
                     field-body.

      KEYWORD <flag> Messages with the specified keyword set.

      LARGER <n>     Messages with an [RFC-822] size larger than the
                     specified number of octets.

      NEW            Messages that have the \Recent flag set but not the
                     \Seen flag.  This is functionally equivalent to
                     "(RECENT UNSEEN)".

      NOT <search-key>
                     Messages that do not match the specified search
                     key.

      OLD            Messages that do not have the \Recent flag set.
                     This is functionally equivalent to "NOT RECENT" (as
                     opposed to "NOT NEW").

      ON <date>      Messages whose internal date is within the
                     specified date.

      OR <search-key1> <search-key2>
                     Messages that match either search key.

      RECENT         Messages that have the \Recent flag set.

      SEEN           Messages that have the \Seen flag set.

      SENTBEFORE <date>
                     Messages whose [RFC-822] Date: header is earlier
                     than the specified date.

      SENTON <date>  Messages whose [RFC-822] Date: header is within the
                     specified date.

      SENTSINCE <date>
                     Messages whose [RFC-822] Date: header is within or
                     later than the specified date.

      SINCE <date>   Messages whose internal date is within or later
                     than the specified date.

      SMALLER <n>    Messages with an [RFC-822] size smaller than the
                     specified number of octets.

      SUBJECT <string>
                     Messages that contain the specified string in the
                     envelope structure's SUBJECT field.

      TEXT <string>  Messages that contain the specified string in the
                     header or body of the message.

      TO <string>    Messages that contain the specified string in the
                     envelope structure's TO field.

      UID <message set>
                     Messages with unique identifiers corresponding to
                     the specified unique identifier set.

      UNANSWERED     Messages that do not have the \Answered flag set.

      UNDELETED      Messages that do not have the \Deleted flag set.

      UNDRAFT        Messages that do not have the \Draft flag set.

      UNFLAGGED      Messages that do not have the \Flagged flag set.

      UNKEYWORD <flag>
                     Messages that do not have the specified keyword
                     set.

      UNSEEN         Messages that do not have the \Seen flag set.

You can also use MailClient.GetMailInfosParam Property to set filters such as "retrieve new emails only" instead of SearchMail method.

Example

[Visual Basic 6.0, VBScript, Visual C++] The following example demonstrates how to search email with EAGetMail POP3 & IMAP ActiveX Object, but it doesn't demonstrates the events and mail parsing usage. To get the full samples of EAGetMail, please refer to Samples section.

[Visual Basic 6.0]
Public Sub ReceiveMail( _
ByVal sServer As String, _
ByVal sUserName As String, _
ByVal sPassword As String, _
ByVal bSSLConnection As Boolean)
    
    Const MailServerPop3 = 0
    Const MailServerImap4 = 1
 
    'For evaluation usage, please use "TryIt" as the license code, otherwise the
    '"invalid license code" exception will be thrown. However, the object will expire in 1-2 months, then
    '"trial version expired" exception will be thrown.
    Dim oClient As New EAGetMailObjLib.MailClient
    oClient.LicenseCode = "TryIt"
    
    'To receive email from imap4 server, please change
    'MailServerPop3 to MailServerImap4 in MailServer constructor
    Dim oServer As New EAGetMailObjLib.MailServer
    oServer.Server = sServer
    oServer.User = sUserName
    oServer.Password = sPassword
    oServer.SSLConnection = bSSLConnection
    oServer.Protocol = MailServerImap4
    
    If oServer.SSLConnection Then
        oServer.Port = 993 'SSL IMAP4
    Else
        oServer.Port = 143 'IMAP4 normal
    End If
    
On Error GoTo ErrorHandle
    oClient.Connect oServer
    
    Dim infos
    infos = oClient.SearchMail("ALL ON 8-AUG-2008 FROM ""John""")
    
    Dim i, Count
    Count = UBound(infos)
    For i = LBound(infos) To Count
        Dim info As EAGetMailObjLib.MailInfo
        Set info = infos(i)
        
        MsgBox "UIDL: " & info.UIDL
        MsgBox "Index: " & info.Index
        MsgBox "Size: " & info.Size
        'For POP3 server, the IMAP4MailFlags is meaningless.
        MsgBox "Flags: " & info.IMAP4Flags 

        Dim oMail As EAGetMailObjLib.Mail
        Set oMail = oClient.GetMail(info)
        'Save mail to local
        oMail.SaveAs "d:\tempfolder\" & i & ".eml", True
    Next

    For i = LBound(infos) To Count
        oClient.Delete (infos(i))
    Next
 
    '' Delete method just mark the email as deleted,
    ' Quit method expunge the emails from server permanently.
    oClient.Quit
    Exit Sub

ErrorHandle:
    ''Error handle
    MsgBox Err.Description
    
    oClient.Close
End Sub


[VBScript] Sub ReceiveMail( _ ByVal sServer, _ ByVal sUserName, _ ByVal sPassword, _ ByVal bSSLConnection) Const MailServerPop3 = 0 Const MailServerImap4 = 1 'For evaluation usage, please use "TryIt" as the license code, otherwise the '"invalid license code" exception will be thrown. However, the object will expire in 1-2 months, then '"trial version expired" exception will be thrown. Dim oClient Set oClient = CreateObject("EAGetMailObj.MailClient") oClient.LicenseCode = "TryIt" 'To receive email from imap4 server, please change 'MailServerPop3 to MailServerImap4 in MailServer constructor Dim oServer Set oServer = CreateObject("EAGetMailObj.MailServer") oServer.Server = sServer oServer.User = sUserName oServer.Password = sPassword oServer.SSLConnection = bSSLConnection oServer.Protocol = MailServerImap4 If oServer.SSLConnection Then oServer.Port = 993 'SSL IMAP4 Else oServer.Port = 143 'IMAP4 normal End If oClient.Connect oServer Dim infos infos = oClient.SearchMail("ALL NEW FROM ""John""") Dim i, Count Count = UBound(infos) For i = LBound(infos) To Count Dim info Set info = infos(i) MsgBox "UIDL: " & info.UIDL MsgBox "Index: " & info.Index MsgBox "Size: " & info.Size 'For POP3 server, the IMAP4MailFlags is meaningless. MsgBox "Flags: " & info.IMAP4Flags Dim oMail Set oMail = oClient.GetMail(info) 'Save mail to local oMail.SaveAs "d:\tempfolder\" & i & ".eml", True Next For i = LBound(infos) To Count oClient.Delete (infos(i)) Next '' Delete method just mark the email as deleted, ' Quit method expunge the emails from server permanently. oClient.Quit End Sub
[Visual C++] #include "stdafx.h" #include <windows.h> #include "eagetmailobj.tlh" using namespace EAGetMailObjLib; void ReceiveMail( LPCTSTR sServer, LPCTSTR sUserName, LPCTSTR sPassword, bool bSSLConnection) { ::CoInitialize(NULL); const int MailServerPop3 = 0; const int MailServerImap4 = 1; try { IMailClientPtr oClient; oClient.CreateInstance(__uuidof(EAGetMailObjLib::MailClient)); IMailServerPtr oServer; oServer.CreateInstance(__uuidof(EAGetMailObjLib::MailServer)); // For evaluation usage, please use "TryIt" as the license code, otherwise the // "invalid license code" exception will be thrown. However, the object will expire in 1-2 months, then // "trial version expired" exception will be thrown. oClient->LicenseCode = _T("TryIt"); oServer->Server = sServer; oServer->User = sUserName; oServer->Password = sPassword; INT nProtocol = MailServerImap4; if(bSSLConnection) { oServer->Port = 993; oServer->SSLConnection = VARIANT_TRUE; } else { oServer->Port = 143; } oClient->Connect(oServer); _variant_t arInfo = oClient->SearchMail(_T("ALL NEW SUBJECT \"test\"")); SAFEARRAY *psa = arInfo.parray; long LBound = 0, UBound = 0; SafeArrayGetLBound(psa, 1, &LBound); SafeArrayGetUBound(psa, 1, &UBound); INT count = UBound-LBound+1; for(long i = LBound; i <= UBound; i++) { _variant_t vtInfo; SafeArrayGetElement(psa, &i, &vtInfo); IMailInfoPtr pInfo; vtInfo.pdispVal->QueryInterface(__uuidof(IMailInfo), (void**)&pInfo); _tprintf(_T("UIDL: %s\r\n"), (TCHAR*)pInfo->UIDL); _tprintf(_T("Index: %d\r\n"), pInfo->Index); _tprintf(_T("Size: %d\r\n"), pInfo->Size); //For POP3 server, the IMAP4MailFlags is meaningless _tprintf(_T("Flags: %s\r\n"), (TCHAR*)pInfo->IMAP4Flags); IMailPtr oMail = oClient->GetMail(pInfo); TCHAR szFile[MAX_PATH+1]; memset(szFile, 0, sizeof(szFile)); ::wsprintf(szFile, _T("d:\\tempfolder\\%d.eml"), i); //save to local disk oMail->SaveAs(szFile, VARIANT_TRUE); oMail.Release(); } for(long i = LBound; i <= UBound; i++) { _variant_t vtInfo; SafeArrayGetElement(psa, &i, &vtInfo); IMailInfoPtr pInfo; vtInfo.pdispVal->QueryInterface(__uuidof(IMailInfo), (void**)&pInfo); //delete email from server oClient->Delete(pInfo); } // Delete method just mark the email as deleted, // Quit method expunge the emails from server permanently. arInfo.Clear(); oClient->Quit(); } catch(_com_error &ep) { _tprintf(_T("ERROR: %s\r\n"), (TCHAR*)ep.Description()); } ::CoUninitialize(); }

See Also

MailClient.GetMailInfos Method