PowerTCP Mail for .NET
Search Method
Example 




An array of one or more ImapSearchParameter objects, defining the type of search to perform.
Searches for messages within a mailbox matching the specified criteria.
Syntax
Public Function Search( _
   ByVal criteria() As ImapSearchParameter _
) As ImapMessage()
Dim instance As Mailbox
Dim criteria() As ImapSearchParameter
Dim value() As ImapMessage
 
value = instance.Search(criteria)
public ImapMessage[] Search( 
   ImapSearchParameter[] criteria
)
public: ImapMessage*[]* Search( 
   ImapSearchParameter*[]* criteria
) 

Parameters

criteria
An array of one or more ImapSearchParameter objects, defining the type of search to perform.

Return Value

An array of ImapMessage objects which meet the criteria.
Exceptions
ExceptionDescription
ProtocolExceptionBad IMAP protocol response received from server.
System.Net.Sockets.SocketExceptionA communications failure has occurred.
System.InvalidCastExceptionSpecified cast is invalid.
Remarks

This method sends the "SEARCH" command to the server. Multiple ImapSearchParameters are combined with a logical AND. Other logical combinations can be represented using the ImapCriterion values ImapCriterion.Not and ImapCriterion.Or. See RFC 3501 or other on-line resources for guidance on how to use this powerful search tool.

If Imap.Session.AutoUtf8 is set to true, and the server advertises UTF8=ACCEPT, UTF8 will be used for encoding and decoding UNICODE strings.

Example
In this example, the Imap component copies messages that match a specified criteria to another mailbox.
private void getMessages(object state)
{
    //Configure server and account info
    imap1.Session.RemoteEndPoint = new Dart.Mail.IPEndPoint(myServer, Imap.GetDefaultPort(imap1.Session));
    imap1.Session.Username = myUsername;
    imap1.Session.Password = myPassword;

    //Connect and log into the account
    imap1.Connect();
    imap1.Authenticate();

    //Set the selected mailbox to the Inbox
    imap1.SelectedMailbox = imap1.Mailboxes["INBOX"];

    // Construct the search "SEARCH SINCE 1-Jan-2012 NOT FROM JACKSON"
    ImapSearchParameter[] criteria = new ImapSearchParameter[] 
    {
        new ImapSearchParameter(ImapCriterion.Since, "1-Jan-2012"),
        new ImapSearchParameter(ImapCriterion.Not, ""),
        new ImapSearchParameter(ImapCriterion.From, "JACKSON")
    };

    // Perform the search
    ImapMessage[] messages = imap1.SelectedMailbox.Search(criteria);

    //Copy all messages that meet the criteria to the 'Save' mailbox
    foreach (ImapMessage imapMessage in messages)
        imapMessage.CopyTo(imap1.Mailboxes["Save"]);

    //Gracefully logout
    imap1.Close();
}
Private Sub getMessages(ByVal state As Object)
    'Configure server and account info
    imap1.Session.RemoteEndPoint = New Dart.Mail.IPEndPoint(myServer, Imap.GetDefaultPort(imap1.Session))
    imap1.Session.Username = myUsername
    imap1.Session.Password = myPassword

    'Connect and log into the account
    imap1.Connect()
    imap1.Authenticate()

    'Set the selected mailbox to the Inbox
    imap1.SelectedMailbox = imap1.Mailboxes("INBOX")

    ' Construct the search "SEARCH SINCE 1-Jan-2012 NOT FROM JACKSON"
    Dim criteria() As ImapSearchParameter = { New ImapSearchParameter(ImapCriterion.Since, "1-Jan-2012"), New ImapSearchParameter(ImapCriterion.Not, ""), New ImapSearchParameter(ImapCriterion.From, "JACKSON") }

    ' Perform the search
    Dim messages() As ImapMessage = imap1.SelectedMailbox.Search(criteria)

    'Copy all messages that meet the criteria to the 'Save' mailbox
    For Each imapMessage As ImapMessage In messages
        imapMessage.CopyTo(imap1.Mailboxes("Save"))
    Next imapMessage

    'Gracefully logout
    imap1.Close()
End Sub
See Also

Reference

Mailbox Class
Mailbox Members


PowerTCP Mail for .NET Documentation Version 4.3
© 2018 Dart Communications. All Rights Reserved.
Send comments on this topic