ComponentPro UltimateMail

      Making a trace log using events

      Language Filter: AllSend comments on this topic to ComponentPro

      When you want log commands sent to the server and the corresponding responses, the following events are very useful for diagnostic purposes:

      Event name Description
      CommandResponse Occurs when a command is sent to the server or a response is received from the server.
      StateChanged Occurs when the session state changes, such as from Disconnected to Connecting or from Downloading to Idle.
      Progress Occurs when a block of data is sent or received.

      The steps below show you how to handle the CommandResponse event to log commands sent to the server and responses:

      Using CommandResponse event to make a trace log

      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. Register handler to the CommandResponse event.
      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();
        
        // Register event handlers.
        client.CommandResponse += client_CommandResponse;
        
        // Connect to the POP3 server.
        client.Connect("server");
        
      5. Use your user name and password to login with Authenticate methods. The code looks similar to the following:
        // Authenticate.
        client.Authenticate("test", "test");
        
      6. 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());
        
      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;
      
      ...
      
      public void HandleCommandSentResponseReadEvents()
      {
          // Create a new instance. 
          Pop3 client = new Pop3();
      
          // Register event handlers. 
          client.CommandResponse += client_CommandResponse;
      
          // Connect to the POP3 server. 
          client.Connect("server");
      
          // Authenticate. 
          client.Authenticate("test", "test");
      
          // ... 
       
          // 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();
      }
      
      void client_CommandResponse(object sender, CommandResponseEventArgs e)
      {
          if (e.Command != null)
              Console.WriteLine("CMD>       " + e.Command);
          else 
              Console.WriteLine("RESPONSE>  " + e.Response);
      }