Search code examples
javagmailjakarta-mail

JavaMail get the URL of the Email - Gmail


I was wondering if it is possible to get the URL of an Email I am fetching via JavaMail.

In the Gmail Webapp I have e.g. an URL of the following type:

https://mail.google.com/mail/u/0/#inbox/14e0bbd1484a035a

I have build successfully an imap connection to the mailfolder and get the subject, body etc.

mailSubject = msg.getSubject();

Can I also get the URL from the Email with the same Message object?

Thank you!


Solution

  • You can derive the URL from Gmail unique message ID (not to be confused with IMAP unique ID):

    1. Retrieve Gmail unique message ID (MSGID) using a recent version of JavaMail (>= 1.5.2) - see the sample code.
    2. Convert the ID to a lowercase hex variant (1504408783077114714 becomes 14e0bbd1484a035a).
    3. Append it to "https://mail.google.com/mail/u/0/#inbox/" to get the URL.