Find message by unique messageId value

Sep 1, 2015 at 8:11 PM
I see many functions that reference a message by an integer index value, but how do you find a message by the messageID property value?
Sep 1, 2015 at 9:21 PM
IMAP allows you to reference messages by index or by UID, but not by Message-Id. If you want to a look up a message based on its Message-Id header value, then you'll need to use the SEARCH command.

Since MailSystem.NET is a dead project, I'll illustrate how to do this using MailKit:
var matches = client.Inbox.Search (SearchQuery.HeaderContains ("Message-Id", messageIdStringValue));
foreach (var uid in matches) {
    var message = client.Inbox.GetMessage (uid);

Hope that helps.
Sep 2, 2015 at 4:32 PM

Thanks!! I’ve switched over to MailKit.

How do you get an attachment count when you are iterating through MessageSummaryItems?

Sep 3, 2015 at 6:03 PM
Edited Sep 3, 2015 at 6:08 PM
You can do something like this:
int CountAttachments (BodyPart body)
    var multipart = body as BodyPartMultipart;
    var basic = body as BodyPartBasic;
    int count = 0;

    if (multipart != null) {
        foreach (var part in multipart.BodyParts)
            count += CountAttachments (part);
    } else if (basic != null && basic.IsAttachment) {
        count = 1;

    return count;

And then, to use it, you'd just do:
var summaries = client.Inbox.Fetch (uids, MessageSummaryItems.UniqueId | MessageSummaryItems.BodyStructure);
foreach (var summary in summaries) {
    int count = CountAttachments (summary.Body);