ComponentPro UltimateMail

      Connecting and authenticating to a POP3 server Asynchronously

      Language Filter: AllSend comments on this topic to ComponentPro

      Use ConnectAsync and AuthenticateAsync to asynchronously connect and login to the POP3 server. These methods establish a connection and authenticate user to the server asynchronously with execution occurring on a new thread, therefore they allow your next line of code to execute immediately.

      In Task-based Asynchronous programs, the single asynchronous method represents the initiation and completion of an asynchronous operation. You may create the continuation code either explicitly, through methods on the Task class (for example, ContinueWith) or implicitly, by using language support built on top of continuations (for example, await in C#, Await in Visual Basic).

      In .NET 4.5 it is recommended to implement the Task-based Asynchronous Pattern.

      In Event-based Asynchronous programs, the event ConnectCompleted is raised when the ConnectAsync completes and the event AuthenticateCompleted is raised when the AuthenticateAsync completes. In the event handler method of the ConnectCompleted, you can check if there were any errors by using the Error property of the event data object.

      The following example demonstrates how to connect and authenticate to an POP3 server asynchronously:

      Task-based approach

      using System;
      using System.Text;
      using ComponentPro.Net.Mail;
      using ComponentPro;
      
      ...
      
      // Create a new instance.
      Pop3 client = new Pop3();
      
      // Connect to the POP3 server asynchronously. 
      await client.ConnectAsync("myserver");
      
      // Or you can specify the POP3 port with 
      // client.ConnectAsync("myserver", 110); 
       
      // Authenticate the user asynchronously. 
      await client.AuthenticateAsync("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 asynchronously. 
      await client.DisconnectAsync();
      
      // ...
      

      Event-based approach

      using System;
      using System.ComponentModel;
      using System.Text;
      using ComponentPro;
      using ComponentPro.Net.Mail;
      
      ...
      
      public void DoAsyncConnect()
      {
          // Create a new instance. 
          Pop3 client = new Pop3();
      
          client.ConnectCompleted += client_ConnectCompleted;
          client.AuthenticateCompleted += client_AuthenticateCompleted;
          client.DisconnectCompleted += client_DisconnectCompleted;
      
          // Connect to the POP3 server asynchronously. 
          client.ConnectAsync("myserver");
      
          // Or you can specify the POP3 port with 
          // client.ConnectAsync("myserver", 110); 
       
          // Your code here 
          // ...
      }
      
      void client_ConnectCompleted(object sender, ExtendedAsyncCompletedEventArgs<string> e)
      {
          Pop3 client = (Pop3)sender;
          if (e.Error != null)
          {
              Console.WriteLine("Error: " + e.Error.ToString());
              return;
          }
      
          // Authenticate the user asynchronously. 
          client.AuthenticateAsync("userName", "password");
      }
      
      void client_AuthenticateCompleted(object sender, AsyncCompletedEventArgs e)
      {
          Pop3 client = (Pop3)sender;
          if (e.Error != null)
          {
              Console.WriteLine("Error: " + e.Error.ToString());
              return;
          }
      
          // 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 asynchronously. 
          client.DisconnectAsync();
      }
      
      void client_DisconnectCompleted(object sender, ExtendedAsyncCompletedEventArgs<string> e)
      {
          if (e.Error != null)
              Console.WriteLine("Error: " + e.Error.ToString());
      }