ComponentPro UltimateMail

      Displaying progress while transferring data

      Language Filter: AllSend comments on this topic to ComponentPro

      When the mail message to transfer is big and your application may take time to transfer, you may wish to show the progress of the transfer to the user. The Smtp class provides progress notification through the Progress event. The Progress event is raised periodically while data transfer is in progress, making accessible necessary data to display progress information, such as the size of the file and the number of transferred bytes.

      The following steps show you how to use the Progress event to show progress information while transferring a file:

      Displaying progress while transferring data

      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.Mail;
        
      2. Create a new instance of the Smtp class.
      3. Now you can connect to the SMTP server with Connect methods. The code looks similar to the following:
        // Create a new Smtp instance.
        Smtp client = new Smtp();
        
        // Connect to the SMTP server.
        client.Connect("myserver");
        
      4. Use your user name and password to login with Authenticate methods. The code looks similar to the following:
        // Authenticate.
        client.Authenticate("test", "test");
        
      5. Register an event handler to the Progress event.
      6. Do your works such as creating a new mail message and send it, etc. The code looks similar to the following:
        // Register an event handler.
        client.Progress += client_Progress;
        
      7. After completing your work, call the Disconnect method to close the SMTP session.

      Final example code

      using System;
      using ComponentPro.Net.Mail;
      
      ...
      
      private long _messageSize;
      
      public void ShowProgress()
      {
          // Create a new Pop3 instance. 
          Pop3 client = new Pop3();
      
          // Connect to the POP3 server. 
          client.Connect("myserver");
      
          // Authenticate. 
          client.Authenticate("test", "test");
      
          try 
          {
              // Register an event handler. 
              client.Progress += client_Progress;
      
              Pop3MessageCollection list = client.ListMessages(Pop3EnvelopeParts.Size | Pop3EnvelopeParts.MessageInboxIndex);
              // Download the first message. 
              _messageSize = list[0].Size;
      
              // Download the first one. 
              client.DownloadMessage(list[0].MessageInboxIndex, "c:\\temp\\my message.eml");
          }
          catch (Pop3Exception exc)
          {
              Console.WriteLine("Exception: " + exc.Message);
          }
      
          // Disconnect. 
          client.Disconnect();
      }
      
      void client_Progress(object sender, Pop3ProgressEventArgs e)
      {
          // Show progress information. 
          if (e.State == Pop3TransferState.Downloading)
          {
              Console.Write("\rDownloaded: {0} bytes ({1}% completed)", e.BytesTransferred, e.CalculatePercentage(_messageSize));
          }
      }