How can i retrieve inline attachment using IMAP methods?

0 votes
asked Jun 9, 2016 by Sigamani (480 points)

I sent one email to Hotmail with Inline attachment. How can i retrieve inline attachment using IMAP methods?

Applies to: Rebex Secure Mail

1 Answer

+1 vote
answered Jun 9, 2016 by Lukas Pokorny (88,550 points)
selected Jun 15, 2016 by Sigamani
 
Best answer

a) The most straightforward way (use this if you need to download the whole message anyway):

  1. Download the whole message as an instance of MailMessage by calling Imap object's GetMailMessage method.

  2. Access inline attachments using MailMessage object's `Resources' collection.

    Sample code:

    MailMessage message = imap.GetMailMessage(uniqueId);
    foreach (LinkedResource resource in message.Resources)
    {
        // show info about the message part
        Console.WriteLine("{0} {1} {2} {3}", resource.ContentType, resource.FileName);
        ...
    }
    

b) Alternatively, if you only need to download a single attachment:

  1. Retrieve the message structure by passing the ImapListFields.MessageStructure flag to Imap's GetMessageInfo or GetMessageList method, which results in an instance of ImapMessageInfo (or a collection in case of the GetMessageInfo).

  2. Call GetParts method on ImapMessageInfo to retrieve an array of message parts (which includes inline attachment).

  3. Iterate through the array to find parts with Kind of ImapMessagePartKind.LinkedResource and determine their Id

  4. Call Imap object's GetMessagePart method to download the inline attachment.

    Sample code:

    // retrieve a list of parts for the specific message
    ImapMessageInfo info = imap.GetMessageInfo(uniqueId, ImapListFields.MessageStructure);
    foreach (ImapMessagePart part in info.GetParts())
    {
        // show info about the message part
        Console.WriteLine("{0} {1} {2} {3}", part.Id, part.Kind, part.ContentType, part.FileName);
    
        if (part.Kind == ImapMessagePartKind.LinkedResource)
        {
            // download the message part if it's a linked resource
            var output = new MemoryStream();
            imap.GetMessagePart(uniqueId, part.Id, output);
            Console.WriteLine("Message part downloaded");
            ...
        }
    }
    
...