ComponentPro UltimateMail

      Searching messages Synchronously

      Language Filter: AllSend comments on this topic to ComponentPro

      IMAP supports a search command that is quite powerful. Imap class encapsulated the command into an easy-to-use method called ListMessages which give you ability to search for messages matching given criteria.

      The following steps will help you to search messages that match a specified criteria:

      Searching messages

      1. Add using directives to your code to create aliases for existing namespaces and avoid having to type the fully qualified type names. The code looks similar to the following:
        using System;
        using ComponentPro.Net;
        using ComponentPro.Net.Mail;
        
      2. Create a new instance of the Imap class.
      3. Now you can connect to the IMAP server with Connect methods. The code looks similar to the following:
        // IMAP server information. 
        const string serverName = "myserver";
        const string user = "name@domain.com";
        const string password = "mytestpassword";
        const int port = 993;
        const SslSecurityMode securityMode = SslSecurityMode.Implicit;
        
        // Create a new instance of the Imap class.
        Imap client = new Imap();
        
        // Connect to the server.
        client.Connect(serverName, port, securityMode);
        
      4. Use your user name and password to login with Authenticate methods.  The code looks similar to the following:
        // Login to the server.
        client.Authenticate(user, password);
        
        // Select 'INBOX' mailbox.
        client.Select("INBOX");
        
      5. Now select a mailbox and pass appropriate parameters and search criteria to the ListMessages method. The code looks similar to the following:
        // Search for messages that have arrived since yesterday.
        ImapCriterion criteria = ImapCriterion.ArrivedBetween(DateTime.Now.AddDays(-1), DateTime.Now);
        
        // Search message that match the specified criteria. Only get message's unique Id and Size.
        ImapMessageCollection listMessages = client.ListMessages(ImapEnvelopeParts.UniqueId | ImapEnvelopeParts.Size, criteria);
        
        foreach (ImapMessage m in listMessages)
        {
            Console.WriteLine(string.Format("UniqueId: {0}, Size: {1}", m.UniqueId, m.Size));
        }
        
      6. After completing your work, call the Disconnect method to close the IMAP session. 

      Final example code

      using System;
      using ComponentPro.Net;
      using ComponentPro.Net.Mail;
      
      ...
      
      // IMAP server information. 
      const string serverName = "myserver";
      const string user = "name@domain.com";
      const string password = "mytestpassword";
      const int port = 993;
      const SslSecurityMode securityMode = SslSecurityMode.Implicit;
      
      // Create a new instance of the Imap class.
      Imap client = new Imap();
      
      // Connect to the server.
      client.Connect(serverName, port, securityMode);
      
      // Login to the server.
      client.Authenticate(user, password);
      
      // Select 'INBOX' mailbox.
      client.Select("INBOX");
      
      // Search for messages that have arrived since yesterday.
      ImapCriterion criteria = ImapCriterion.ArrivedBetween(DateTime.Now.AddDays(-1), DateTime.Now);
      
      // Search message that match the specified criteria. Only get message's unique Id and Size.
      ImapMessageCollection listMessages = client.ListMessages(ImapEnvelopeParts.UniqueId | ImapEnvelopeParts.Size, criteria);
      
      foreach (ImapMessage m in listMessages)
      {
          Console.WriteLine(string.Format("UniqueId: {0}, Size: {1}", m.UniqueId, m.Size));
      }
      
      // Close the connection.
      client.Disconnect();