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 Imap 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 Imap class.
      3. Now you can connect to the IMAP server with Connect methods. The code looks similar to the following:
        // Create a new Imap instance.
        Imap client = new Imap();
        
        // Connect to the IMAP 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 downloading messages, etc.  The code looks similar to the following:
        // Register an event handler.
        client.Progress += client_Progress;
        
        client.Select("INBOX");
        
        // Download a message with sequence number #1.
        client.DownloadMessage(1, "c:\\temp\\my message.eml");
        
      7. After completing your work, call the Disconnect method to close the IMAP session. 

      Final example code

      using System;
      using ComponentPro.Net.Mail;
      
      ...
      
      public void ShowProgress()
      {
          // Create a new Imap instance. 
          Imap client = new Imap();
      
          // Connect to the IMAP server. 
          client.Connect("myserver");
      
          // Authenticate. 
          client.Authenticate("test", "test");
      
          try 
          {
              // Register an event handler. 
              client.Progress += client_Progress;
      
              client.Select("INBOX");
      
              // Download a message with sequence number #1. 
              client.DownloadMessage(1, "c:\\temp\\my message.eml");
          }
          catch (ImapException exc)
          {
              Console.WriteLine("Exception: " + exc.Message);
          }
      
          // Disconnect. 
          client.Disconnect();
      }
      
      void client_Progress(object sender, ImapProgressEventArgs e)
      {
          // Show progress information. 
          if (e.Length > 0 && e.State == ImapTransferState.Downloading)
          {
              Console.Write("\rDownloaded: {0} bytes ({1}% completed)", e.BytesTransferred, e.Percentage);
          }
      }