How to show progress when retrieving mail with a big attachment

0 votes
asked May 11, 2011 by Rebex KB (8,430 points)
edited May 16, 2011

I want to monitor the download progress of an attachment if the file is too big.

Applies to: Rebex Secure Mail

1 Answer

0 votes
answered May 16, 2011 by Jan Sotola (17,670 points)
edited May 16, 2011

The progress can be monitored by handling the TransferProgress event of the Imap or Pop3 class.

Here is a simple example for displaying progress in console application retrieving mail from an Imap server:

class Program
    static void client_TransferProgress(object sender, ImapTransferProgressEventArgs e)
        if (e.BytesTotal > 0)
            Console.Write("\r{0:F0}%", 100m * e.BytesTransferred / e.BytesTotal);

    static void Main(string[] args)
        using (Imap client = new Imap())
            // connect and log in   
            client.Login("username", "password");

            // select working folder

            // get the MessageInfo (without message body and attachments 
            // but with info whether the message contains attachments)
            ImapMessageInfo messageInfo = client.GetMessageInfo(client.CurrentFolder.TotalMessageCount, ImapListFields.Envelope | ImapListFields.MessageStructure);
            Console.WriteLine("Reading mail sub. '{0}' (attachments = {1})...", messageInfo.Subject, messageInfo.HasAttachment);

            // set the progress event handler
            client.TransferProgress += 
                new ImapTransferProgressEventHandler(client_TransferProgress);

            // get the full message (including attachments)
            MailMessage message = client.GetMailMessage(client.CurrentFolder.TotalMessageCount);
            Console.WriteLine("\nMail read - ({0} attachaments)", message.Attachments.Count);

In WinForm applications, you can use the same event handler in common way. Just don't forget to refresh the UI during progress reporting – e.g by calling Application.DoEvents() method within the event handler.