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

VB.NET
Dim success As Boolean = False

'  Assumes Chilkat is unlocked. See Global Unlock Sample.

Dim token As New Chilkat.JsonObject
success = token.LoadFile("gmail-mail-token.json")
If (success <> True) Then
    Debug.WriteLine("Could not load the protocol token from the private working directory.")
    Exit Sub
End If

If ((token.JsonTypeOf("access_token") <> 1) Or (token.StringOfEquals("access_token","",True) = True)) Then
    Debug.WriteLine("Authorize or refresh the mailbox first.")
    Exit Sub
End If

Dim mailbox As String = "user@example.com"
Dim imap As New Chilkat.Imap
imap.Ssl = True
imap.Port = 993
imap.RequireSslCertVerify = True
imap.AuthMethod = "XOAUTH2"
success = imap.Connect("imap.gmail.com")
If (success <> True) Then
    Debug.WriteLine("Could not establish the Gmail IMAP TLS connection.")
    Exit Sub
End If

success = imap.Login(mailbox,token.StringOf("access_token"))
If (success <> True) Then
    imap.Disconnect()
    Debug.WriteLine("IMAP authentication failed. Check the mailbox, scope, expiry, and account policy.")
    Exit Sub
End If

Dim folders As New Chilkat.Mailboxes
success = imap.MbxList(False,"","*",folders)
If (success <> True) Then
    imap.Logout()
    imap.Disconnect()
    Debug.WriteLine("Could not list Gmail folders.")
    Exit Sub
End If

'  LIST special-use attributes work with localized folder names. XLIST is obsolete.
Dim special As New Chilkat.JsonObject
Dim i As Integer = 0
While (i < folders.Count)
    If (folders.IsSelectable(i) = True) Then
        Debug.WriteLine(folders.GetName(i) & "  " & folders.GetFlags(i))
        If (folders.HasFlag(i,"\All") = True) Then
            special.UpdateString("allMail",folders.GetName(i))
        End If

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

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

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

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

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

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

    End If

    i = i + 1
End While
imap.Logout()
imap.Disconnect()
special.EmitCompact = False
Debug.WriteLine(special.Emit())
'  Missing keys mean the folder was not advertised. Do not substitute an English name.