MailBee.NET Objects 7.1

Pop3.BeginDownloadMessages Method 

Begins an asynchronous request for downloading the entire or partial messages in the specified range from the server.

public IAsyncResult BeginDownloadMessages(
   int startIndex,
   int count,
   int bodyLineCount,
   AsyncCallback callback,
   object state
);

Parameters

startIndex
The ordinal position (in the inbox) of the first message in the range to be downloaded.
count
Number of messages to be downloaded, or -1 to indicate that all messages in the range startIndex to InboxMessageCount must be downloaded.
bodyLineCount
Number of lines of the message source body to download in addition to the message source header, or -1 to download the entire messages.
callback
The AsyncCallback delegate. You can leave it a null reference (Nothing in Visual Basic) if you do not use callbacks.
state
An object that contains state information for this request. You can leave it a null reference (Nothing in Visual Basic).

Return Value

An IAsyncResult that references the asynchronous downloading the message.

Remarks

This method is an asynchronous version of DownloadMessageHeaders.

Exceptions

Exception Type Condition
MailBeeInvalidStateException There is already an operation in progress.

Example

This sample asynchronously downloads headers of all the messages in the inbox. Then, it displays the following information about each message:

MessageDataChunkReceived event is handled in order to track the download progress. No callback function is used. The sample is written for a console application.
[C#]
using System;
using MailBee;
using MailBee.Pop3Mail;
using MailBee.Mime;

class Sample
{
    // MessageDataChunkReceived event handler.
    private static void OnMessageDataChunkReceived(object sender,
        Pop3MessageDataChunkReceivedEventArgs e)
    {
        Console.WriteLine(e.BytesJustReceived + " bytes of the message #" +
            e.MessageNumber + " received");
    }

    // The actual code.
    static void Main(string[] args)
    {
        Pop3 pop = new Pop3();
        pop.Connect("pop.somehost.com");
        pop.Login("jdoe", "secret");

        // Subscribe to MessageDataChunkReceived event.
        pop.MessageDataChunkReceived +=
            new Pop3MessageDataChunkReceivedEventHandler(OnMessageDataChunkReceived);

        // Initiate an asynchronous download attempt.
        pop.BeginDownloadMessages(1, -1, 0, null, null);

        // Simulate some lengthy work here. At the same time,
        // the messages are downloaded on another thread.
        System.Threading.Thread.Sleep(3000);

        // End the messages download operation and return MailMessageCollection object.
        // If the operation is still in progress at the moment when 
        // this method starts, the method will wait until the operation completion.
        MailMessageCollection msgs = pop.EndDownloadMessages();

        foreach (MailMessage msg in msgs)
        {
            Console.WriteLine("Message #" + msg.IndexOnServer +
                ": the length of the entire message is " + msg.SizeOnServer +
                " bytes, the length of the downloaded header is " + msg.Size + " bytes.");
        }

        // Disconnect from the server.
        pop.Disconnect();
    }
}
[Visual Basic]
Imports System
Imports MailBee
Imports MailBee.Pop3Mail
Imports MailBee.Mime

Class Sample
    ' MessageDataChunkReceived event handler.
    Private Shared Sub OnMessageDataChunkReceived(ByVal sender As Object, _
        ByVal e As Pop3MessageDataChunkReceivedEventArgs)
        Console.WriteLine(e.BytesJustReceived & " bytes of the message #" & _
            e.MessageNumber & " received")
    End Sub
    
    ' The actual code
    Shared Sub Main(ByVal args As String())
        Dim pop As Pop3
        pop.Connect("pop.somehost.com")
        pop.Login("jdoe", "secret")

        ' Subscribe to MessageDataChunkReceived event.
        AddHandler pop.MessageDataChunkReceived, AddressOf OnMessageDataChunkReceived

        ' Initiate an asynchronous download attempt.
        pop.BeginDownloadMessages(1, -1, 0, Nothing, Nothing)

        ' Simulate some lengthy work here. At the same time,
        ' the messages are downloaded on another thread.
        System.Threading.Thread.Sleep(3000)

        ' End the messages download operation and return MailMessageCollection object.
        ' If the operation is still in progress at the moment when 
        ' this method starts, the method will wait until the operation completion.
        Dim msgs As MailMessageCollection
        msgs = pop.EndDownloadMessages()
        
        For Each msg As MailMessage In msgs
            Console.WriteLine("Message #" + msg.IndexOnServer & _
                ": the length of the entire message is " & msg.SizeOnServer & _
                " bytes, the length of the downloaded header is " & msg.Size & " bytes.")
        Next

        ' Disconnect from the server.
        pop.Disconnect()
    End Sub
End Class

See Also

Pop3 Class | MailBee.Pop3Mail Namespace | DownloadEntireMessages | DownloadMessageHeaders