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 Swift Downloads
func chilkatTest() {
var success: Bool = false
// Assumes Chilkat is unlocked. See Global Unlock Sample.
let token = CkoJsonObject()!
success = token.loadFile(path: "gmail-mail-token.json")
if success != true {
print("Could not load the protocol token from the private working directory.")
return
}
if (token.jsonType(of: "access_token").intValue != 1) || (token.string(ofEquals: "access_token", value: "", caseSensitive: true) == true) {
print("Authorize or refresh the mailbox first.")
return
}
var mailbox: String? = "user@example.com"
let imap = CkoImap()!
imap.ssl = true
imap.port = 993
imap.requireSslCertVerify = true
imap.authMethod = "XOAUTH2"
success = imap.connect(hostname: "imap.gmail.com")
if success != true {
print("Could not establish the Gmail IMAP TLS connection.")
return
}
success = imap.login(login: mailbox, password: token.string(of: "access_token"))
if success != true {
imap.disconnect()
print("IMAP authentication failed. Check the mailbox, scope, expiry, and account policy.")
return
}
let folders = CkoMailboxes()!
success = imap.mbxList(subscribed: false, reference: "", mbxPattern: "*", mboxes: folders)
if success != true {
imap.logout()
imap.disconnect()
print("Could not list Gmail folders.")
return
}
// LIST special-use attributes work with localized folder names. XLIST is obsolete.
let special = CkoJsonObject()!
var i: Int = 0
while (i < folders.count.intValue) {
if folders.isSelectable(index: i) == true {
print("\(folders.getName(index: i)!) \(folders.getFlags(index: i)!)")
if folders.hasFlag(index: i, flagName: "\\All") == true {
special.updateString(jsonPath: "allMail", value: folders.getName(index: i))
}
if folders.hasFlag(index: i, flagName: "\\Sent") == true {
special.updateString(jsonPath: "sent", value: folders.getName(index: i))
}
if folders.hasFlag(index: i, flagName: "\\Drafts") == true {
special.updateString(jsonPath: "drafts", value: folders.getName(index: i))
}
if folders.hasFlag(index: i, flagName: "\\Junk") == true {
special.updateString(jsonPath: "spam", value: folders.getName(index: i))
}
if folders.hasFlag(index: i, flagName: "\\Trash") == true {
special.updateString(jsonPath: "trash", value: folders.getName(index: i))
}
if folders.hasFlag(index: i, flagName: "\\Flagged") == true {
special.updateString(jsonPath: "starred", value: folders.getName(index: i))
}
if folders.hasFlag(index: i, flagName: "\\Important") == true {
special.updateString(jsonPath: "important", value: folders.getName(index: i))
}
}
i = i + 1
}
imap.logout()
imap.disconnect()
special.emitCompact = false
print("\(special.emit()!)")
// Missing keys mean the folder was not advertised. Do not substitute an English name.
}