MailBee.NET Objects 4.0

Pop3.BeginDownloadMessage Method 

Begins an asynchronous request for downloading the specified message header or entire message from the server.

public IAsyncResult BeginDownloadMessage(
   int index,
   int bodyLineCount,
   AsyncCallback callback,
   object state
);

Parameters

index
The ordinal position of the message in the inbox. It must be in the range 1 to InboxMessageCount.
bodyLineCount
Number of lines of the message source body to download in addition to the message source header, or -1 to download the entire message.
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 DownloadMessageHeader.

If multiple messages or message headers are downloaded, it's recommended to use BeginDownloadMessages method, since it can operate much faster if the server supports pipelining.

Exceptions

Exception TypeCondition
MailBeeInvalidStateExceptionThere is already an operation in progress.

Example

This WinForms sample demonstrates asynchronous downloading of the first message in the inbox. No callback function is used. Once the message is downloaded, all the attachments to the message are saved into C:\Temp folder.

[C#]
// To use the code below, import MailBee namespaces at the top of your code.
using MailBee;
using MailBee.Pop3Mail;
using MailBee.Mime;

// Put the code below inside your class.

// The actual code.
private void Form1_Load(object sender, System.EventArgs e)
{
    Pop3 pop = new Pop3();

    // Let MailBee process events.
    pop.RaiseEventsViaMessageLoop = false;

    pop.Connect("pop.somehost.com");
    pop.Login("jdoe", "secret");

    // Initiate an asynchronous download attempt.
    // bodyLineCount = -1 to indicate the entire message must be downloaded.
    pop.BeginDownloadMessage(1, -1, null, null);

    // Simulate some lengthy work here...
    for (int i = 0; i < 100; i++)
    {
        // Make a portion of the work.
        System.Threading.Thread.Sleep(10);

        // Process events which were raised during execution of the work above.
        pop.Wait(0);
    }

    // End the message download operation and return MailMessage object.
    // If the operation is still in progress at the moment when 
    // this method starts, the method will wait until the operation completion.
    MailMessage msg = pop.EndDownloadMessage();

    // Save all attachments into C:\Temp folder.
    msg.Attachments.SaveAll(@"C:\Temp");

    // Disconnect from the server.
    pop.Disconnect();
}
[Visual Basic]
' To use the code below, import MailBee namespaces at the top of your code.
Imports MailBee
Imports MailBee.Pop3Mail
Imports MailBee.Mime

' Put the code below inside your class.

' The actual code.
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs)
    Dim pop As New Pop3

    ' Let MailBee process events.
    pop.RaiseEventsViaMessageLoop = False

    pop.Connect("pop.somehost.com")
    pop.Login("jdoe", "secret")

    ' Initiate an asynchronous download attempt.
    ' bodyLineCount = -1 to indicate the entire message must be downloaded.
    pop.BeginDownloadMessage(1, -1, Nothing, Nothing)

    ' Simulate some lengthy work here...
    Dim i As Integer
    For i = 1 To 100
        ' Make a portion of the work.
        System.Threading.Thread.Sleep(10)

        ' Process events which were raised during execution of the work above.
        pop.Wait(0)
    Next

    ' End the message download operation and return MailMessage object.
    ' If the operation is still in progress at the moment when 
    ' this method starts, the method will wait until the operation completion.
    Dim msg As MailMessage
    msg = pop.EndDownloadMessage()

    ' Save all attachments into C:\Temp folder.
    msg.Attachments.SaveAll("C:\Temp")

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

See Also

Pop3 Class | MailBee.Pop3Mail Namespace | DownloadEntireMessage | DownloadMessageHeader | BeginDownloadMessages