2

I'm using MailKit to get a list of full IMessageSummaries like this:

var allMessages = remoteFolder.Fetch(remoteIndexList, MessageSummaryItems.Full | MessageSummaryItems.Flags | MessageSummaryItems.UniqueId | MessageSummaryItems.BodyStructure);

I'm not interested in any optimizations like downloading parts of the IMessageSummary etc., I just want the whole data, as fast as I can get it.

But using the above approach, I can't then read properly the HTML in the message's body, for example using HtmlPreviewVisitor because the Body property of IMessageSummary is a BodyPartBasic. I need the whole MimeMessage, obviously.

The problem is that if I want to get multiple MimeMessages, I can't, I can only get one at a time using ImapClient.GetMessage(int index, ...) method.

Is there a way to extract all of the parts from the original MimeMessage that was used to create IMessageSummary and use them with HtmlPreviewVisitor without having to download each full MimeMessage again?

Nevca
  • 194
  • 1
  • 1
  • 9

1 Answers1

5

What you need to do is:

foreach (var item in allMessages) {
    var message = remoteFolder.GetMessage (item.UniqueId);
}

The IMessageSummary doesn't actually contain the message or download the message, it just asks the IMAP server for various bits of metadata about the message (such as the structure of the message, the flags such as read/unread, the sender, subject, date, to, cc, reply-to, etc).

If you have no interest in fetching individual MIME parts from the IMAP server, then you might want to use a reduced set of MessageSummaryItems such as MessageSummaryItems.All | MessageSummaryItems.UniqueId (I know, All seems like it should contain more than Full, but that's not how the IMAP aliases work...).

You may even be able to further reduce the MessageSummaryItems that you are requesting depending on what pieces of data you are planning to use.

For example, if you don't use the IMessageSummaryItem.Envelope property, then you don't need the MessageSummaryItems.Envelope flag and by not requesting it, you'll speed up the query because the IMAP server won't send you as much info.

jstedfast
  • 35,744
  • 5
  • 97
  • 110
  • Yes, I figured out I should use GetMessage() for each IMessageSummary but why isn't there GetMessages() method that would return a list of messages using just one request to the IMAP server, just like Fetch()? – Nevca Oct 18 '17 at 06:04
  • Because that’s a lot of data that would be dumped into memory. – jstedfast Oct 18 '17 at 11:46
  • Thanks @jstedfast – Nevca Oct 20 '17 at 04:21