0 votes

For specific folder, GetMessageByUID returns a null value when I tried the first time and in secontime, with the same uid, it gives me an email. can you please check this issue?

by (1.6k points)

1 Answer

+1 vote

Null returned by Imap.GetMessageByUID means that the provided uid is not valid, in other words, message with such uid doesn't exist in the specified folder (it might have been deleted).

Please use Imap.GetAll method to obtain uids of all messages in the selected mailbox or Imap.Search to search for messages matching specified criteria:

List<long> uids = imap.Search(Flag.Unseen);


foreach (long uid in uids)
{
    var eml = imap.GetMessageByUID(uid);

    IMail email = new MailBuilder().CreateFromEml(eml);
}
by (301k points)
List<long> uids;

if (last == null || last.UIDValidity != status.UIDValidity)
{
     uids = repo.MailManager.ImapClient.GetAll();
}
else
{
    uids = repo.MailManager.ImapClient.Search().Where(
                   Expression.UID(
                       Range.From(last.LargestUID))
               );
    uids.Remove(last.LargestUID);
}

foreach (long uid in uids)
{
    lastUid = uid;
    var eml = repo.MailManager.ImapClient.GetMessageByUID(uid);
    IMail email = new MailBuilder().CreateFromEml(eml);
}
                    
Hey, i am using above code i get uid from the mail server itself. Also i do not facing this issue for every folder, i am facing this issue for draft folder only. can you please suggest?
Your code looks OK to me (you should of course save UIDValidity and lastUid for this folder).

I assume that the server removed this message in the mean time,
otherwise it's returning uids that are not valid which is illegal.

Are you sure you are the only one that connects to this mailbox?
This issue happens with me for only one folder and no one is using this account other than me. (And yes I am saving and using VIDValididy and lastUID )
This looks like a server bug to me then. Try gathering the logs ( https://www.limilabs.com/blog/logging-in-mail-dll ) where server returns uids and then refuses to return any data for them, and report that to the server vendor.
...