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

PowerBuilder
integer li_rc
long li_Success
oleobject loo_Token
string ls_Mailbox
oleobject loo_Imap
long li_Sequence
string ls_Flags
oleobject loo_Folders
long i

li_Success = 0

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

loo_Token = create oleobject
li_rc = loo_Token.ConnectToNewObject("Chilkat.JsonObject")
if li_rc < 0 then
    destroy loo_Token
    MessageBox("Error","Connecting to COM object failed")
    return
end if
li_Success = loo_Token.LoadFile("gmail-mail-token.json")
if li_Success <> 1 then
    Write-Debug "Could not read gmail-mail-token.json from the private working directory."
    destroy loo_Token
    return
end if

if (loo_Token.JsonTypeOf("access_token") <> 1) OR (loo_Token.StringOfEquals("access_token","",1) = 1) then
    Write-Debug "An OAuth access token is required. Authorize or refresh the account first."
    destroy loo_Token
    return
end if

ls_Mailbox = "user@example.com"
loo_Imap = create oleobject
li_rc = loo_Imap.ConnectToNewObject("Chilkat.Imap")

loo_Imap.Ssl = 1
loo_Imap.Port = 993
loo_Imap.RequireSslCertVerify = 1
loo_Imap.AuthMethod = "XOAUTH2"
li_Success = loo_Imap.Connect("imap.gmail.com")
if li_Success <> 1 then
    Write-Debug "Could not establish the Gmail IMAP TLS connection."
    destroy loo_Token
    destroy loo_Imap
    return
end if

li_Success = loo_Imap.Login(ls_Mailbox,loo_Token.StringOf("access_token"))
if li_Success <> 1 then
    loo_Imap.Disconnect()
    Write-Debug "IMAP authentication failed. Check the mailbox, token scope, expiry, and Workspace policy."
    destroy loo_Token
    destroy loo_Imap
    return
end if

li_Success = loo_Imap.ExamineMailbox("INBOX")
if li_Success <> 1 then
    loo_Imap.Logout()
    loo_Imap.Disconnect()
    Write-Debug "Could not examine INBOX."
    destroy loo_Token
    destroy loo_Imap
    return
end if

//  Capture only the EXAMINE response, after authentication has completed.
Write-Debug loo_Imap.LastResponse
//  Sample flags without marking messages read. Sequence numbers are session-local.
if loo_Imap.NumMessages > 0 then
    li_Sequence = 1
    ls_Flags = loo_Imap.FetchFlags(li_Sequence,0)
    if loo_Imap.LastMethodSuccess <> 1 then
        loo_Imap.Logout()
        loo_Imap.Disconnect()
        Write-Debug "Could not fetch flags; the message may have changed or disappeared."
        destroy loo_Token
        destroy loo_Imap
        return
    end if

    Write-Debug "Sequence " + string(li_Sequence) + ": " + ls_Flags
end if

loo_Folders = create oleobject
li_rc = loo_Folders.ConnectToNewObject("Chilkat.Mailboxes")

li_Success = loo_Imap.MbxList(0,"","*",loo_Folders)
if li_Success <> 1 then
    loo_Imap.Logout()
    loo_Imap.Disconnect()
    Write-Debug "Could not list folders."
    destroy loo_Token
    destroy loo_Imap
    destroy loo_Folders
    return
end if

i = 0
do while (i < loo_Folders.Count)
    if loo_Folders.HasFlag(i,"\Junk") = 1 then
        Write-Debug "Spam folder: " + loo_Folders.GetName(i)
    end if

    i = i + 1
loop
loo_Imap.Logout()
loo_Imap.Disconnect()


destroy loo_Token
destroy loo_Imap
destroy loo_Folders