Gmail IMAP: Discover Special Folders
See more GMail SMTP/IMAP/POP Examples
Find Gmail special folders from the attributes returned by LIST, using MbxList and Mailboxes.HasFlag. The result maps roles such as Sent and Trash to the server-provided mailbox names. Chilkat decodes mailbox names for use with its mailbox methods.
Folder names can be localized and visibility can differ by account. The role attributes are stable; do not hard-code [Gmail]/Sent Mail or use the deprecated XLIST command. INBOX is a protocol-defined name. Missing map keys indicate folders not advertised by the server.
Use the mailbox that authorized the full-mail scope https://mail.google.com/. Obtain the protocol OAuth token and refresh it before running this example. The token is loaded from gmail-mail-token.json in a private working directory; a narrow Gmail REST token is insufficient.
Chilkat Java Downloads
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Imap;
import com.chilkatsoft.chilkat2.JsonObject;
import com.chilkatsoft.chilkat2.Mailboxes;
public class ChilkatExample {
public static void main(String[] args) {
// Assumes Chilkat is unlocked. See Global Unlock Sample.
JsonObject token = new JsonObject();
try {
token.loadFile("gmail-mail-token.json");
} catch (ChilkatException e) {
System.out.println("Could not load the protocol token from the private working directory.");
return;
}
if ((token.jsonTypeOf("access_token") != 1) || (token.stringOfEquals("access_token", "", true))) {
System.out.println("Authorize or refresh the mailbox first.");
return;
}
String mailbox = "user@example.com";
Imap imap = new Imap();
imap.setSsl(true);
imap.setPort(993);
imap.setRequireSslCertVerify(true);
imap.setAuthMethod("XOAUTH2");
try {
imap.connect("imap.gmail.com");
} catch (ChilkatException e) {
System.out.println("Could not establish the Gmail IMAP TLS connection.");
return;
}
try {
imap.login(mailbox, token.stringOf("access_token"));
} catch (ChilkatException e) {
imap.disconnect();
System.out.println("IMAP authentication failed. Check the mailbox, scope, expiry, and account policy.");
return;
}
Mailboxes folders = new Mailboxes();
try {
imap.mbxList(false, "", "*", folders);
} catch (ChilkatException e) {
imap.logout();
imap.disconnect();
System.out.println("Could not list Gmail folders.");
return;
}
// LIST special-use attributes work with localized folder names. XLIST is obsolete.
JsonObject special = new JsonObject();
int i = 0;
while (i < folders.getCount()) {
if (folders.isSelectable(i)) {
System.out.println(folders.getName(i) + " " + folders.getFlags(i));
if (folders.hasFlag(i, "\\All")) {
special.updateString("allMail", folders.getName(i));
}
if (folders.hasFlag(i, "\\Sent")) {
special.updateString("sent", folders.getName(i));
}
if (folders.hasFlag(i, "\\Drafts")) {
special.updateString("drafts", folders.getName(i));
}
if (folders.hasFlag(i, "\\Junk")) {
special.updateString("spam", folders.getName(i));
}
if (folders.hasFlag(i, "\\Trash")) {
special.updateString("trash", folders.getName(i));
}
if (folders.hasFlag(i, "\\Flagged")) {
special.updateString("starred", folders.getName(i));
}
if (folders.hasFlag(i, "\\Important")) {
special.updateString("important", folders.getName(i));
}
}
i = i + 1;
}
imap.logout();
imap.disconnect();
special.setEmitCompact(false);
System.out.println(special.emit());
// Missing keys mean the folder was not advertised. Do not substitute an English name.
}
}