Sample code for 30+ languages & platforms
Java Requires Chilkat v11.0.0+

IMAP Search with THREAD Semantics

See more IMAP Examples

Demonstrates how to search an IMAP mailbox and return message numbers grouped together in parent/child relationships based on which messages are replies to others.

Chilkat Java Downloads

Chilkat2 Java
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Imap;
import com.chilkatsoft.chilkat2.JsonArray;
import com.chilkatsoft.chilkat2.JsonObject;

public class ChilkatExample {

    public static void main(String[] args) {
        // This example requires the Chilkat API to have been previously unlocked.
        // See Global Unlock Sample for sample code.

        Imap imap = new Imap();

        // Connect to your IMAP server and authenticate..
        imap.setSsl(true);
        imap.setPort(993);
        try {
            imap.connect("imap.mail.us-west-2.awsapps.com");

            imap.login("myLogin", "myPassword");

            // Select a mailbox
            imap.selectMailbox("Inbox");
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        // Search for all message having the letter 'a' somewhere in the Subject,
        // and return the messages as JSON.
        JsonObject json = new JsonObject();
        try {
            imap.queryThread("REFERENCES", "SUBJECT a", true, json);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        // The IMAP server will return a raw response with a format such as this:  (2)(3 6 (4 23)(44 7 96))

        // In tree form, it's like this:
        //
        //             -- 2
        //             -- 3
        //                 \-- 6
        //                    |-- 4
        //                    |      \-- 23
        //                    |
        //                    |-- 44
        //                               \-- 7
        //                                       \-- 96
        //

        // It means there are 2 main threads returned, but the 2nd thread splits into two sub-threads.
        // In total, we can think of it as 3 threads -- 2 main threads (with no parents) and one sub-thread w/ a parent.
        //
        // - The 1st thread contains the message 2, and has no parent thread.
        // - The 2nd thread contains the messages 3, 6, 4, 23, and has no parent thread.
        // - The 3rd thread contains the messages 44, 7, 96 and the parent thread is message 6.
        //

        // (Yes, this is all highly confusing...)

        // Chilkat will return the above sample response as JSON that looks like this:

        // {
        //   "threads": [
        //     [2],
        //     [3, 6, [4, 23], [44, 7, 96]]
        //   ]
        // }
        //

        // Use this online tool to generate parsing code from sample JSON:
        // Generate Parsing Code from JSON
        // In this case, the online tool can help you get a feel for how to write the JSON parsing code..

        int numThreads = json.sizeOfArray("threads");
        System.out.println("The total number of top-level threads is " + numThreads);

        // Let's say we wanted to get the messages in the thread 3, 6, 4, 23.
        // We always follow the 1st branch to the bottom, ignoring the other branches.
        // For example, if we had  [3, 5, [4, 23, [55, 56, 57], [68, 69]], [44, 7, 96]]
        // then the thread would be 3, 5, 4, 43, 55, 56, 57

        // For testing, let's substitute the response from the IMAP server with this sample:
        json.load("{\"threads\": [[2], [3, 5, [4, 23, [55, 56, 57], [68, 69]], [44, 7, 96]]]}");

        // Begin with the 2nd top-level thread, which is at index 1.
        System.out.println("Following the 2nd top level thread...");
        JsonArray arr = json.arrayOf("threads[1]");
        int threadSize = arr.getSize();
        int i = 0;
        while (i < threadSize) {
            // Do we have an array or integer at this position?
            if (arr.typeAt(i) == 4) {
                // This is a sub-array.
                JsonArray subArr = arr.arrayAt(i);
                arr.close();
                // Follow the sub-array starting at the 1st position..
                arr = subArr;
                i = 0;
                threadSize = arr.getSize();
            } else {
                // Must be a single integer.
                System.out.println(arr.intAt(i));
                i = i + 1;
            }
        }

        // The output is:
        //
        // Following the 2nd top level thread...
        // 3
        // 5
        // 4
        // 23
        // 55
        // 56
        // 57
    }
}