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

Classic ASP
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<%
success = 0

'  Assumes Chilkat is unlocked. See Global Unlock Sample.

set token = Server.CreateObject("Chilkat.JsonObject")
success = token.LoadFile("gmail-mail-token.json")
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Could not load the protocol token from the private working directory.") & "</pre>"
    Response.End
End If

If ((token.JsonTypeOf("access_token") <> 1) Or (token.StringOfEquals("access_token","",1) = 1)) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Authorize or refresh the mailbox first.") & "</pre>"
    Response.End
End If

mailbox = "user@example.com"
set imap = Server.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
    Response.Write "<pre>" & Server.HTMLEncode( "Could not establish the Gmail IMAP TLS connection.") & "</pre>"
    Response.End
End If

success = imap.Login(mailbox,token.StringOf("access_token"))
If (success <> 1) Then
    success = imap.Disconnect()
    Response.Write "<pre>" & Server.HTMLEncode( "IMAP authentication failed. Check the mailbox, scope, expiry, and account policy.") & "</pre>"
    Response.End
End If

set folders = Server.CreateObject("Chilkat.Mailboxes")
success = imap.MbxList(0,"","*",folders)
If (success <> 1) Then
    success = imap.Logout()
    success = imap.Disconnect()
    Response.Write "<pre>" & Server.HTMLEncode( "Could not list Gmail folders.") & "</pre>"
    Response.End
End If

'  LIST special-use attributes work with localized folder names. XLIST is obsolete.
set special = Server.CreateObject("Chilkat.JsonObject")
i = 0
Do While (i < folders.Count)
    If (folders.IsSelectable(i) = 1) Then
        Response.Write "<pre>" & Server.HTMLEncode( folders.GetName(i) & "  " & folders.GetFlags(i)) & "</pre>"
        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
Response.Write "<pre>" & Server.HTMLEncode( special.Emit()) & "</pre>"
'  Missing keys mean the folder was not advertised. Do not substitute an English name.

%>
</body>
</html>