ComponentPro UltimateMail

      Connecting and authenticating to a POP3 server Synchronously

      Language Filter: AllSend comments on this topic to ComponentPro

      To authenticate to an POP3 server, you can follow the steps below

      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 System.Text;
        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 one of Authenticate methods. The code looks similar to the following:
        // Login to the server.
        client.Authenticate("user", "password");
        
      5. Do your work like listing messages, downloading mail messages, etc. The code looks similar to the following:
        StringBuilder sb = new StringBuilder();
        
        Pop3MessageCollection list = client.ListMessages(Pop3EnvelopeParts.Size | Pop3EnvelopeParts.UniqueId);
        for (int i = 0; i < list.Count; i++)
        {
            sb.AppendFormat("{0} - {1}\r\n", i + 1, list[i].UniqueId);
        }
        
        Console.WriteLine(sb.ToString());
        
      6. After completing your work, call the Disconnect method to close the POP3 session.

      Final example code

      using System;
      using System.Text;
      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");
      
      StringBuilder sb = new StringBuilder();
      
      Pop3MessageCollection list = client.ListMessages(Pop3EnvelopeParts.Size | Pop3EnvelopeParts.UniqueId);
      for (int i = 0; i < list.Count; i++)
      {
          sb.AppendFormat("{0} - {1}\r\n", i + 1, list[i].UniqueId);
      }
      
      Console.WriteLine(sb.ToString());
      
      // Close the connection.
      client.Disconnect();