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 Xojo Plugin Downloads
Dim success As Boolean
success = False
// Assumes Chilkat is unlocked. See Global Unlock Sample.
Dim token As New Chilkat.JsonObject
success = token.LoadFile("gmail-mail-token.json")
If (success <> True) Then
System.DebugLog("Could not load the protocol token from the private working directory.")
Return
End If
If ((token.JsonTypeOf("access_token") <> 1) Or (token.StringOfEquals("access_token","",True) = True)) Then
System.DebugLog("Authorize or refresh the mailbox first.")
Return
End If
Dim mailbox As String
mailbox = "user@example.com"
Dim imap As New Chilkat.Imap
imap.Ssl = True
imap.Port = 993
imap.RequireSslCertVerify = True
imap.AuthMethod = "XOAUTH2"
success = imap.Connect("imap.gmail.com")
If (success <> True) Then
System.DebugLog("Could not establish the Gmail IMAP TLS connection.")
Return
End If
success = imap.Login(mailbox,token.StringOf("access_token"))
If (success <> True) Then
success = imap.Disconnect()
System.DebugLog("IMAP authentication failed. Check the mailbox, scope, expiry, and account policy.")
Return
End If
Dim capabilities As String
capabilities = imap.Capability()
If ((imap.LastMethodSuccess <> True) Or (imap.HasCapability("X-GM-EXT-1",capabilities) <> True)) Then
success = imap.Logout()
success = imap.Disconnect()
System.DebugLog("Gmail IMAP extensions are unavailable.")
Return
End If
// Choose archive or trash. This example changes one message's location.
Dim action As String
action = "archive"
If ((action <> "archive") And (action <> "trash")) Then
success = imap.Logout()
success = imap.Disconnect()
System.DebugLog("Choose archive or trash.")
Return
End If
If (imap.HasCapability("MOVE",capabilities) <> True) Then
success = imap.Logout()
success = imap.Disconnect()
System.DebugLog("The server must support IMAP MOVE.")
Return
End If
Dim folders As New Chilkat.Mailboxes
success = imap.MbxList(False,"","*",folders)
If (success <> True) Then
success = imap.Logout()
success = imap.Disconnect()
System.DebugLog("Could not list Gmail folders.")
Return
End If
Dim role As String
role = "\All"
If (action = "trash") Then
role = "\Trash"
End If
Dim destination As String
destination = ""
Dim count As Int32
count = 0
Dim i As Int32
i = 0
While (i < folders.Count)
If ((folders.HasFlag(i,role) = True) And (folders.IsSelectable(i) = True)) Then
destination = folders.GetName(i)
count = count + 1
End If
i = i + 1
Wend
If (count <> 1) Then
success = imap.Logout()
success = imap.Disconnect()
System.DebugLog("Could not identify the destination folder uniquely.")
Return
End If
success = imap.SelectMailbox("INBOX")
If (success <> True) Then
success = imap.Logout()
success = imap.Disconnect()
System.DebugLog("Could not open INBOX for the move.")
Return
End If
// Obtain this decimal string from the Gmail message/thread ID example.
Dim gmailMessageId As String
gmailMessageId = "1278455344230334865"
Dim idText As New Chilkat.StringBuilder
success = idText.Append(gmailMessageId)
Dim validation As New Chilkat.JsonObject
Dim matches As Int32
matches = idText.RegexMatch("\A[1-9][0-9]{0,19}\z",validation,1000)
If (matches <> 1) Then
success = imap.Logout()
success = imap.Disconnect()
System.DebugLog("Expected a decimal Gmail message ID string.")
Return
End If
Dim target As New Chilkat.MessageSet
Dim joined1 As New Chilkat.StringBuilder
success = joined1.Append("X-GM-MSGID ")
success = joined1.Append(gmailMessageId)
success = imap.QueryMbx(joined1.GetAsString(),True,target)
If (success <> True) Then
success = imap.Logout()
success = imap.Disconnect()
System.DebugLog("Could not find the Gmail message in the selected mailbox.")
Return
End If
If (target.Count <> 1) Then
success = imap.Logout()
success = imap.Disconnect()
System.DebugLog("Expected one message in the selected mailbox. Matches: " + Str(target.Count))
Return
End If
Dim uid As String
uid = target.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.
success = imap.MoveMessages(target,destination)
If (success <> True) Then
success = imap.Logout()
success = imap.Disconnect()
System.DebugLog("Move failed; verify the mailbox state before retrying after a connection loss.")
Return
End If
System.DebugLog("Completed " + action + " for Gmail message " + gmailMessageId)
success = imap.Logout()
success = imap.Disconnect()