Gmail IMAP: Archive a Message or Move It to Trash
See more GMail SMTP/IMAP/POP Examples
This example changes one message in INBOX. Choose archive to move it to the discovered All Mail folder, or trash to move it to the discovered Trash folder. It resolves a decimal Gmail message ID to a mailbox UID and uses MoveMessages with the server's MOVE capability.
Archiving removes a message from INBOX while retaining it in All Mail. Moving it to Trash is a different action; Gmail normally permanently deletes messages after 30 days there. This example does not empty Trash or permanently delete mail. A message can retain other custom labels when archived.
Folder names are discovered by special-use attributes. Avoid a generic delete/EXPUNGE sequence whose Gmail outcome can depend on account settings and which can affect unrelated messages. If a connection fails during a move, inspect the source and destination before retrying. A concurrent move or deletion can make the UID disappear.
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
Local $sCapabilities = $oImap.Capability()
If (($oImap.LastMethodSuccess <> True) Or ($oImap.HasCapability("X-GM-EXT-1",$sCapabilities) <> True)) Then
$oImap.Logout()
$oImap.Disconnect()
ConsoleWrite("Gmail IMAP extensions are unavailable." & @CRLF)
Exit
EndIf
; Choose archive or trash. This example changes one message's location.
Local $sAction = "archive"
If (($sAction <> "archive") And ($sAction <> "trash")) Then
$oImap.Logout()
$oImap.Disconnect()
ConsoleWrite("Choose archive or trash." & @CRLF)
Exit
EndIf
If ($oImap.HasCapability("MOVE",$sCapabilities) <> True) Then
$oImap.Logout()
$oImap.Disconnect()
ConsoleWrite("The server must support IMAP MOVE." & @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
Local $sRole = "\All"
If ($sAction = "trash") Then
$sRole = "\Trash"
EndIf
Local $sDestination = ""
Local $iCount = 0
Local $i = 0
While ($i < $oFolders.Count)
If (($oFolders.HasFlag($i,$sRole) = True) And ($oFolders.IsSelectable($i) = True)) Then
$sDestination = $oFolders.GetName($i)
$iCount = $iCount + 1
EndIf
$i = $i + 1
Wend
If ($iCount <> 1) Then
$oImap.Logout()
$oImap.Disconnect()
ConsoleWrite("Could not identify the destination folder uniquely." & @CRLF)
Exit
EndIf
$bSuccess = $oImap.SelectMailbox("INBOX")
If ($bSuccess <> True) Then
$oImap.Logout()
$oImap.Disconnect()
ConsoleWrite("Could not open INBOX for the move." & @CRLF)
Exit
EndIf
; Obtain this decimal string from the Gmail message/thread ID example.
Local $sGmailMessageId = "1278455344230334865"
$oIdText = ObjCreate("Chilkat.StringBuilder")
$oIdText.Append($sGmailMessageId)
$oValidation = ObjCreate("Chilkat.JsonObject")
Local $iMatches = $oIdText.RegexMatch("\A[1-9][0-9]{0,19}\z",$oValidation,1000)
If ($iMatches <> 1) Then
$oImap.Logout()
$oImap.Disconnect()
ConsoleWrite("Expected a decimal Gmail message ID string." & @CRLF)
Exit
EndIf
$oTarget = ObjCreate("Chilkat.MessageSet")
$oJoined1 = ObjCreate("Chilkat.StringBuilder")
$oJoined1.Append("X-GM-MSGID ")
$oJoined1.Append($sGmailMessageId)
$bSuccess = $oImap.QueryMbx($oJoined1.GetAsString(),True,$oTarget)
If ($bSuccess <> True) Then
$oImap.Logout()
$oImap.Disconnect()
ConsoleWrite("Could not find the Gmail message in the selected mailbox." & @CRLF)
Exit
EndIf
If ($oTarget.Count <> 1) Then
$oImap.Logout()
$oImap.Disconnect()
ConsoleWrite("Expected one message in the selected mailbox. Matches: " & $oTarget.Count & @CRLF)
Exit
EndIf
Local $sUid = $oTarget.ToCompactString()
; MoveMessages uses UID MOVE and maintains the Imap object's mailbox state.
; No broad EXPUNGE is issued, and unrelated deleted flags are not committed.
$bSuccess = $oImap.MoveMessages($oTarget,$sDestination)
If ($bSuccess <> True) Then
$oImap.Logout()
$oImap.Disconnect()
ConsoleWrite("Move failed; verify the mailbox state before retrying after a connection loss." & @CRLF)
Exit
EndIf
ConsoleWrite("Completed " & $sAction & " for Gmail message " & $sGmailMessageId & @CRLF)
$oImap.Logout()
$oImap.Disconnect()