0 votes
by (8.6k points)
edited

I want to provide information about email structure to a user (such as subject, number of attachments, their names and lengths). How can I retrieve such information from an IMAP server without downloading whole message?

Applies to: Rebex Secure Mail

1 Answer

0 votes
by (8.6k points)
edited
 
Best answer

To retrieve inforamtion about message structure, use the ImapListFields.MessageStructure parameter in the GetMessageInfo or GetMessageList methods of the Rebex IMAP/SSL component.

Following example prints requested information:

C#

// connect and login
Imap imap = new Imap();
imap.Connect(...);
imap.Login(...);

imap.SelectFolder("INBOX");

// retrieve information of emails envelope and structure
ImapMessageCollection col = 
    imap.GetMessageList(ImapListFields.Envelope | ImapListFields.MessageStructure);

// print the information
foreach (ImapMessageInfo item in col)
{
    Console.WriteLine("{0}: {1}", item.SequenceNumber, item.Subject);
    foreach (ImapMessagePart part in item.GetParts())
    {
        if (part.Kind == ImapMessagePartKind.Attachment)
        {
            Console.WriteLine("  {0} [{1} B]", part.FileName, part.Length);
        }
    }
}

VB.NET

''# connect and login
Dim imap = New Imap()
imap.Connect(...)
imap.Login(...)

imap.SelectFolder("INBOX")

''# retrieve information of emails envelope and structure
Dim col = imap.GetMessageList(ImapListFields.Envelope Or ImapListFields.MessageStructure)

''# print the information
For Each item As ImapMessageInfo In col
    Console.WriteLine("{0}: {1}", item.SequenceNumber, item.Subject)
    For Each part In item.GetParts()
        If part.Kind = ImapMessagePartKind.Attachment Then
            Console.WriteLine("  {0} [{1} B]", part.FileName, part.Length)
        End If
    Next part
Next item

Please note the ImapListFields flags are combined together with the OR binary operator (not AND) as shown in the imap.GetMessageList method call.

by (18.1k points)
edited

A special approach is needed when retrieving attachment names from e-mails which have been sent from Hotmail. See the following article for more information.

...