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 PureBasic Downloads
IncludeFile "CkMessageSet.pb"
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkMailboxes.pb"
IncludeFile "CkJsonObject.pb"
IncludeFile "CkImap.pb"
Procedure ChilkatExample()
success.i = 0
; Assumes Chilkat is unlocked. See Global Unlock Sample.
token.i = CkJsonObject::ckCreate()
If token.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkJsonObject::ckLoadFile(token,"gmail-mail-token.json")
If success <> 1
Debug "Could not load the protocol token from the private working directory."
CkJsonObject::ckDispose(token)
ProcedureReturn
EndIf
If (CkJsonObject::ckJsonTypeOf(token,"access_token") <> 1) OR (CkJsonObject::ckStringOfEquals(token,"access_token","",1) = 1)
Debug "Authorize or refresh the mailbox first."
CkJsonObject::ckDispose(token)
ProcedureReturn
EndIf
mailbox.s = "user@example.com"
imap.i = CkImap::ckCreate()
If imap.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkImap::setCkSsl(imap, 1)
CkImap::setCkPort(imap, 993)
CkImap::setCkRequireSslCertVerify(imap, 1)
CkImap::setCkAuthMethod(imap, "XOAUTH2")
success = CkImap::ckConnect(imap,"imap.gmail.com")
If success <> 1
Debug "Could not establish the Gmail IMAP TLS connection."
CkJsonObject::ckDispose(token)
CkImap::ckDispose(imap)
ProcedureReturn
EndIf
success = CkImap::ckLogin(imap,mailbox,CkJsonObject::ckStringOf(token,"access_token"))
If success <> 1
CkImap::ckDisconnect(imap)
Debug "IMAP authentication failed. Check the mailbox, scope, expiry, and account policy."
CkJsonObject::ckDispose(token)
CkImap::ckDispose(imap)
ProcedureReturn
EndIf
capabilities.s = CkImap::ckCapability(imap)
If (CkImap::ckLastMethodSuccess(imap) <> 1) OR (CkImap::ckHasCapability(imap,"X-GM-EXT-1",capabilities) <> 1)
CkImap::ckLogout(imap)
CkImap::ckDisconnect(imap)
Debug "Gmail IMAP extensions are unavailable."
CkJsonObject::ckDispose(token)
CkImap::ckDispose(imap)
ProcedureReturn
EndIf
; Choose archive or trash. This example changes one message's location.
action.s = "archive"
If (action <> "archive") AND (action <> "trash")
CkImap::ckLogout(imap)
CkImap::ckDisconnect(imap)
Debug "Choose archive or trash."
CkJsonObject::ckDispose(token)
CkImap::ckDispose(imap)
ProcedureReturn
EndIf
If CkImap::ckHasCapability(imap,"MOVE",capabilities) <> 1
CkImap::ckLogout(imap)
CkImap::ckDisconnect(imap)
Debug "The server must support IMAP MOVE."
CkJsonObject::ckDispose(token)
CkImap::ckDispose(imap)
ProcedureReturn
EndIf
folders.i = CkMailboxes::ckCreate()
If folders.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkImap::ckMbxList(imap,0,"","*",folders)
If success <> 1
CkImap::ckLogout(imap)
CkImap::ckDisconnect(imap)
Debug "Could not list Gmail folders."
CkJsonObject::ckDispose(token)
CkImap::ckDispose(imap)
CkMailboxes::ckDispose(folders)
ProcedureReturn
EndIf
role.s = "\All"
If action = "trash"
role = "\Trash"
EndIf
destination.s = ""
count.i = 0
i.i = 0
While (i < CkMailboxes::ckCount(folders))
If (CkMailboxes::ckHasFlag(folders,i,role) = 1) AND (CkMailboxes::ckIsSelectable(folders,i) = 1)
destination = CkMailboxes::ckGetName(folders,i)
count = count + 1
EndIf
i = i + 1
Wend
If count <> 1
CkImap::ckLogout(imap)
CkImap::ckDisconnect(imap)
Debug "Could not identify the destination folder uniquely."
CkJsonObject::ckDispose(token)
CkImap::ckDispose(imap)
CkMailboxes::ckDispose(folders)
ProcedureReturn
EndIf
success = CkImap::ckSelectMailbox(imap,"INBOX")
If success <> 1
CkImap::ckLogout(imap)
CkImap::ckDisconnect(imap)
Debug "Could not open INBOX for the move."
CkJsonObject::ckDispose(token)
CkImap::ckDispose(imap)
CkMailboxes::ckDispose(folders)
ProcedureReturn
EndIf
; Obtain this decimal string from the Gmail message/thread ID example.
gmailMessageId.s = "1278455344230334865"
idText.i = CkStringBuilder::ckCreate()
If idText.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkStringBuilder::ckAppend(idText,gmailMessageId)
validation.i = CkJsonObject::ckCreate()
If validation.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
matches.i = CkStringBuilder::ckRegexMatch(idText,"\A[1-9][0-9]{0,19}\z",validation,1000)
If matches <> 1
CkImap::ckLogout(imap)
CkImap::ckDisconnect(imap)
Debug "Expected a decimal Gmail message ID string."
CkJsonObject::ckDispose(token)
CkImap::ckDispose(imap)
CkMailboxes::ckDispose(folders)
CkStringBuilder::ckDispose(idText)
CkJsonObject::ckDispose(validation)
ProcedureReturn
EndIf
target.i = CkMessageSet::ckCreate()
If target.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
joined1.i = CkStringBuilder::ckCreate()
If joined1.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkStringBuilder::ckAppend(joined1,"X-GM-MSGID ")
CkStringBuilder::ckAppend(joined1,gmailMessageId)
success = CkImap::ckQueryMbx(imap,CkStringBuilder::ckGetAsString(joined1),1,target)
If success <> 1
CkImap::ckLogout(imap)
CkImap::ckDisconnect(imap)
Debug "Could not find the Gmail message in the selected mailbox."
CkJsonObject::ckDispose(token)
CkImap::ckDispose(imap)
CkMailboxes::ckDispose(folders)
CkStringBuilder::ckDispose(idText)
CkJsonObject::ckDispose(validation)
CkMessageSet::ckDispose(target)
CkStringBuilder::ckDispose(joined1)
ProcedureReturn
EndIf
If CkMessageSet::ckCount(target) <> 1
CkImap::ckLogout(imap)
CkImap::ckDisconnect(imap)
Debug "Expected one message in the selected mailbox. Matches: " + Str(CkMessageSet::ckCount(target))
CkJsonObject::ckDispose(token)
CkImap::ckDispose(imap)
CkMailboxes::ckDispose(folders)
CkStringBuilder::ckDispose(idText)
CkJsonObject::ckDispose(validation)
CkMessageSet::ckDispose(target)
CkStringBuilder::ckDispose(joined1)
ProcedureReturn
EndIf
uid.s = CkMessageSet::ckToCompactString(target)
; MoveMessages uses UID MOVE and maintains the Imap object's mailbox state.
; No broad EXPUNGE is issued, and unrelated deleted flags are not committed.
success = CkImap::ckMoveMessages(imap,target,destination)
If success <> 1
CkImap::ckLogout(imap)
CkImap::ckDisconnect(imap)
Debug "Move failed; verify the mailbox state before retrying after a connection loss."
CkJsonObject::ckDispose(token)
CkImap::ckDispose(imap)
CkMailboxes::ckDispose(folders)
CkStringBuilder::ckDispose(idText)
CkJsonObject::ckDispose(validation)
CkMessageSet::ckDispose(target)
CkStringBuilder::ckDispose(joined1)
ProcedureReturn
EndIf
Debug "Completed " + action + " for Gmail message " + gmailMessageId
CkImap::ckLogout(imap)
CkImap::ckDisconnect(imap)
CkJsonObject::ckDispose(token)
CkImap::ckDispose(imap)
CkMailboxes::ckDispose(folders)
CkStringBuilder::ckDispose(idText)
CkJsonObject::ckDispose(validation)
CkMessageSet::ckDispose(target)
CkStringBuilder::ckDispose(joined1)
ProcedureReturn
EndProcedure