0 votes
by (480 points)

Unable to fetch the yahoo mail using Message-ID in IMAP Search method. It returns empty string .

ImapMessageCollection Msgsearch = imap.Search(ImapSearchParameter.Header("Message-ID", "51564654484944321346546654.JavaMail.yahoo@mail.yahoo.com"));

this returns Msgsearch count = 0

Applies to: Rebex Secure Mail

1 Answer

+1 vote
by (72.7k points)
selected by
 
Best answer

It seems that Yahoo cannot search for Message-ID (it is not fully featured IMAP server).

You have to do the search manually.

It can be done like this:

private static ImapMessageCollection SearchMessageId(Imap client, string messageId)
{
    // Message-ID to search for
    MessageId id = new MessageId(messageId);

    // iterate through all messages
    ImapMessageCollection result = new ImapMessageCollection();
    var page = client.GetMessageList(ImapListFields.Envelope);
    foreach (var item in page)
    {
        if (id.Equals(item.MessageId))
            result.Add(item);
    }
    return result;
}

However, this code retrieves all messages. It can be very slow and the server can limit the result to only first N messages. Instead, I suggest you to perform paged list. Alternatively, you can stop search after first successful match. The method can look like this:

private static ImapMessageCollection SearchMessageId(Imap client, string messageId, int pageSize)
{
    // Message-ID to search for
    MessageId id = new MessageId(messageId);

    // iterate through all messages
    ImapMessageCollection result = new ImapMessageCollection();
    for (int i = client.CurrentFolder.TotalMessageCount; i > 0; i -= pageSize)
    {
        var set = new ImapMessageSet();
        set.AddRange(Math.Max(1, i - pageSize), i);

        var page = client.GetMessageList(set, ImapListFields.Envelope);
        foreach (var item in page)
        {
            if (id.Equals(item.MessageId))
                result.Add(item);
        }

        if (result.Count > 0)
            break;
    }
    return result;
}

It can be used like this:

var msgSearch = SearchMessageId(
    imap,
    "51564654484944321346546654.JavaMail.yahoo@mail.yahoo.com",
    50);
by (480 points)
thanks Lukas Matyska
...