0 votes

Using IMAP library with Microsoft Exchange.

If I send some emails to the mailbox from an external email account and then use IMAP and access the mails like this (after doing connect and login)

theImap.SelectInbox()    'Find all unseen messages.
uids = theImap.Search(Flag.Unseen)

it is hit any miss if anything get returned, sometimes it works sometimes it doesnt.

I do not have any other client connected to the mailbox.

Having failed to detect new emails, I have proven the emails exist by running with

theImap.Search(Flag.All)

And all mails are in the mailbox.

How can I reliably detect emails which I have not previously read?

Thanks
Any ideas

by

1 Answer

0 votes
  1. Search is always performed on the IMAP server. If it returns incorrect results it's the server's issue.

  2. Turn on logging. I would like to see unseen search (Search(Flag.Unseen)) and flags for all messages (GetFlagsByUID(theImap.GetAll())) in the same log.

  3. If you find \UNSEEN flag unreliable, due to Exchange server search error or due to another account accessing the same account, please read the article, that explains how to get new / unprocessed mails only.

  4. Remember that downloading a mail (using Imap.GetMessageByUID), adds the seen flag to it. You can use *Imap.Peek** methods if you don't want the flag to be set by an IMAP server.

by (301k points)
edited by
Connect/Login/SelectInbox/.Search(Flag.Unseen) gives the following. There is at least one new email on the server.
However, Search(Flag.Unseen) returns 0 UIDs.

Logging follows, (domain name in login below changed by me). I would be grateful for your comments :

Mail.dll: 22 11:35:50 3.0.14185.1633
Mail.dll: 22 11:35:50 S: * OK The Microsoft Exchange IMAP4 service is ready.
Mail.dll: 22 11:35:50 C: fb9949a9909e442e CAPABILITY
Mail.dll: 22 11:35:50 S: * CAPABILITY IMAP4 IMAP4rev1 AUTH=NTLM AUTH=GSSAPI AUTH=PLAIN UIDPLUS CHILDREN IDLE NAMESPACE LITERAL+
Mail.dll: 22 11:35:50 S: fb9949a9909e442e OK CAPABILITY completed.
Mail.dll: 22 11:35:50 C: 23a0087751f34bd8 LOGIN mhr_smd_incoming@*****.co.uk pass
Mail.dll: 22 11:35:51 S: 23a0087751f34bd8 OK LOGIN completed.
Mail.dll: 22 11:35:51 C: 40b1de03a3a14ca8 CAPABILITY
Mail.dll: 22 11:35:51 S: * CAPABILITY IMAP4 IMAP4rev1 AUTH=NTLM AUTH=GSSAPI AUTH=PLAIN UIDPLUS CHILDREN IDLE NAMESPACE LITERAL+
Mail.dll: 22 11:35:51 S: 40b1de03a3a14ca8 OK CAPABILITY completed.
Mail.dll: 22 11:35:51 C: 63a9ee57c1b641f4 SELECT "INBOX"
Mail.dll: 22 11:35:51 S: * 5 EXISTS
Mail.dll: 22 11:35:51 S: * 0 RECENT
Mail.dll: 22 11:35:51 S: * FLAGS (\Seen \Answered \Flagged \Deleted \Draft $MDNSent)
Mail.dll: 22 11:35:51 S: * OK [PERMANENTFLAGS (\Seen \Answered \Flagged \Deleted \Draft $MDNSent)] Permanent flags
Mail.dll: 22 11:35:51 S: * OK [UIDVALIDITY 54090] UIDVALIDITY value
Mail.dll: 22 11:35:51 S: * OK [UIDNEXT 50] The next unique identifier value
Mail.dll: 22 11:35:51 S: 63a9ee57c1b641f4 OK [READ-WRITE] SELECT completed.
Mail.dll: 22 11:35:51 C: edc7f1ee12e449ce UID SEARCH UNSEEN
Mail.dll: 22 11:35:51 S: * SEARCH
Mail.dll: 22 11:35:51 S: edc7f1ee12e449ce OK SEARCH completed.
Without GetFlagsByUID this log shows nothing. Server simply thinks there are no unseen messages. How do you know that the unseen message is there? Are you sure you are using the same account for this check?
can you advise what namespace I need to use the GetMessageFlagsByUID function.
I have checked installed and online references and it does not appear to exist.

Thanks
It's called GetFlagsByUID - my mistake, sorry.
I found this after I sent my reply - no problem at all.

The server thinks all emails have been seen. As I said at least one definitely has not.

Mail.dll: 22 11:57:27 3.0.14185.1633
Mail.dll: 22 11:57:27 S: * OK The Microsoft Exchange IMAP4 service is ready.
Mail.dll: 22 11:57:27 C: 45732e9231624e95 CAPABILITY
Mail.dll: 22 11:57:28 S: * CAPABILITY IMAP4 IMAP4rev1 AUTH=NTLM AUTH=GSSAPI AUTH=PLAIN UIDPLUS CHILDREN IDLE NAMESPACE LITERAL+
Mail.dll: 22 11:57:28 S: 45732e9231624e95 OK CAPABILITY completed.
Mail.dll: 22 11:57:28 C: b234189ea9444586 LOGIN mhr_smd_incoming@****.co.uk pass
Mail.dll: 22 11:57:28 S: b234189ea9444586 OK LOGIN completed.
Mail.dll: 22 11:57:28 C: 684445a277464ecb CAPABILITY
Mail.dll: 22 11:57:28 S: * CAPABILITY IMAP4 IMAP4rev1 AUTH=NTLM AUTH=GSSAPI AUTH=PLAIN UIDPLUS CHILDREN IDLE NAMESPACE LITERAL+
Mail.dll: 22 11:57:28 S: 684445a277464ecb OK CAPABILITY completed.
Mail.dll: 22 11:57:28 C: 4c456e3d93924530 SELECT "INBOX"
Mail.dll: 22 11:57:28 S: * 5 EXISTS
Mail.dll: 22 11:57:28 S: * 0 RECENT
Mail.dll: 22 11:57:28 S: * FLAGS (\Seen \Answered \Flagged \Deleted \Draft $MDNSent)
Mail.dll: 22 11:57:28 S: * OK [PERMANENTFLAGS (\Seen \Answered \Flagged \Deleted \Draft $MDNSent)] Permanent flags
Mail.dll: 22 11:57:28 S: * OK [UIDVALIDITY 54090] UIDVALIDITY value
Mail.dll: 22 11:57:28 S: * OK [UIDNEXT 50] The next unique identifier value
Mail.dll: 22 11:57:28 S: 4c456e3d93924530 OK [READ-WRITE] SELECT completed.
Mail.dll: 22 11:57:30 C: 46098029bb114b2f UID SEARCH UNSEEN
Mail.dll: 22 11:57:30 S: * SEARCH
Mail.dll: 22 11:57:30 S: 46098029bb114b2f OK SEARCH completed.
Mail.dll: 22 11:57:35 C: dee28d35ca0145ef UID SEARCH ALL
Mail.dll: 22 11:57:35 S: * SEARCH 45 46 47 48 49
Mail.dll: 22 11:57:35 S: dee28d35ca0145ef OK SEARCH completed.
Mail.dll: 22 11:57:36 C: c530393a990548b0 UID FETCH 45:49 (UID FLAGS)
Mail.dll: 22 11:57:36 S: * 1 FETCH (UID 45 FLAGS (\Seen))
Mail.dll: 22 11:57:37 S: * 2 FETCH (UID 46 FLAGS (\Seen))
Mail.dll: 22 11:57:37 S: * 3 FETCH (UID 47 FLAGS (\Seen))
Mail.dll: 22 11:57:37 S: * 4 FETCH (UID 48 FLAGS (\Seen))
Mail.dll: 22 11:57:37 S: * 5 FETCH (UID 49 FLAGS (\Seen))
Mail.dll: 22 11:57:37 S: c530393a990548b0 OK FETCH completed.
I have done another test starting with all emails deleted and then display GetFlagsByUID for all emails.

I then send an email to the mailbox, once it arrives this is displayed as \Recent. Then after about 10 seconds it changes to \Seen.

There are no clients connected to this mailbox other than the software I am running.  The only instructions being run against the mailbox are:

Connect
Login
SelectInbox
Search(Flag.Unseen)
GetFlagsByUID(theImap.GetAll())

Can any of these change the state of the flags ?   If they cant then I dont know what the problem can be. If MS Exchange is not compliant in its implementation, then I presume it would be well known?

This shows the flag change from \Recent, to no flag, to \Seen in around 10s

Mail.dll: 14 12:33:38 S: * 1 FETCH (UID 59 FLAGS (\Recent))
Mail.dll: 21 12:33:41 S: * 1 FETCH (UID 59 FLAGS ())
Mail.dll: 14 12:33:44 S: * 1 FETCH (UID 59 FLAGS ())
Mail.dll: 14 12:33:47 S: * 1 FETCH (UID 59 FLAGS (\Seen))


Your thoughts please.
Is this expected Exchange behaviour ?
No, none of theses methods issue a command that causes the server to set the \SEEN flag. To me it looks like some software is accessing this account (change the password to be sure).
Thanks for your help on this.
No problem, have you found the cause of this behavior?
Yes. One of our VM test platforms was restored to a previous state on Friday and was left pointing to a common database meaning it was sharing the same mailbox I was using. All seems fine now.

Thanks again for your speedy and thorough support,
...