views:

1524

answers:

2

I'm using javax.mail to retrieve mails from GMail using IMAP. I want to also obtain the tags being applied to each mail, so I'm iterating over all the folders in the store (which actually are tags) and downloading mails for each other.

I was using the method getUID of ImapFolder to obtain the UID for each message, and then compare them to recognise the same mail along different folders.

For example, if I had the mail "Hello" tagged with "A" and "B", I would iterate through folders "Inbox", "A" and "B" and get the same mail from each folder; so I could keep a single copy tagged with "Inbox", "A" and "B".

However, the getUID method returns a folder-relative ID, so the same mail may have different IDs in different folders, or different mails may have the same ID in different folders. In the example. "HellO" may appear as mail 1 in "A" and as mail 2 in "B".

Is there a way of obtaining a store-relative id for each mail, independent of the folder? Or a better way to obtain the tags attached to a single mail?

A: 

I think the only way is to use a hash that you calculate yourself based on the message headers.

Stefan Schultze
I think it will have to come to it, I wanted to avoid it if possible, but it seems like there is no other way. Btw, headers should include Subject, From, To, Cc, Date and... anything else to uniquely identify a mail?
Santiago Palladino
I'd include the Message-ID header field in any case.
Stefan Schultze
+1  A: 

Got the answer from gmail forums: there is a "message-id" header that contains the value I was looking for.

Santiago Palladino
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: )
Stefan Schultze