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

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.

Reference documentation

Chilkat Xojo Plugin Downloads

Xojo Plugin
Dim success As Boolean
success = False

//  Assumes Chilkat is unlocked. See Global Unlock Sample.

Dim token As New Chilkat.JsonObject
success = token.LoadFile("gmail-mail-token.json")
If (success <> True) Then
    System.DebugLog("Could not read gmail-mail-token.json from the private working directory.")
    Return
End If

If ((token.JsonTypeOf("access_token") <> 1) Or (token.StringOfEquals("access_token","",True) = True)) Then
    System.DebugLog("An OAuth access token is required. Authorize or refresh the account first.")
    Return
End If

Dim mailbox As String
mailbox = "user@example.com"
Dim imap As New Chilkat.Imap
imap.Ssl = True
imap.Port = 993
imap.RequireSslCertVerify = True
imap.AuthMethod = "XOAUTH2"
success = imap.Connect("imap.gmail.com")
If (success <> True) Then
    System.DebugLog("Could not establish the Gmail IMAP TLS connection.")
    Return
End If

success = imap.Login(mailbox,token.StringOf("access_token"))
If (success <> True) Then
    success = imap.Disconnect()
    System.DebugLog("IMAP authentication failed. Check the mailbox, token scope, expiry, and Workspace policy.")
    Return
End If

success = imap.ExamineMailbox("INBOX")
If (success <> True) Then
    success = imap.Logout()
    success = imap.Disconnect()
    System.DebugLog("Could not examine INBOX.")
    Return
End If

//  Capture only the EXAMINE response, after authentication has completed.
System.DebugLog(imap.LastResponse)
//  Sample flags without marking messages read. Sequence numbers are session-local.
If (imap.NumMessages > 0) Then
    Dim sequence As UInt32
    sequence = 1
    Dim flags As String
    flags = imap.FetchFlags(sequence,False)
    If (imap.LastMethodSuccess <> True) Then
        success = imap.Logout()
        success = imap.Disconnect()
        System.DebugLog("Could not fetch flags; the message may have changed or disappeared.")
        Return
    End If

    System.DebugLog("Sequence " + Str(sequence) + ": " + flags)
End If

Dim folders As New Chilkat.Mailboxes
success = imap.MbxList(False,"","*",folders)
If (success <> True) Then
    success = imap.Logout()
    success = imap.Disconnect()
    System.DebugLog("Could not list folders.")
    Return
End If

Dim i As Int32
i = 0
While (i < folders.Count)
    If (folders.HasFlag(i,"\Junk") = True) Then
        System.DebugLog("Spam folder: " + folders.GetName(i))
    End If

    i = i + 1
Wend
success = imap.Logout()
success = imap.Disconnect()