Search code examples
c#asp.net-mvcemailimapmailkit

Mailkit error No uids were specified. Parameter name: uids


I want to get all of messages in inbox like this:

using (ImapClient client = new ImapClient(new ProtocolLogger("/imap.log", append: true)))
{
   client.Timeout = 1200000;

   if (!client.IsConnected)
      client.Connect(imap.testserver.com, testportnumber, true);
   client.Authenticate(email, lockmail);
   var searchcond = SearchQuery.All;
   var Inbox = client.Inbox;
   Inbox.Open(MailKit.FolderAccess.ReadOnly);
   var inbox = Inbox.Search(searchcond);
   var fetchs = Inbox.Fetch(inbox, MessageSummaryItems.Envelope | MessageSummaryItems.Flags);
}

On Fetch line this error occur: No uids were specified. Parameter name: uids

is that mean Inbox.Search return some Uids that doesnt exist on mailfolder?

How can i solve it?


Solution

  • It means that Search() returned 0 UIDs.

    You can avoid this problem by doing:

    var uids = inbox.Search (query);
    if (uids.Count > 0) {
        var messages = inbox.Fetch (uids, ...);
        ...
    }
    

    If you are using MailKit older than 2.0.7, you can try upgrading to 2.0.7 which should make this not a problem (as in, the Fetch() method should not handle an empty set of uids and return w/o error).