ComponentPro UltimateMail

      Connecting through a proxy server

      Language Filter: AllSend comments on this topic to ComponentPro

      The Ultimate POP3 component fully supports many proxy servers (often referred to as "proxies"). If you need to connect to your POP3 server through a proxy, simply create a new instance of the WebProxyEx class, set the appropriate properties of the WebProxyEx object, assign it to the Proxy property of the Pop3 class and the necessary proxy communication will take place.

      Ultimate POP3 component supports the following Proxy servers:

      SOCKS4 SOCKS4 proxy.
      SOCKS4A SOCKS4A proxy (capable of resolving domain names).
      SOCKS5 SOCKS5 proxy.
      HTTP CONNECT (Basic and NTLM supported) HTTP proxy using CONNECT method.

      The following steps will help you to connect to an POP3 server through a proxy server:

      Connecting through a proxy server

      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;
        using ComponentPro.Net.Mail;
        
      2. Create a new instance of the Pop3 class.
      3. Create a new instance of the WebProxyEx class, set the appropriate properties of the WebProxyEx object and assign it to the Proxy property of the Pop3 class.
      4. Now you can connect to the POP3 server with Connect methods. The code looks similar to the following:
        // Create a new instance.
        Pop3 client = new Pop3();
        
        // Create a new proxy object.
        WebProxyEx proxy = new WebProxyEx();
        proxy.Server = "proxyserver"; // Set proxy address here.
        proxy.Port = 1080; // Set proxy port here.
        proxy.UserName = "username"; // Proxy user name.
        proxy.Password = "password"; // Password.
        
        client.Proxy = proxy;
        
        // Connect to the POP3 server.
        client.Connect("myserver");
        
      5. Use your user name and password to login with Authenticate methods. The code looks similar to the following:
        // Authenticate.
        client.Authenticate("userName", "password");
        
      6. Do your work like selecting a mailbox, displaying unique id of 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());
        
      7. After completing your work, call the Disconnect method to close the POP3 session.

      Final example code

      using System;
      using System.Text;
      using ComponentPro.Net;
      using ComponentPro.Net.Mail;
      
      ...
      
      // Create a new instance.
      Pop3 client = new Pop3();
      
      // Create a new proxy object.
      WebProxyEx proxy = new WebProxyEx();
      proxy.Server = "proxyserver"; // Set proxy address here.
      proxy.Port = 1080; // Set proxy port here.
      proxy.UserName = "username"; // Proxy user name.
      proxy.Password = "password"; // Password.
      
      client.Proxy = proxy;
      
      // Connect to the POP3 server.
      client.Connect("myserver");
      
      // Authenticate.
      client.Authenticate("userName", "password");
      
      // Do something here... 
      // ...
      
      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());
      
      // Disconnect.
      client.Disconnect();