Gmail IMAP: Inspect Flags and Locate the Spam Folder
See more IMAP Examples
Inspect the current INBOX flags and discover Gmail's Spam folder through its \Junk special-use attribute. The example reads the first message's flags, if present, and makes no mailbox changes.
Client-specific Junk/NonJunk keywords are not a portable spam verdict. A mailbox's \Junk LIST attribute identifies a special folder; it is not a message flag. Use current server responses rather than a historical transcript. For Gmail label membership, see X-GM-LABELS.
First authorize the mailbox for https://mail.google.com/ and refresh the token as needed. Read the current token from gmail-mail-token.json in a private working directory. Narrow Gmail REST scopes do not authorize these protocols. Keep the token and authentication logs private.
See the Gmail SMTP, IMAP, and POP3 guide for current authentication choices and Gmail-specific examples.
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 read gmail-mail-token.json from the private working directory.")
return
}
if (token.jsonType(of: "access_token").intValue != 1) || (token.string(ofEquals: "access_token", value: "", caseSensitive: true) == true) {
print("An OAuth access token is required. Authorize or refresh the account 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, token scope, expiry, and Workspace policy.")
return
}
success = imap.examineMailbox(mailbox: "INBOX")
if success != true {
imap.logout()
imap.disconnect()
print("Could not examine INBOX.")
return
}
// Capture only the EXAMINE response, after authentication has completed.
print("\(imap.lastResponse!)")
// Sample flags without marking messages read. Sequence numbers are session-local.
if imap.numMessages.intValue > 0 {
var sequence: UInt32 = 1
var flags: String? = imap.fetchFlags(msgId: sequence, bUid: false)
if imap.lastMethodSuccess != true {
imap.logout()
imap.disconnect()
print("Could not fetch flags; the message may have changed or disappeared.")
return
}
print("Sequence \(sequence): \(flags!)")
}
let folders = CkoMailboxes()!
success = imap.mbxList(subscribed: false, reference: "", mbxPattern: "*", mboxes: folders)
if success != true {
imap.logout()
imap.disconnect()
print("Could not list folders.")
return
}
var i: Int = 0
while (i < folders.count.intValue) {
if folders.hasFlag(index: i, flagName: "\\Junk") == true {
print("Spam folder: \(folders.getName(index: i)!)")
}
i = i + 1
}
imap.logout()
imap.disconnect()
}