MailBee.NET Objects 4.0

Imap.DownloadMessageHeaders Method 

Downloads the header of each message in the specified set from the server.

public MailMessageCollection DownloadMessageHeaders(
   string messageIndexSet,
   bool indexIsUid
);

Parameters

messageIndexSet
A message sequence string containing ordinal message numbers or UIDs. Can be composed manually or using ToString.
indexIsUid
If true, messageIndexSet is treated as a sequence of UIDs; otherwise, as a sequence of ordinal message numbers.

Return Value

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

Remarks

To learn how to specify a valid message sequence (messageIndexSet value), see DownloadEnvelopes topic.

This method will download all the message headers in a single network operation, which greatly increases performance and reduces network traffic. However, if the application needs message headers just to get common information about messages (From, To, Date, Subject, Size, etc), it's more efficient to use DownloadEnvelopes method. Typically, IMAP4 ENVELOPE structure and related information is 200-400 bytes in size, while message header sections are usually larger 500 bytes. ENVELOPE structure, however, does not contain header fields which describe priority/importance of the message, attachments presence, etc. On other hand, the application may avoid downloading message header section and still obtain all the necessary information by requesting envelopes, body structures, and the required headers using DownloadEnvelopes method.

Note   To track downloading headers, EnvelopeDownloaded event should be used. There is no MessageDownloaded event since mail messages are actually downloaded within envelopes. Once an envelope is downloaded, the mail message data is extracted from it. Generally speaking, DownloadMessageHeaders is a kind of overload of DownloadEnvelopes method.

Exceptions

Exception TypeCondition
MailBeeExceptionAn error occurred and ThrowExceptions is true.

Example

This console sample downloads message header sections of the last 5 messages in the inbox, and displays them.

[C#]
using System;
using MailBee;
using MailBee.ImapMail;
using MailBee.Mime;

class Sample
{
    static void Main(string[] args)
    {
        Imap imp = new Imap();

        // Connect to the server, login and select inbox.
        imp.Connect("mail.domain.com");
        imp.Login("jdoe@domain.com", "secret");
        imp.SelectFolder("INBOX");

        // Download the last 5 message headers. Note: we do not check if
        // there are less than 5 messages in the inbox. See sample code
        // in DownloadEnvelopes(string, bool) overload for more details.
        MailMessageCollection msgs = imp.DownloadMessageHeaders(
            (imp.MessageCount - 4).ToString() + ":*", false);

        // Display header sections of the downloaded message headers.
        foreach (MailMessage msg in msgs)
        {
            Console.WriteLine("Header section of message #" + msg.IndexOnServer);
            Console.WriteLine();
            Console.WriteLine(msg.RawHeader);
            Console.WriteLine("===============================================");
        }

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

Module Sample
    Sub Main(ByVal args As String())
        Dim imp As New Imap

        ' Connect to the server, login and select inbox.
        imp.Connect("mail.domain.com")
        imp.Login("jdoe", "secret")
        imp.SelectFolder("INBOX")

        ' Download the last 5 message headers. Note: we do not check if
        ' there are less than 5 messages in the inbox. See sample code
        ' in DownloadEnvelopes(string, bool) overload for more details.
        Dim msgs As MailMessageCollection = _
            imp.DownloadMessageHeaders((imp.MessageCount - 4).ToString() & ":*", False)

        ' Display header sections of the downloaded message headers.
        For Each msg As MailMessage In msgs
            Console.WriteLine("Header section of message #" & msg.IndexOnServer)
            Console.WriteLine()
            Console.WriteLine(msg.RawHeader)
            Console.WriteLine("===============================================")
        Next

        ' Disconnect from the server.
        imp.Disconnect()
    End Sub
End Module

See Also

Imap Class | MailBee.ImapMail Namespace