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 AutoIt Downloads
Local $bSuccess = False
; Assumes Chilkat is unlocked. See Global Unlock Sample.
$oToken = ObjCreate("Chilkat.JsonObject")
$bSuccess = $oToken.LoadFile("gmail-mail-token.json")
If ($bSuccess <> True) Then
ConsoleWrite("Could not read gmail-mail-token.json from the private working directory." & @CRLF)
Exit
EndIf
If (($oToken.JsonTypeOf("access_token") <> 1) Or ($oToken.StringOfEquals("access_token","",True) = True)) Then
ConsoleWrite("An OAuth access token is required. Authorize or refresh the account first." & @CRLF)
Exit
EndIf
Local $sMailbox = "user@example.com"
$oImap = ObjCreate("Chilkat.Imap")
$oImap.Ssl = True
$oImap.Port = 993
$oImap.RequireSslCertVerify = True
$oImap.AuthMethod = "XOAUTH2"
$bSuccess = $oImap.Connect("imap.gmail.com")
If ($bSuccess <> True) Then
ConsoleWrite("Could not establish the Gmail IMAP TLS connection." & @CRLF)
Exit
EndIf
$bSuccess = $oImap.Login($sMailbox,$oToken.StringOf("access_token"))
If ($bSuccess <> True) Then
$oImap.Disconnect()
ConsoleWrite("IMAP authentication failed. Check the mailbox, token scope, expiry, and Workspace policy." & @CRLF)
Exit
EndIf
$bSuccess = $oImap.ExamineMailbox("INBOX")
If ($bSuccess <> True) Then
$oImap.Logout()
$oImap.Disconnect()
ConsoleWrite("Could not examine INBOX." & @CRLF)
Exit
EndIf
; Capture only the EXAMINE response, after authentication has completed.
ConsoleWrite($oImap.LastResponse & @CRLF)
; Sample flags without marking messages read. Sequence numbers are session-local.
If ($oImap.NumMessages > 0) Then
Local $iSequence = 1
Local $sFlags = $oImap.FetchFlags($iSequence,False)
If ($oImap.LastMethodSuccess <> True) Then
$oImap.Logout()
$oImap.Disconnect()
ConsoleWrite("Could not fetch flags; the message may have changed or disappeared." & @CRLF)
Exit
EndIf
ConsoleWrite("Sequence " & $iSequence & ": " & $sFlags & @CRLF)
EndIf
$oFolders = ObjCreate("Chilkat.Mailboxes")
$bSuccess = $oImap.MbxList(False,"","*",$oFolders)
If ($bSuccess <> True) Then
$oImap.Logout()
$oImap.Disconnect()
ConsoleWrite("Could not list folders." & @CRLF)
Exit
EndIf
Local $i = 0
While ($i < $oFolders.Count)
If ($oFolders.HasFlag($i,"\Junk") = True) Then
ConsoleWrite("Spam folder: " & $oFolders.GetName($i) & @CRLF)
EndIf
$i = $i + 1
Wend
$oImap.Logout()
$oImap.Disconnect()