0 votes
by (8.6k points)
edited

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
by (18.1k points)
edited

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.Connect("imap.example.com");
            client.Login("username", "password");

            // select working folder
            client.SelectFolder("Inbox");

            // 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.

...