Sample code for 30+ languages & platforms
PowerBuilder Requires Chilkat v11.1.0+

Gmail IMAP: Discover Special Folders

See more GMail SMTP/IMAP/POP Examples

Find Gmail special folders from the attributes returned by LIST, using MbxList and Mailboxes.HasFlag. The result maps roles such as Sent and Trash to the server-provided mailbox names. Chilkat decodes mailbox names for use with its mailbox methods.

Folder names can be localized and visibility can differ by account. The role attributes are stable; do not hard-code [Gmail]/Sent Mail or use the deprecated XLIST command. INBOX is a protocol-defined name. Missing map keys indicate folders not advertised by the server.

Use the mailbox that authorized the full-mail scope https://mail.google.com/. Obtain the protocol OAuth token and refresh it before running this example. The token is loaded from gmail-mail-token.json in a private working directory; a narrow Gmail REST token is insufficient.

Google documentation

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
long li_Success
oleobject loo_Token
string ls_Mailbox
oleobject loo_Imap
oleobject loo_Folders
oleobject loo_Special
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 load the protocol token 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 "Authorize or refresh the mailbox 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, scope, expiry, and account policy."
    destroy loo_Token
    destroy loo_Imap
    return
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 Gmail folders."
    destroy loo_Token
    destroy loo_Imap
    destroy loo_Folders
    return
end if

//  LIST special-use attributes work with localized folder names. XLIST is obsolete.
loo_Special = create oleobject
li_rc = loo_Special.ConnectToNewObject("Chilkat.JsonObject")

i = 0
do while (i < loo_Folders.Count)
    if loo_Folders.IsSelectable(i) = 1 then
        Write-Debug loo_Folders.GetName(i) + "  " + loo_Folders.GetFlags(i)
        if loo_Folders.HasFlag(i,"\All") = 1 then
            loo_Special.UpdateString("allMail",loo_Folders.GetName(i))
        end if

        if loo_Folders.HasFlag(i,"\Sent") = 1 then
            loo_Special.UpdateString("sent",loo_Folders.GetName(i))
        end if

        if loo_Folders.HasFlag(i,"\Drafts") = 1 then
            loo_Special.UpdateString("drafts",loo_Folders.GetName(i))
        end if

        if loo_Folders.HasFlag(i,"\Junk") = 1 then
            loo_Special.UpdateString("spam",loo_Folders.GetName(i))
        end if

        if loo_Folders.HasFlag(i,"\Trash") = 1 then
            loo_Special.UpdateString("trash",loo_Folders.GetName(i))
        end if

        if loo_Folders.HasFlag(i,"\Flagged") = 1 then
            loo_Special.UpdateString("starred",loo_Folders.GetName(i))
        end if

        if loo_Folders.HasFlag(i,"\Important") = 1 then
            loo_Special.UpdateString("important",loo_Folders.GetName(i))
        end if

    end if

    i = i + 1
loop
loo_Imap.Logout()
loo_Imap.Disconnect()
loo_Special.EmitCompact = 0
Write-Debug loo_Special.Emit()
//  Missing keys mean the folder was not advertised. Do not substitute an English name.


destroy loo_Token
destroy loo_Imap
destroy loo_Folders
destroy loo_Special