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

asked 11 May '11, 17:28

Rebex%20KB's gravatar image

Rebex KB ♦♦
258519
accept rate: 63%


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.

link

answered 16 May '11, 09:28

Jan%20Sotola's gravatar image

Jan Sotola ♦♦
3566
accept rate: 36%

edited 16 May '11, 09:36

Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Tags:

×76
×53
×36
×10

Asked: 11 May '11, 17:28

Seen: 507 times

Last updated: 16 May '11, 09:36