ComponentPro UltimateMail

      Getting Message List Synchronously

      Language Filter: AllSend comments on this topic to ComponentPro

      It is very easy to get a list of messages in a POP3 mailbox. To do that, simply call the ListMessages method. You can pass an optional flag parameter indicating which part of the message will be retrieved. The ListMessages method returns a collection of Pop3Message object containing information about messages you have requested.

      The following steps will help you to do that:

      Listing 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.Mail;
        
      2. Create a new instance of the Pop3 class.
      3. Now you can connect to the POP3 server with Connect methods. The code looks similar to the following:
        // Create a new instance of the Pop3 class.
        Pop3 client = new Pop3();
        
        // Connect to the server.
        client.Connect("myserver");
        
        // Or you can specify the POP3 port with 
        // client.Connect("myserver", 110);
        
      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");
        
      5. Now call the ListMessages method to retrieve a list of POP3 messages. The code looks similar to the following:
        // List messages. Only Size and UniqueId are retrieved.
        Pop3MessageCollection list = client.ListMessages(Pop3EnvelopeParts.Size | Pop3EnvelopeParts.UniqueId);
        
        foreach (Pop3Message m in list)
        {
            Console.WriteLine(string.Format("UniqueId: {0}, Size: {1}", m.UniqueId, m.Size));
        }
        
      6. After completing your work, call the Disconnect method to close the POP3 session.

      Final example code

      using System;
      using ComponentPro.Net.Mail;
      
      ...
      
      // Create a new instance of the Pop3 class.
      Pop3 client = new Pop3();
      
      // Connect to the server.
      client.Connect("myserver");
      
      // Or you can specify the POP3 port with 
      // client.Connect("myserver", 110); 
       
      // Login to the server.
      client.Authenticate("user", "password");
      
      // List messages. Only Size and UniqueId are retrieved.
      Pop3MessageCollection list = client.ListMessages(Pop3EnvelopeParts.Size | Pop3EnvelopeParts.UniqueId);
      
      foreach (Pop3Message m in list)
      {
          Console.WriteLine(string.Format("UniqueId: {0}, Size: {1}", m.UniqueId, m.Size));
      }
      
      // Close the connection.
      client.Disconnect();