2

Using JavaMail API and IMAP, i want to learn that a message has been moved from folder a to b. how can i do that without adding listeners? i mean i want to discover the changes of messages when i login to the account and open the folder.

The problem is if you have 3 messages in folder a with ids 1 2 and 3 and you move the message with id 3 to the folder B, the id of the message changes and we have a message with id 1 in folder B.

My goal is synchronizing the message structure in mail server with my own local server. I have to keep all message information, flags etc on my own. So on each login, i have to discover all the changes made to the messages stored in mail server.

I can get new or unread mails by:

Message messages[] = inbox.search(new FlagTerm(new Flags(Flag.RECENT), true));

or by

Message messages[] = inbox.search(new FlagTerm(new Flags(Flag.SEEN), false));

but i am not interested only to new mails, i also want to know the changes made to the old mails for example i want to get informed about that:

a mail which has been read 2 months ago, has been moved to another folder.

my idea is,

because the uids change, i can not use that for identifying mails. i think i have to use mail infos such as subject sender receive date, build an hashvalue of them and compare the hash values of messages on each login. but it will cause performance problems.

benchpresser
  • 2,171
  • 2
  • 23
  • 41

2 Answers2

4

You cannot do that in IMAP. Tracking the Message-Id header might get you half way there, but you will have to add all sorts of checks for corner cases like duplicate message IDs (yes, they are supposed to be unique) etc. Also keep in mind that what baseline IMAP gives you is a well-synchronized view to a single mailbox, not an atomic view on a set of mailboxes when combined. This means that even though a user has "moved" a message between folder A and B, it could very well be visible in both A and B for your script for a longer time period.

Some IMAP servers have added non-standard fields that you can FETCH which contain a cryptographic hash of the message payload. These are still non-standard, though.

Jan Kundrát
  • 3,700
  • 1
  • 18
  • 29
  • when using thunderbird, when i create a folder from another tool and move an very old messsage to the new folder,thunderbird gets the new folder and new message immediately when click the folders. does thunderbird know that by fetching all messages from start and appliying some sort of comparsion or does the mail server provide a specific info such as x moved from a to b? – benchpresser Apr 29 '13 at 07:34
  • i tested it with an mail account with 2000 messages. when i move a very old message from its folder to another folder (different mail client tool other than thunderbird, a web based qmail engine, squirrel mail), and when i click on the folder on thunderbird, the moved message disappers. how can thunderbird can learn that so quickliy? – benchpresser Apr 29 '13 at 09:07
  • 1
    You asked about how to detect "moves". IMAP only provides information about "message A disappearing from mailbox X" and "message B appearing in mailbox Y". There is no easy and reliable way to detect that A == B. There is the ``UIDPLUS`` extension which can help under certain circumstances, but it is far from being a complete solution. – Jan Kundrát Apr 29 '13 at 17:10
  • 2
    An email client just discovers that the message has disappeared in a folder. It does this by getting a `UID` list when you connect to it. It will redownload it in the new folder when you look at that, when it gets a `UID` list for that folder. – Max Apr 29 '13 at 23:33
2

you can try using the rfc822-header information it contains a message-id like blablabla@mail.gmail.com which should not change when mails are moved into folders. but you will have to crawl all the mail-headers of the user to sync this, at least i don't know of a way to retrieve moved messages

cproinger
  • 2,258
  • 1
  • 17
  • 33
  • 1
    was about to answer exactly that. Message-ID is exactly the right field to use... of course, wil have to account for mails being *copied* in to multiple folders, and not all messages have Message-ID's (drafts...?) – rolfl Apr 28 '13 at 18:27
  • Not all messages have ``Message-Id`` header and this header is, sadly, not guaranteed to be unique. When it comes to Internet protocols and RFCs, you cannot rely on a ``SHOULD`` wording. – Jan Kundrát Apr 30 '13 at 08:47
  • 1
    but this page says: http://stackoverflow.com/questions/219343/get-uid-for-message-from-gmail-using-javax-mail-with-imap?rq=1 Message-ID is unique in most, but not in all cases. Especially automated senders often generate he same ID, or you don't have one at all (I've also seen empty ones - Message-ID: ) – benchpresser Apr 30 '13 at 11:02