Mailing with IMAP: how to detect if a message has been moved from one folder to another?

Using the JavaMail and IMAP APIs, I want to know that a message has been moved from folder a to b. how can i do this without adding listeners? I want to be able to detect message changes when I log into an account and open a folder.

The problem is that you have 3 messages in folder a with IDs 1 2 and 3 and you move the message with ID 3 to folder B, the message ID changes and we have a message with ID 1 in folder B.

My goal is to sync the message structure on the mail server with my own local server. I have to store all information about messages, flags, etc. On your own. Therefore, every time I log in, I have to detect all the changes made to the messages stored on the mail server.

I may receive new or unread emails:

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

      

or

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

      

but I am not only interested in new emails, I also want to know the changes made to old emails, for example, I want to be informed about this:

Mail that was read 2 months ago has been moved to another folder.

My idea:

because the uids change, I cannot use that to identify emails. i think i need to use postal information like subject sender received date, construct hash value and compare message hash values ​​on every login. but this will cause performance issues.

+1


source to share


2 answers


You cannot do this in IMAP. Header tracking Message-Id

might get you halfway there, but you'll have to add all sorts of checks for corner cases like duplicate post IDs (yes, they have to be unique), etc. Also keep in mind that Basic IMAP gives you a well-synchronized view of a single mailbox, not an atomic view of a set of mailboxes when combined. This means that even if the user "moved" the message between folders A and B, it can be clearly visible in both A and B to your script for a longer period of time.



Some IMAP servers have added custom fields that you can FETCH

that contain the cryptographic hash of the message payload. However, they are still non-standard.

+4


source


you can try using rfc822 header information which contains the message id like blablabla@mail.gmail.com which shouldn't change as mail moves to folders. but you will have to scan all the user's mail headers to keep them in sync, at least i dont know a way to get moved messages.



+2


source







All Articles