MailBee.NET Objects 7.2

Pop3.DownloadEntireMessages Method ()

Completely downloads all the messages from the inbox on the server.

public MailMessageCollection DownloadEntireMessages();

Return Value

On success, a MailMessageCollection containing the downloaded messages; otherwise, a null reference (Nothing in Visual Basic).

Remarks

If the POP3 server supports pipelining, this method will download all the messages in a single network operation, which greatly increases performance and reduces network traffic.

Note   As this method downloads all e-mails in the inbox, you must have plenty of memory to have such operation done. In case of out-of-memory issues, download large emails individually.

You can use GetMessageSizes method to learn the sizes of all e-mails in the inbox without downloading them. For instance, if you found that you have 10 small e-mails less than 10MB total, then a large 100MB e-mail, then many small e-mails 500MB total, you can first download 10 small emails with a single call of DownloadEntireMessages overload, process them, then download a large e-mail with DownloadEntireMessage method, process it, and then use DownloadEntireMessages overload again several times to get e-mails downloaded in approx. 100MB groups. This way, you won't need to download each and every e-mail one-by-one (which would defeat the idea of using pipelining) and still won't use too much memory. Small e-mails will be downloaded in a batch while large ones will get downloaded on their own.

Exceptions

Exception TypeCondition
MailBeeExceptionAn error occurred and ThrowExceptions is true.

Example

This sample completely downloads all the messages from the inbox, and displays the filename and data size for each of the attachments of each downloaded message. MessageDataChunkReceived event is used to track the download progress. 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("mail.domain.com");
        pop.Login("jdoe", "secret");

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

        // Download the messages.
        MailMessageCollection msgs = pop.DownloadEntireMessages();

        // Display some information about downloaded messages.
        foreach (MailMessage msg in msgs)
        {
            string attachFilenames = string.Empty;
            if (msg.Attachments.Count == 0)
            {
                attachFilenames = "no";
            }
            else
            {
                foreach (Attachment attach in msg.Attachments)
                {
                    if (attachFilenames != string.Empty)
                    {
                        attachFilenames += "; ";
                    }
                    attachFilenames += attach.Filename;
                }
            }
            Console.WriteLine("Message #" + msg.IndexOnServer +
                " contains " + attachFilenames + " attachment(s)");
        }

        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("mail.domain.com")
        pop.Login("jdoe", "secret")

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

        ' Download the messages
        Dim msgs As MailMessageCollection
        msgs = pop.DownloadEntireMessages()

        ' Display some information about downloaded messages.
        For Each msg As MailMessage In msgs
            Dim attachFilenames As String
            attachFilenames = String.Empty
            If msg.Attachments.Count = 0 Then
                attachFilenames = "no"
            Else
                For Each attach As Attachment In msg.Attachments
                    If attachFilenames <> String.Empty Then
                        attachFilenames &= "; "
                    End If
                    attachFilenames &= attach.Filename
                Next
            End If
            Console.WriteLine("Message #" & msg.IndexOnServer & _
                " contains " & msg.Attachments.Count & " attachment(s)")
        Next
        pop.Disconnect()
    End Sub
End Class

See Also

Pop3 Class | MailBee.Pop3Mail Namespace | Pop3.DownloadEntireMessages Overload List