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 PowerShell Downloads
Add-Type -Path "C:\chilkat\ChilkatDotNet47-x64\ChilkatDotNet47.dll"
$success = $false
# Assumes Chilkat is unlocked. See Global Unlock Sample.
$token = New-Object Chilkat.JsonObject
$success = $token.LoadFile("gmail-mail-token.json")
if ($success -ne $true) {
$("Could not load the protocol token from the private working directory.")
exit
}
if (($token.JsonTypeOf("access_token") -ne 1) -or ($token.StringOfEquals("access_token","",$true) -eq $true)) {
$("Authorize or refresh the mailbox first.")
exit
}
$mailbox = "user@example.com"
$imap = New-Object Chilkat.Imap
$imap.Ssl = $true
$imap.Port = 993
$imap.RequireSslCertVerify = $true
$imap.AuthMethod = "XOAUTH2"
$success = $imap.Connect("imap.gmail.com")
if ($success -ne $true) {
$("Could not establish the Gmail IMAP TLS connection.")
exit
}
$success = $imap.Login($mailbox,$token.StringOf("access_token"))
if ($success -ne $true) {
$imap.Disconnect()
$("IMAP authentication failed. Check the mailbox, scope, expiry, and account policy.")
exit
}
$folders = New-Object Chilkat.Mailboxes
$success = $imap.MbxList($false,"","*",$folders)
if ($success -ne $true) {
$imap.Logout()
$imap.Disconnect()
$("Could not list Gmail folders.")
exit
}
# LIST special-use attributes work with localized folder names. XLIST is obsolete.
$special = New-Object Chilkat.JsonObject
$i = 0
while (($i -lt $folders.Count)) {
if ($folders.IsSelectable($i) -eq $true) {
$($folders.GetName($i) + " " + $folders.GetFlags($i))
if ($folders.HasFlag($i,"\All") -eq $true) {
$special.UpdateString("allMail",$folders.GetName($i))
}
if ($folders.HasFlag($i,"\Sent") -eq $true) {
$special.UpdateString("sent",$folders.GetName($i))
}
if ($folders.HasFlag($i,"\Drafts") -eq $true) {
$special.UpdateString("drafts",$folders.GetName($i))
}
if ($folders.HasFlag($i,"\Junk") -eq $true) {
$special.UpdateString("spam",$folders.GetName($i))
}
if ($folders.HasFlag($i,"\Trash") -eq $true) {
$special.UpdateString("trash",$folders.GetName($i))
}
if ($folders.HasFlag($i,"\Flagged") -eq $true) {
$special.UpdateString("starred",$folders.GetName($i))
}
if ($folders.HasFlag($i,"\Important") -eq $true) {
$special.UpdateString("important",$folders.GetName($i))
}
}
$i = $i + 1
}
$imap.Logout()
$imap.Disconnect()
$special.EmitCompact = $false
$($special.Emit())
# Missing keys mean the folder was not advertised. Do not substitute an English name.