Sample code for 30+ languages & platforms
VBScript 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 VBScript Downloads

VBScript
Dim fso, outFile
Set fso = CreateObject("Scripting.FileSystemObject")
'Create a Unicode (utf-16) output text file.
Set outFile = fso.CreateTextFile("output.txt", True, True)

success = 0

'  Assumes Chilkat is unlocked. See Global Unlock Sample.

set token = CreateObject("Chilkat.JsonObject")
success = token.LoadFile("gmail-mail-token.json")
If (success <> 1) Then
    outFile.WriteLine("Could not read gmail-mail-token.json from the private working directory.")
    WScript.Quit
End If

If ((token.JsonTypeOf("access_token") <> 1) Or (token.StringOfEquals("access_token","",1) = 1)) Then
    outFile.WriteLine("An OAuth access token is required. Authorize or refresh the account first.")
    WScript.Quit
End If

mailbox = "user@example.com"
set imap = CreateObject("Chilkat.Imap")
imap.Ssl = 1
imap.Port = 993
imap.RequireSslCertVerify = 1
imap.AuthMethod = "XOAUTH2"
success = imap.Connect("imap.gmail.com")
If (success <> 1) Then
    outFile.WriteLine("Could not establish the Gmail IMAP TLS connection.")
    WScript.Quit
End If

success = imap.Login(mailbox,token.StringOf("access_token"))
If (success <> 1) Then
    success = imap.Disconnect()
    outFile.WriteLine("IMAP authentication failed. Check the mailbox, token scope, expiry, and Workspace policy.")
    WScript.Quit
End If

success = imap.ExamineMailbox("INBOX")
If (success <> 1) Then
    success = imap.Logout()
    success = imap.Disconnect()
    outFile.WriteLine("Could not examine INBOX.")
    WScript.Quit
End If

'  Capture only the EXAMINE response, after authentication has completed.
outFile.WriteLine(imap.LastResponse)
'  Sample flags without marking messages read. Sequence numbers are session-local.
If (imap.NumMessages > 0) Then
    sequence = 1
    flags = imap.FetchFlags(sequence,0)
    If (imap.LastMethodSuccess <> 1) Then
        success = imap.Logout()
        success = imap.Disconnect()
        outFile.WriteLine("Could not fetch flags; the message may have changed or disappeared.")
        WScript.Quit
    End If

    outFile.WriteLine("Sequence " & sequence & ": " & flags)
End If

set folders = CreateObject("Chilkat.Mailboxes")
success = imap.MbxList(0,"","*",folders)
If (success <> 1) Then
    success = imap.Logout()
    success = imap.Disconnect()
    outFile.WriteLine("Could not list folders.")
    WScript.Quit
End If

i = 0
Do While (i < folders.Count)
    If (folders.HasFlag(i,"\Junk") = 1) Then
        outFile.WriteLine("Spam folder: " & folders.GetName(i))
    End If

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

outFile.Close