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 ComponentPro.Net;
        using ComponentPro.Net.Mail;
        
      2. Create a new instance of the Smtp class.
      3. Register handler to the CommandResponse event.
      4. Now you can connect to the SMTP server with Connect methods. The code looks similar to the following:
        // Create a new instance.
        Smtp client = new Smtp();
        
        // Register event handlers.
        client.CommandResponse += client_CommandResponse;
        
        // Connect to the SMTP 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. Now do your tasks such as creating a new mail message and send it, etc. The code looks similar to the following:
        // Create a new mail message.
        MailMessage msg = new MailMessage();
        msg.Subject = "Test Subject";
        msg.BodyText = "Content";
        msg.From = "from@mydomain.com";
        msg.To = "to@somedomain.com";
        
        // And send it.
        client.Send(msg);
        
      7. After completing your work, call the Disconnect method to close the SMTP session. 

      Final example code

      using System;
      using ComponentPro.Net;
      using ComponentPro.Net.Mail;
      
      ...
      
      public void HandleCommandSentResponseReadEvents()
      {
          // Create a new instance. 
          Smtp client = new Smtp();
      
          // Register event handlers. 
          client.CommandResponse += client_CommandResponse;
      
          // Connect to the SMTP server. 
          client.Connect("server");
      
          // Authenticate. 
          client.Authenticate("test", "test");
      
          // ... 
       
          // Do something here 
          // ... 
       
          // Create a new mail message. 
          MailMessage msg = new MailMessage();
          msg.Subject = "Test Subject";
          msg.BodyText = "Content";
          msg.From = "from@mydomain.com";
          msg.To = "to@somedomain.com";
      
          // And send it. 
          client.Send(msg);
      
          // ... 
       
          // 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);
      }