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 DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Handle hoToken
String sMailbox
Handle hoImap
String sCapabilities
String sAction
Variant vFolders
Handle hoFolders
String sRole
String sDestination
Integer iCount
Integer i
String sGmailMessageId
Handle hoIdText
Variant vValidation
Handle hoValidation
Integer iMatches
Variant vTarget
Handle hoTarget
Handle hoJoined1
String sUid
String sTemp1
Integer iTemp1
Boolean bTemp1
Boolean bTemp2
Move False To iSuccess
// Assumes Chilkat is unlocked. See Global Unlock Sample.
Get Create (RefClass(cComChilkatJsonObject)) To hoToken
If (Not(IsComObjectCreated(hoToken))) Begin
Send CreateComObject of hoToken
End
Get ComLoadFile Of hoToken "gmail-mail-token.json" To iSuccess
If (iSuccess <> True) Begin
Showln "Could not load the protocol token from the private working directory."
Procedure_Return
End
Get ComJsonTypeOf Of hoToken "access_token" To iTemp1
Get ComStringOfEquals Of hoToken "access_token" "" True To bTemp1
If ((iTemp1 <> 1) Or (bTemp1 = True)) Begin
Showln "Authorize or refresh the mailbox first."
Procedure_Return
End
Move "user@example.com" To sMailbox
Get Create (RefClass(cComChilkatImap)) To hoImap
If (Not(IsComObjectCreated(hoImap))) Begin
Send CreateComObject of hoImap
End
Set ComSsl Of hoImap To True
Set ComPort Of hoImap To 993
Set ComRequireSslCertVerify Of hoImap To True
Set ComAuthMethod Of hoImap To "XOAUTH2"
Get ComConnect Of hoImap "imap.gmail.com" To iSuccess
If (iSuccess <> True) Begin
Showln "Could not establish the Gmail IMAP TLS connection."
Procedure_Return
End
Get ComStringOf Of hoToken "access_token" To sTemp1
Get ComLogin Of hoImap sMailbox sTemp1 To iSuccess
If (iSuccess <> True) Begin
Get ComDisconnect Of hoImap To iSuccess
Showln "IMAP authentication failed. Check the mailbox, scope, expiry, and account policy."
Procedure_Return
End
Get ComCapability Of hoImap To sCapabilities
Get ComLastMethodSuccess Of hoImap To bTemp1
Get ComHasCapability Of hoImap "X-GM-EXT-1" sCapabilities To bTemp2
If ((bTemp1 <> True) Or (bTemp2 <> True)) Begin
Get ComLogout Of hoImap To iSuccess
Get ComDisconnect Of hoImap To iSuccess
Showln "Gmail IMAP extensions are unavailable."
Procedure_Return
End
// Choose archive or trash. This example changes one message's location.
Move "archive" To sAction
If ((sAction <> "archive") And (sAction <> "trash")) Begin
Get ComLogout Of hoImap To iSuccess
Get ComDisconnect Of hoImap To iSuccess
Showln "Choose archive or trash."
Procedure_Return
End
Get ComHasCapability Of hoImap "MOVE" sCapabilities To bTemp1
If (bTemp1 <> True) Begin
Get ComLogout Of hoImap To iSuccess
Get ComDisconnect Of hoImap To iSuccess
Showln "The server must support IMAP MOVE."
Procedure_Return
End
Get Create (RefClass(cComChilkatMailboxes)) To hoFolders
If (Not(IsComObjectCreated(hoFolders))) Begin
Send CreateComObject of hoFolders
End
Get pvComObject of hoFolders to vFolders
Get ComMbxList Of hoImap False "" "*" vFolders To iSuccess
If (iSuccess <> True) Begin
Get ComLogout Of hoImap To iSuccess
Get ComDisconnect Of hoImap To iSuccess
Showln "Could not list Gmail folders."
Procedure_Return
End
Move "\All" To sRole
If (sAction = "trash") Begin
Move "\Trash" To sRole
End
Move "" To sDestination
Move 0 To iCount
Move 0 To i
While (i < (ComCount(hoFolders)))
Get ComHasFlag Of hoFolders i sRole To bTemp1
Get ComIsSelectable Of hoFolders i To bTemp2
If ((bTemp1 = True) And (bTemp2 = True)) Begin
Get ComGetName Of hoFolders i To sDestination
Move (iCount + 1) To iCount
End
Move (i + 1) To i
Loop
If (iCount <> 1) Begin
Get ComLogout Of hoImap To iSuccess
Get ComDisconnect Of hoImap To iSuccess
Showln "Could not identify the destination folder uniquely."
Procedure_Return
End
Get ComSelectMailbox Of hoImap "INBOX" To iSuccess
If (iSuccess <> True) Begin
Get ComLogout Of hoImap To iSuccess
Get ComDisconnect Of hoImap To iSuccess
Showln "Could not open INBOX for the move."
Procedure_Return
End
// Obtain this decimal string from the Gmail message/thread ID example.
Move "1278455344230334865" To sGmailMessageId
Get Create (RefClass(cComChilkatStringBuilder)) To hoIdText
If (Not(IsComObjectCreated(hoIdText))) Begin
Send CreateComObject of hoIdText
End
Get ComAppend Of hoIdText sGmailMessageId To iSuccess
Get Create (RefClass(cComChilkatJsonObject)) To hoValidation
If (Not(IsComObjectCreated(hoValidation))) Begin
Send CreateComObject of hoValidation
End
Get pvComObject of hoValidation to vValidation
Get ComRegexMatch Of hoIdText "\A[1-9][0-9]{0,19}\z" vValidation 1000 To iMatches
If (iMatches <> 1) Begin
Get ComLogout Of hoImap To iSuccess
Get ComDisconnect Of hoImap To iSuccess
Showln "Expected a decimal Gmail message ID string."
Procedure_Return
End
Get Create (RefClass(cComChilkatMessageSet)) To hoTarget
If (Not(IsComObjectCreated(hoTarget))) Begin
Send CreateComObject of hoTarget
End
Get Create (RefClass(cComChilkatStringBuilder)) To hoJoined1
If (Not(IsComObjectCreated(hoJoined1))) Begin
Send CreateComObject of hoJoined1
End
Get ComAppend Of hoJoined1 "X-GM-MSGID " To iSuccess
Get ComAppend Of hoJoined1 sGmailMessageId To iSuccess
Get ComGetAsString Of hoJoined1 To sTemp1
Get pvComObject of hoTarget to vTarget
Get ComQueryMbx Of hoImap sTemp1 True vTarget To iSuccess
If (iSuccess <> True) Begin
Get ComLogout Of hoImap To iSuccess
Get ComDisconnect Of hoImap To iSuccess
Showln "Could not find the Gmail message in the selected mailbox."
Procedure_Return
End
Get ComCount Of hoTarget To iTemp1
If (iTemp1 <> 1) Begin
Get ComLogout Of hoImap To iSuccess
Get ComDisconnect Of hoImap To iSuccess
Get ComCount Of hoTarget To iTemp1
Showln "Expected one message in the selected mailbox. Matches: " iTemp1
Procedure_Return
End
Get ComToCompactString Of hoTarget To sUid
// MoveMessages uses UID MOVE and maintains the Imap object's mailbox state.
// No broad EXPUNGE is issued, and unrelated deleted flags are not committed.
Get pvComObject of hoTarget to vTarget
Get ComMoveMessages Of hoImap vTarget sDestination To iSuccess
If (iSuccess <> True) Begin
Get ComLogout Of hoImap To iSuccess
Get ComDisconnect Of hoImap To iSuccess
Showln "Move failed; verify the mailbox state before retrying after a connection loss."
Procedure_Return
End
Showln "Completed " sAction " for Gmail message " sGmailMessageId
Get ComLogout Of hoImap To iSuccess
Get ComDisconnect Of hoImap To iSuccess
End_Procedure