Sample code for 30+ languages & platforms
VBScript 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 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 load the protocol token 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("Authorize or refresh the mailbox 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, scope, expiry, and account policy.")
    WScript.Quit
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 Gmail folders.")
    WScript.Quit
End If

'  LIST special-use attributes work with localized folder names. XLIST is obsolete.
set special = CreateObject("Chilkat.JsonObject")
i = 0
Do While (i < folders.Count)
    If (folders.IsSelectable(i) = 1) Then
        outFile.WriteLine(folders.GetName(i) & "  " & folders.GetFlags(i))
        If (folders.HasFlag(i,"\All") = 1) Then
            success = special.UpdateString("allMail",folders.GetName(i))
        End If

        If (folders.HasFlag(i,"\Sent") = 1) Then
            success = special.UpdateString("sent",folders.GetName(i))
        End If

        If (folders.HasFlag(i,"\Drafts") = 1) Then
            success = special.UpdateString("drafts",folders.GetName(i))
        End If

        If (folders.HasFlag(i,"\Junk") = 1) Then
            success = special.UpdateString("spam",folders.GetName(i))
        End If

        If (folders.HasFlag(i,"\Trash") = 1) Then
            success = special.UpdateString("trash",folders.GetName(i))
        End If

        If (folders.HasFlag(i,"\Flagged") = 1) Then
            success = special.UpdateString("starred",folders.GetName(i))
        End If

        If (folders.HasFlag(i,"\Important") = 1) Then
            success = special.UpdateString("important",folders.GetName(i))
        End If

    End If

    i = i + 1
Loop
success = imap.Logout()
success = imap.Disconnect()
special.EmitCompact = 0
outFile.WriteLine(special.Emit())
'  Missing keys mean the folder was not advertised. Do not substitute an English name.

outFile.Close