How to retrieve mail structure without downloading it from an IMAP server?

0 votes
asked May 31, 2011 by Rebex KB (8,210 points)
edited Oct 19, 2012

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
answered May 31, 2011 by Rebex KB (8,210 points)
edited Oct 19, 2012
 
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.

commented Oct 19, 2012 by Jan Sotola (16,580 points)
edited Oct 19, 2012

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.

...