Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determining which Gmail Tab an email is placed under, using the Javamail API

I am working on application that can determine which Gmail Tab an email is placed under (ie Social, Promotions, etc). Is there any way of determining which Gmail Tab an email is placed into with the Javamail API? Or are there some other applications that can determine this?

Thanks in advance.

like image 506
user2029861 Avatar asked Nov 12 '22 01:11

user2029861


1 Answers

It is not possible to determine the category/tab with the Javamail API because Google do not integrate the information with the IMAP protocol. The category/tab is only available via Google's proprietary GMail APIs.

Image of categories settings in GMail

In the GMail settings under labels, I was able to get categories to show up as folders in the GMail web client by clicking on 'show' next to categories, however they did not come down in the IMAP request. It must be by design because there is no "Show in IMAP" option provided for the categories.

In the existing answer it is mentioned that the GMail IMAP extensions can be used to access X-GM-THRID and X-GM-LABELS

These extensions are explained in Google's GMail IMAP docs: https://developers.google.com/gmail/imap/imap-extensions

  1. X-GM-LABELS -> "Gmail treats labels as folders for the purposes of IMAP."

    a010 FETCH 1:4 (X-GM-LABELS)

    • 1 FETCH (X-GM-LABELS (\Inbox \Sent Important "Muy Importante"))
    • 2 FETCH (X-GM-LABELS (foo))
    • 3 FETCH (X-GM-LABELS ())
    • 4 FETCH (X-GM-LABELS (\Drafts)) a010 OK FETCH (Success)
  2. X-GM-THRID -> "Gmail provides a thread ID to associate groups of messages in the same manner as in the Gmail web interface."

    a008 FETCH 1:4 (X-GM-THRID)

    • 1 FETCH (X-GM-THRID 1278455344230334865)
    • 2 FETCH (X-GM-THRID 1266894439832287888)
    • 3 FETCH (X-GM-THRID 1266894439832287888)
    • 4 FETCH (X-GM-THRID 1266894439832287888) a008 OK FETCH (Success)

But neither of these extensions specifies the category to which an email belongs.

There are however two alternative solutions that will allow Javamail to work with categories indirectly:

  1. Use a filter that associates labels with each category -> Causes an IMAP folder to be created for each category
  2. Create a filter and associate it with "Skip inbox" to exclude certain emails from coming down in the IMAP request if that is your goal

Procedure for changing categories into labels/IMAP-folders:

  1. Go to GMail Settings
  2. Click on "See All Settings"
  3. Click on "Filters and Blocked Addresses"
  4. Click on "Create new Filter"
  5. On the line "Includes the words" enter "category:primary"
  6. Click "Create the Filter"
  7. Tick "Apply the Label"
  8. Choose "New label..."
  9. Enter the label name [This will be the IMAP folder name]

Screenshots for the procedure:

Procedure

The procedure for skipping the inbox

  1. Same procedure as above
  2. In step 7, choose "Skip in the inbox" instead of "Apply the Label"

The list of possible categories to filter on:

  • category:primary
  • category:social
  • category:promotions
  • category:forums
  • category:updates
like image 50
Elletlar Avatar answered Nov 14 '22 21:11

Elletlar