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.
Chilkat B4X Downloads
Dim success As Boolean = False
' Assumes Chilkat is unlocked. See Global Unlock Sample.
Dim token As ChilkatJsonObject
token.Initialize
success = token.LoadFile("gmail-mail-token.json")
If success <> True Then
Log("Could not load the protocol token from the private working directory.")
Return
End If
If (token.JsonTypeOf("access_token") <> 1) Or (token.StringOfEquals("access_token", "", True) = True) Then
Log("Authorize or refresh the mailbox first.")
Return
End If
Dim mailbox As String = "user@example.com"
Dim imap As ChilkatImap
imap.Initialize("imap")
imap.Ssl = True
imap.Port = 993
imap.RequireSslCertVerify = True
imap.AuthMethod = "XOAUTH2"
success = imap.Connect("imap.gmail.com")
If success <> True Then
Log("Could not establish the Gmail IMAP TLS connection.")
Return
End If
success = imap.Login(mailbox, token.StringOf("access_token"))
If success <> True Then
imap.Disconnect
Log("IMAP authentication failed. Check the mailbox, scope, expiry, and account policy.")
Return
End If
Dim folders As ChilkatMailboxes
folders.Initialize
success = imap.MbxList(False, "", "*", folders)
If success <> True Then
imap.Logout
imap.Disconnect
Log("Could not list Gmail folders.")
Return
End If
' LIST special-use attributes work with localized folder names. XLIST is obsolete.
Dim special As ChilkatJsonObject
special.Initialize
Dim i As Int = 0
Do While (i < folders.Count)
If folders.IsSelectable(i) = True Then
Log(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
Loop
imap.Logout
imap.Disconnect
special.EmitCompact = False
Log(special.Emit)
' Missing keys mean the folder was not advertised. Do not substitute an English name.