0 votes

We are using Mail.dll to connect to an 'outlook.office365.com' mail account.

The Inbox consists of a collection of Seen and Unseen emails.

We are trying to return a list of emails that have a specific email address and have not been read. We are trying to achieve this using the where clause on the imap.Search() function.

The search returns a number of results which doesn't match whats in the search criteria.

If we interrogate the returned results we observed that all of the emails have the flag 'Seen' set.

This was working previously, we have only noticed this after moving across to Office365.

using (Imap imap = new Imap())
{
    imap.ConnectSSL(InboundServerName);
    imap.UseBestLogin(InboundUserName, InboundPassword);
    imap.SelectInbox();

    List<long> uids = imap.Search().Where(
        Expression.And(
            Expression.HasFlag(Flag.Unseen),
            Expression.Or(
                Expression.To(EmailAddress),
                Expression.Cc(EmailAddress))
        ));

    foreach (var uid in uids)
    {
        IMail email = new MailBuilder().CreateFromEml(
        imap.PeekMessageByUID(uid));
        var flags = imap.GetFlagsByUID(uid);
    }
    imap.Close();
}
by (200 points)

1 Answer

0 votes

First I need to explain that entire search process is done by the IMAP server.
Mail.dll creates and sends a search query and the retrieves the results. If the server is performing search incorrectly, there is not much you can do.

Try using:

Expression.Not(Expression.HasFlag(Flag.Seen))

instead of:

Expression.HasFlag(Flag.Unseen)

You can also turn on logging to see the query send by Mail.dll IMAP library:
https://www.limilabs.com/blog/logging-in-mail-dll

by (270k points)
edited by
We have waited 12 hours before running the query again, once again we are not receiving the correct amount of unseen emails. We have also run a basic test that returns unseen emails, and this returns the expected list ( including the newly added email). We have programmatically set the emails as seen expecting the query to return the remaining emails on the next run, but unfortunately we received the same amount of emails, just with the flag Seen set.

We have added logging, is there anything particular you require or we could supply from the output?
You can show the query generated by Mail.dll here (UID SEARCH line), but as we are not responsible for how your server performs search, we can't help you.

You should report this issue to your server administrator.
For additional testing, we created a new @outlook.com email address on a different server. We sent this address 3 emails. When running the query ALL 3 emails appear. After marking 1 as read, we still received the original 3 emails. 1 email returned with the flag 'Seen'.

We continually ran the query for 30 minutes without change. After modifying the query back to the original (the query posted in our initial question) we received our expected result. But after sending it a fourth email, the new email in not included in the results.
Each query returns correct result only the first time around. This look like caching issue, isn't it?
We did some tests on our own and I can confirm this is a server problem.

S: * 4 FETCH (UID 1888 FLAGS ())           // <- 1888 message is unseen
S: 17266eca7d4c4ef3 OK FETCH completed.
C: ab5175b61f664486 UID SEARCH SEEN TO test@example.com // <- we search for seen email
S: * SEARCH 1888                           // <- incorrect: 1888 is unseen

The only way to workaround it is to perform 2 searches and process the results on the client side.
...