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 AutoIt Downloads
Local $bSuccess = False
; Assumes Chilkat is unlocked. See Global Unlock Sample.
$oToken = ObjCreate("Chilkat.JsonObject")
$bSuccess = $oToken.LoadFile("gmail-mail-token.json")
If ($bSuccess <> True) Then
ConsoleWrite("Could not load the protocol token from the private working directory." & @CRLF)
Exit
EndIf
If (($oToken.JsonTypeOf("access_token") <> 1) Or ($oToken.StringOfEquals("access_token","",True) = True)) Then
ConsoleWrite("Authorize or refresh the mailbox first." & @CRLF)
Exit
EndIf
Local $sMailbox = "user@example.com"
$oImap = ObjCreate("Chilkat.Imap")
$oImap.Ssl = True
$oImap.Port = 993
$oImap.RequireSslCertVerify = True
$oImap.AuthMethod = "XOAUTH2"
$bSuccess = $oImap.Connect("imap.gmail.com")
If ($bSuccess <> True) Then
ConsoleWrite("Could not establish the Gmail IMAP TLS connection." & @CRLF)
Exit
EndIf
$bSuccess = $oImap.Login($sMailbox,$oToken.StringOf("access_token"))
If ($bSuccess <> True) Then
$oImap.Disconnect()
ConsoleWrite("IMAP authentication failed. Check the mailbox, scope, expiry, and account policy." & @CRLF)
Exit
EndIf
$oFolders = ObjCreate("Chilkat.Mailboxes")
$bSuccess = $oImap.MbxList(False,"","*",$oFolders)
If ($bSuccess <> True) Then
$oImap.Logout()
$oImap.Disconnect()
ConsoleWrite("Could not list Gmail folders." & @CRLF)
Exit
EndIf
; LIST special-use attributes work with localized folder names. XLIST is obsolete.
$oSpecial = ObjCreate("Chilkat.JsonObject")
Local $i = 0
While ($i < $oFolders.Count)
If ($oFolders.IsSelectable($i) = True) Then
ConsoleWrite($oFolders.GetName($i) & " " & $oFolders.GetFlags($i) & @CRLF)
If ($oFolders.HasFlag($i,"\All") = True) Then
$oSpecial.UpdateString("allMail",$oFolders.GetName($i))
EndIf
If ($oFolders.HasFlag($i,"\Sent") = True) Then
$oSpecial.UpdateString("sent",$oFolders.GetName($i))
EndIf
If ($oFolders.HasFlag($i,"\Drafts") = True) Then
$oSpecial.UpdateString("drafts",$oFolders.GetName($i))
EndIf
If ($oFolders.HasFlag($i,"\Junk") = True) Then
$oSpecial.UpdateString("spam",$oFolders.GetName($i))
EndIf
If ($oFolders.HasFlag($i,"\Trash") = True) Then
$oSpecial.UpdateString("trash",$oFolders.GetName($i))
EndIf
If ($oFolders.HasFlag($i,"\Flagged") = True) Then
$oSpecial.UpdateString("starred",$oFolders.GetName($i))
EndIf
If ($oFolders.HasFlag($i,"\Important") = True) Then
$oSpecial.UpdateString("important",$oFolders.GetName($i))
EndIf
EndIf
$i = $i + 1
Wend
$oImap.Logout()
$oImap.Disconnect()
$oSpecial.EmitCompact = False
ConsoleWrite($oSpecial.Emit() & @CRLF)
; Missing keys mean the folder was not advertised. Do not substitute an English name.