JavaMail uses the information in the IMAP ENVELOPE as the return values for methods such as getMessageID. Normally that will be the same data you would get using getHeader("Message-ID"), but if the Message-ID header is missing in the original message the server might "make up" a value to return in the ENVELOPE.
Getting envelope object using JavaMail
-
02-10-2022 - |
Pergunta
I'm building a mail client for Android and using JavaMail to get the messages via Imap Protocol. I'm saving the Message-Id header for every message I reveive and I came across a message that didn't contain any Message-Id header in it's original message but when I receive the message from Gmail via Imap I get a field called Envalope and this field happens to contain all the data I need including the Message-Id.
The problem is that I can't reach that Envelope object no matter what I do and I was wondering if someone has already done that and can throw some tips.
The original message came with the following content:
MIME-Version: 1.0
From: "Mailbox Support" <support@mailboxapp.com>
To: ******
Subject: Tips for Using Mailbox in Gmail
Content-Type: multipart/alternative;
boundary="----mailcomposer-?=_1-1369421942466"
------mailcomposer-?=_1-1369421942466
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: quoted-printable
[some text]
------mailcomposer-?=_1-1369421942466
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: quoted-printable
[Some rich text]
------mailcomposer-?=_1-1369421942466--
Thanks a lot
Solução
Outras dicas
get hold of folder which you want to read like store.getFolder("INBOX") and assign it to a variable which is of type Folder
get all messages from above folder variable as folder.getMessages()
when you loop over on each message cast message to (IMAPMessage) and assign it to a variable of type IMAPMessage
as _getEnvelope is private method in IMAPMessage, you need to you reflection to get access to it as follow
Method method = IMAPMessage.class.getDeclaredMethod("_getEnvelope", null); method.setAccessible(true); ENVELOPE envelope = (ENVELOPE) method.invoke(imapMessage, null);
from envelope object you can access to messageId and rest of the details