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 Visual FoxPro Downloads
LOCAL lnSuccess
LOCAL loToken
LOCAL lcMailbox
LOCAL loImap
LOCAL lcCapabilities
LOCAL lcAction
LOCAL loFolders
LOCAL lcRole
LOCAL lcDestination
LOCAL lnCount
LOCAL i
LOCAL lcGmailMessageId
LOCAL loIdText
LOCAL loValidation
LOCAL lnMatches
LOCAL loTarget
LOCAL loJoined1
LOCAL lcUid
lnSuccess = 0
* Assumes Chilkat is unlocked. See Global Unlock Sample.
loToken = CreateObject('Chilkat.JsonObject')
lnSuccess = loToken.LoadFile("gmail-mail-token.json")
IF (lnSuccess <> 1) THEN
? "Could not load the protocol token from the private working directory."
RELEASE loToken
CANCEL
ENDIF
IF ((loToken.JsonTypeOf("access_token") <> 1) OR (loToken.StringOfEquals("access_token","",1) = 1)) THEN
? "Authorize or refresh the mailbox first."
RELEASE loToken
CANCEL
ENDIF
lcMailbox = "user@example.com"
loImap = CreateObject('Chilkat.Imap')
loImap.Ssl = 1
loImap.Port = 993
loImap.RequireSslCertVerify = 1
loImap.AuthMethod = "XOAUTH2"
lnSuccess = loImap.Connect("imap.gmail.com")
IF (lnSuccess <> 1) THEN
? "Could not establish the Gmail IMAP TLS connection."
RELEASE loToken
RELEASE loImap
CANCEL
ENDIF
lnSuccess = loImap.Login(lcMailbox,loToken.StringOf("access_token"))
IF (lnSuccess <> 1) THEN
loImap.Disconnect()
? "IMAP authentication failed. Check the mailbox, scope, expiry, and account policy."
RELEASE loToken
RELEASE loImap
CANCEL
ENDIF
lcCapabilities = loImap.Capability()
IF ((loImap.LastMethodSuccess <> 1) OR (loImap.HasCapability("X-GM-EXT-1",lcCapabilities) <> 1)) THEN
loImap.Logout()
loImap.Disconnect()
? "Gmail IMAP extensions are unavailable."
RELEASE loToken
RELEASE loImap
CANCEL
ENDIF
* Choose archive or trash. This example changes one message's location.
lcAction = "archive"
IF ((lcAction <> "archive") AND (lcAction <> "trash")) THEN
loImap.Logout()
loImap.Disconnect()
? "Choose archive or trash."
RELEASE loToken
RELEASE loImap
CANCEL
ENDIF
IF (loImap.HasCapability("MOVE",lcCapabilities) <> 1) THEN
loImap.Logout()
loImap.Disconnect()
? "The server must support IMAP MOVE."
RELEASE loToken
RELEASE loImap
CANCEL
ENDIF
loFolders = CreateObject('Chilkat.Mailboxes')
lnSuccess = loImap.MbxList(0,"","*",loFolders)
IF (lnSuccess <> 1) THEN
loImap.Logout()
loImap.Disconnect()
? "Could not list Gmail folders."
RELEASE loToken
RELEASE loImap
RELEASE loFolders
CANCEL
ENDIF
lcRole = "\All"
IF (lcAction = "trash") THEN
lcRole = "\Trash"
ENDIF
lcDestination = ""
lnCount = 0
i = 0
DO WHILE (i < loFolders.Count)
IF ((loFolders.HasFlag(i,lcRole) = 1) AND (loFolders.IsSelectable(i) = 1)) THEN
lcDestination = loFolders.GetName(i)
lnCount = lnCount + 1
ENDIF
i = i + 1
ENDDO
IF (lnCount <> 1) THEN
loImap.Logout()
loImap.Disconnect()
? "Could not identify the destination folder uniquely."
RELEASE loToken
RELEASE loImap
RELEASE loFolders
CANCEL
ENDIF
lnSuccess = loImap.SelectMailbox("INBOX")
IF (lnSuccess <> 1) THEN
loImap.Logout()
loImap.Disconnect()
? "Could not open INBOX for the move."
RELEASE loToken
RELEASE loImap
RELEASE loFolders
CANCEL
ENDIF
* Obtain this decimal string from the Gmail message/thread ID example.
lcGmailMessageId = "1278455344230334865"
loIdText = CreateObject('Chilkat.StringBuilder')
loIdText.Append(lcGmailMessageId)
loValidation = CreateObject('Chilkat.JsonObject')
lnMatches = loIdText.RegexMatch("\A[1-9][0-9]{0,19}\z",loValidation,1000)
IF (lnMatches <> 1) THEN
loImap.Logout()
loImap.Disconnect()
? "Expected a decimal Gmail message ID string."
RELEASE loToken
RELEASE loImap
RELEASE loFolders
RELEASE loIdText
RELEASE loValidation
CANCEL
ENDIF
loTarget = CreateObject('Chilkat.MessageSet')
loJoined1 = CreateObject('Chilkat.StringBuilder')
loJoined1.Append("X-GM-MSGID ")
loJoined1.Append(lcGmailMessageId)
lnSuccess = loImap.QueryMbx(loJoined1.GetAsString(),1,loTarget)
IF (lnSuccess <> 1) THEN
loImap.Logout()
loImap.Disconnect()
? "Could not find the Gmail message in the selected mailbox."
RELEASE loToken
RELEASE loImap
RELEASE loFolders
RELEASE loIdText
RELEASE loValidation
RELEASE loTarget
RELEASE loJoined1
CANCEL
ENDIF
IF (loTarget.Count <> 1) THEN
loImap.Logout()
loImap.Disconnect()
? "Expected one message in the selected mailbox. Matches: " + STR(loTarget.Count)
RELEASE loToken
RELEASE loImap
RELEASE loFolders
RELEASE loIdText
RELEASE loValidation
RELEASE loTarget
RELEASE loJoined1
CANCEL
ENDIF
lcUid = loTarget.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.
lnSuccess = loImap.MoveMessages(loTarget,lcDestination)
IF (lnSuccess <> 1) THEN
loImap.Logout()
loImap.Disconnect()
? "Move failed; verify the mailbox state before retrying after a connection loss."
RELEASE loToken
RELEASE loImap
RELEASE loFolders
RELEASE loIdText
RELEASE loValidation
RELEASE loTarget
RELEASE loJoined1
CANCEL
ENDIF
? "Completed " + lcAction + " for Gmail message " + lcGmailMessageId
loImap.Logout()
loImap.Disconnect()
RELEASE loToken
RELEASE loImap
RELEASE loFolders
RELEASE loIdText
RELEASE loValidation
RELEASE loTarget
RELEASE loJoined1