title: Access to the Gmail unique message ID through Java Mail
body: <p>I am trying to get the unique message ID through JavaMail, so that I can identify mail without the context of a particular IMAP-folder later. The code I've written so far is as follows:</p>
  
  <pre><code>def fp = new FetchProfile()
  
  fp.add(FetchProfile.Item.ENVELOPE)
  fp.add(FetchProfile.Item.FLAGS)
  fp.add(FetchProfile.Item.CONTENT_INFO)
  fp.add(javax.mail.UIDFolder.FetchProfileItem.UID)
  fp.add("X-mailer")
  fp.add("X-GM-MSGID") // Attribute for retrieving the ID in the fetch (?)
  
  uIDFolder.fetch(iMAPmessages, fp);
  
  iMAPmessages.each { msg -&gt;
     println msg.dump()
  }
  </code></pre>
  
  <p>Which results in the following output (for one message):</p>
  
  <pre><code>&lt;com.sun.mail.imap.IMAPMessage@14c04449 
    bs=com.sun.mail.imap.protocol.BODYSTRUCTURE@5ac31f43
    envelope=com.sun.mail.imap.protocol.ENVELOPE@40399642 
    items=[:] 
    receivedDate=Mon Jul 28 12:18:38 MSK 2014 
    size=3147 
    peek=false 
    uid=991 
    modseq=-1  
    sectionId=null 
    type=null 
    subject=null   
    description=null 
    headersLoaded=false 
    loadedHeaders=[X-GM-MSGID:X-GM-MSGID, X-MAILER:X-mailer] 
    dh=null 
    content=null 
    contentStream=null 
    headers=javax.mail.internet.InternetHeaders@4b7e7ff
    flags=com.sun.mail.imap.protocol.FLAGS@20 
    modified=false 
    saved=true 
    cachedContent=null 
    strict=true 
    msgnum=372 
    expunged=false 
    folder=INBOX 
    session=javax.mail.Session@4f0dc86c&gt;
  </code></pre>
  
  <p>The Envelope:</p>
  
  <pre><code>&lt;com.sun.mail.imap.protocol.ENVELOPE@34ea0af2 
    msgno=371 
    date=Sun Jul 27 17:46:08 MSK 2014 
    subject=Re: Require 
    from=[From &lt;from@gmail.com&gt;]
    sender=[sender &lt;sender@gmail.com&gt;] 
    replyTo=[From &lt;from@gmail.com&gt;] 
    to=[receiver &lt;receiver@gmail.com&gt;, =?ISO-8859-1?Q?Johan_W=E4ngl=F6f?= &lt;example@gmail.com&gt;] 
    cc=null 
    bcc=null 
    inReplyTo=&lt;CAHr+Gay-ZX0U-pFuwQUMziyECTOVrLNn-XLtc643DYXwgtdDFQ@mail.gmail.com&gt;
    messageId=&lt;CAHr+GawnCxaT476K8Rm1MeQNPx8HOfL+VqM0ThMydfTNR12rBg@mail.gmail.com&gt;&gt;
  </code></pre>
  
  <p>The last attribute added to the fetch profile is the special attribute to fetch the unique ID (<a href="https://developers.google.com/gmail/imap_extensions#access_to_the_gmail_unique_message_id_x-gm-msgid" rel="nofollow">https://developers.google.com/gmail/imap_extensions#access_to_the_gmail_unique_message_id_x-gm-msgid</a>).</p>
  
  <p>The messages contains no unique message ID, as far as I can tell. Am I doing something completely wrong?</p>
  
  <p><strong>Trace of the IMAP Session</strong></p>
  
  <pre><code>* 372 FETCH (UID 991 RFC822.SIZE 3147 INTERNALDATE "28-Jul-2014 08:18:38 +0000" FLAGS (\Seen) ENVELOPE ("Mon, 28 Jul 2014 10:18:38 +0200" "Hej du" (("Emil Tholin" NIL "emtholin" "gmail.com")) (("Emil Tholin" NIL "emtholin" "gmail.com")) (("Emil Tholin" NIL "emtholin" "gmail.com")) (("Emil Tholin" NIL "emtholin" "gmail.com")) NIL NIL NIL "&lt;CADsZLRxveuyQX5gXVHvr8ca57yCLopJEc+TpuL2GS-57Kq+yqg@mail.gmail.com&gt;") BODYSTRUCTURE ((("TEXT" "PLAIN" ("CHARSET" "UTF-8") NIL NIL "7BIT" 13 1 NIL NIL NIL)("TEXT" "HTML" ("CHARSET" "UTF-8") NIL NIL "7BIT" 34 1 NIL NIL NIL) "ALTERNATIVE" ("BOUNDARY" "001a11c1250ef810aa04ff3c91b5") NIL NIL)("TEXT" "PLAIN" ("CHARSET" "US-ASCII" "NAME" "rygg.txt") NIL NIL "BASE64" 2138 0 NIL ("ATTACHMENT" ("FILENAME" "rygg.txt")) NIL) "MIXED" ("BOUNDARY" "001a11c1250ef810af04ff3c91b7") NIL NIL) BODY[HEADER.FIELDS (X-mailer X-GM-MSGID)] {4}
  )
  A4 OK Success
  A5 CLOSE
  A5 OK Returned to authenticated state. (Success)
  DEBUG IMAPS: added an Authenticated connection -- size: 1
  specificFolderHeaders() - Closing the mail connection
  A6 LOGOUT
  * BYE LOGOUT Requested
  A6 OK 73 good day (Success)
  DEBUG IMAPS: IMAPStore connection dead
  DEBUG IMAPS: IMAPStore cleanup, force false
  DEBUG IMAPS: IMAPStore cleanup done
  </code></pre>