MailClient.SearchMail Method


Search emails in IMAP4 mail server.

[Visual Basic]
Public Function SearchMail( condition As String _
) As  MailInfo()
[C#]
public MailInfo [] SearchMail(
string condition
);
[C++]
public: MailInfo* __gc[] SearchMail(
String* condition );
[JScript]
public function SearchMail( condition : String 
) : MailInfo [];

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.

Example

[Visual Basic, C#, C++] The following example demonstrates how to receive email with EAGetMail POP3 & IMAP Component, 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]
Imports EAGetMail

Public Sub ReceiveMail( _
ByVal sServer As String, _
ByVal sUserName As String, _
ByVal sPassword As String, _
ByVal bSSLConnection As Boolean)
    Dim oClient As New MailClient("TryIt")

    Dim oServer As New MailServer(sServer, _
        sUserName, sPassword, bSSLConnection, _
        ServerAuthType.AuthLogin, ServerProtocol.Imap4)

    
    ' generates a log file
    ' oClient.LogFileName = "c:\imap4.log"
    
    Try
        oClient.Connect(oServer)
        Search the emails whose internal date is within 8-AUG-2008 and sender contains "John" 
        Dim infos() As MailInfo = oClient.SearchMail( "ALL ON 8-AUG-2008 FROM ""John""" )
        Dim count As Integer = infos.Length
        For i As Integer = 0 To count - 1
            Dim info As MailInfo = infos(i)
            
            Console.WriteLine( "UIDL: {0}", info.UIDL )
            Console.WriteLine( "Index: {0}", info.Index )
            Console.WriteLine( "Size: {0}", info.Size )
            Console.WriteLine( "Flags: {0}", info.IMAP4MailFlags )
            
            Dim oMail As Mail = oClient.GetMail(info)
            ''Save mail to local    file
            oMail.SaveAs(String.Format("c:\{0}.eml", i), True)
        Next

        For i As Integer = 0 To count - 1
            Dim info As MailInfo = infos(i)
            oClient.Delete(info)
        Next
        '
        ' Delete method just mark the email as deleted, 
        ' Quit method pure the emails from server exactly.
        oClient.Quit()

    Catch ep As MailServerException
        ''Message contains the information returned by mail server
        Console.WriteLine("Server Respond: {0}", ep.Message)
    Catch ep As System.Net.Sockets.SocketException
        Console.WriteLine("Socket Error: {0}", ep.Message)
    Catch ep As Exception
        Console.WriteLine("System Error: {0}", ep.Message)
    End Try
    
    ' show the conversation between server and client.
    Console.WriteLine( oClient.Conversation )
    oClient.Close()
End Sub

[C#]
using System;
using System.Collections;
using EAGetMail;

public void ReceiveMail( 
    string sServer, 
    string sUserName,
    string sPassword,
    bool bSSLConnection)
{
    MailClient oClient = new MailClient("TryIt");

    MailServer oServer  = new MailServer(sServer, 
        sUserName, sPassword, bSSLConnection, 
        ServerAuthType.AuthLogin, ServerProtocol.Imap4);

    
    // generates a log file
    // oClient.LogFileName = "c:\\imap4.log";
    
    try
    {
        oClient.Connect(oServer);
        Search the emails whose has no SEEN flag and sender contains "John" 
        MailInfo [] infos = oClient.SearchMail( "ALL NEW FROM \"John\"" );
       
        int count = infos.Length;
        for( int i = 0; i < count; i++ )
        {
            MailInfo info = infos[i];
            
            Console.WriteLine( "UIDL: {0}", info.UIDL );
            Console.WriteLine( "Index: {0}", info.Index );
            Console.WriteLine( "Size: {0}", info.Size );
            Console.WriteLine( "Flags: {0}", info.IMAP4MailFlags );
                        
            Mail oMail = oClient.GetMail(info);
            //Save mail to local file
            oMail.SaveAs(String.Format("c:\\{0}.eml", i), true);
        }

        for( int i = 0; i < count; i++ )
        {
            MailInfo info = infos[i];
            oClient.Delete(info);
        }
    
        // Delete method just mark the email as deleted, 
        // Quit method pure the emails from server exactly.
        oClient.Quit();
    }
    catch( MailServerException ep ) 
    {
        //Message contains the information returned by mail server
        Console.WriteLine("Server Respond: {0}", ep.Message);
    }
    catch( System.Net.Sockets.SocketException ep ) 
    {
        Console.WriteLine("Socket Error: {0}", ep.Message);
    }
    catch( Exception ep ) 
    {
        Console.WriteLine("System Error: {0}", ep.Message);
    }

    // show the conversation between server and client.
    Console.WriteLine( oClient.Conversation );
    oClient.Close();
}

[C++]
using namespace System;
using namespace System::Collections;
using namespace EAGetMail; 

Void ReceiveMail( 
        String *sServer, 
        String *sUserName,
        String *sPassword,
        bool bSSLConnection)
{
    MailClient *oClient = new MailClient(S"TryIt");

    MailServer *oServer  = new MailServer(sServer, 
        sUserName, sPassword, bSSLConnection, 
        ServerAuthType::AuthLogin, ServerProtocol::Imap4);
    
    // generates a log file
    // oClient-> = S"c:\\imap4.log";
    try
    {
        oClient->Connect(oServer);
        Search the emails whose has no SEEN flag and subject contains "test" 
        MailInfo *infos[] = oClient->SearchMail( S"ALL NEW SUBJECT \"test\"" );
               
        int count = infos->Length;
        for( int i = 0; i < count; i++ )
        {
            MailInfo *info = infos[i];

            Console::WriteLine( "UIDL: {0}", info->UIDL );
            Console::WriteLine( "Index: {0}", __box(info->Index));
            Console::WriteLine( "Size: {0}", __box(info->Size));
            //For POP3 server, the IMAP4MailFlags is meaningless.
            Console::WriteLine( "Flags: {0}", info->IMAP4MailFlags );
                        
            Mail *oMail = oClient->GetMail(info);
            //Save mail to local file
            oMail->SaveAs(String::Format(S"c:\\{0}.eml", __box(i)), true);
        }

        for( int i = 0; i < count; i++ )
        {
            MailInfo *info = infos[i];
            oClient->Delete(info);
        }
    
        // Delete method just mark the email as deleted, 
        // Quit method pure the emails from server exactly.
        oClient->Quit();
    }
    catch( MailServerException *ep ) 
    {
        //Message contains the information returned by mail server
        Console::WriteLine( S"Server Respond: {0}", ep->Message);
    }
    catch( System::Net::Sockets::SocketException *ep ) 
    {
        Console::WriteLine( S"Socket Error: {0}", ep->Message);
    }
    catch( Exception *ep ) 
    {
        Console::WriteLine( S"System Error: {0}", ep->Message);
    }
    
    // show the conversation between server and client.
    Console::WriteLine( oClient->Conversation );
    oClient->Close();
}