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 Android™ Downloads
// Important: Don't forget to include the call to System.loadLibrary
// as shown at the bottom of this code sample.
package com.test;
import android.app.Activity;
import com.chilkatsoft.*;
import android.widget.TextView;
import android.os.Bundle;
public class SimpleActivity extends Activity {
private static final String TAG = "Chilkat";
// Called when the activity is first created.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
boolean success = false;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
CkJsonObject token = new CkJsonObject();
success = token.LoadFile("gmail-mail-token.json");
if (success != true) {
Log.i(TAG, "Could not load the protocol token from the private working directory.");
return;
}
if ((token.JsonTypeOf("access_token") != 1) or (token.StringOfEquals("access_token","",true) == true)) {
Log.i(TAG, "Authorize or refresh the mailbox first.");
return;
}
String mailbox = "user@example.com";
CkImap imap = new CkImap();
imap.put_Ssl(true);
imap.put_Port(993);
imap.put_RequireSslCertVerify(true);
imap.put_AuthMethod("XOAUTH2");
success = imap.Connect("imap.gmail.com");
if (success != true) {
Log.i(TAG, "Could not establish the Gmail IMAP TLS connection.");
return;
}
success = imap.Login(mailbox,token.stringOf("access_token"));
if (success != true) {
imap.Disconnect();
Log.i(TAG, "IMAP authentication failed. Check the mailbox, scope, expiry, and account policy.");
return;
}
CkMailboxes folders = new CkMailboxes();
success = imap.MbxList(false,"","*",folders);
if (success != true) {
imap.Logout();
imap.Disconnect();
Log.i(TAG, "Could not list Gmail folders.");
return;
}
// LIST special-use attributes work with localized folder names. XLIST is obsolete.
CkJsonObject special = new CkJsonObject();
int i = 0;
while ((i < folders.get_Count())) {
if (folders.IsSelectable(i) == true) {
Log.i(TAG, folders.getName(i) + " " + folders.getFlags(i));
if (folders.HasFlag(i,"\\All") == true) {
special.UpdateString("allMail",folders.getName(i));
}
if (folders.HasFlag(i,"\\Sent") == true) {
special.UpdateString("sent",folders.getName(i));
}
if (folders.HasFlag(i,"\\Drafts") == true) {
special.UpdateString("drafts",folders.getName(i));
}
if (folders.HasFlag(i,"\\Junk") == true) {
special.UpdateString("spam",folders.getName(i));
}
if (folders.HasFlag(i,"\\Trash") == true) {
special.UpdateString("trash",folders.getName(i));
}
if (folders.HasFlag(i,"\\Flagged") == true) {
special.UpdateString("starred",folders.getName(i));
}
if (folders.HasFlag(i,"\\Important") == true) {
special.UpdateString("important",folders.getName(i));
}
}
i = i + 1;
}
imap.Logout();
imap.Disconnect();
special.put_EmitCompact(false);
Log.i(TAG, special.emit());
// Missing keys mean the folder was not advertised. Do not substitute an English name.
}
static {
System.loadLibrary("chilkat");
// Note: If the incorrect library name is passed to System.loadLibrary,
// then you will see the following error message at application startup:
//"The application <your-application-name> has stopped unexpectedly. Please try again."
}
}