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 VB.NET Downloads
Dim success As Boolean = 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
Debug.WriteLine("Could not load the protocol token from the private working directory.")
Exit Sub
End If
If ((token.JsonTypeOf("access_token") <> 1) Or (token.StringOfEquals("access_token","",True) = True)) Then
Debug.WriteLine("Authorize or refresh the mailbox first.")
Exit Sub
End If
Dim mailbox As String = "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
Debug.WriteLine("Could not establish the Gmail IMAP TLS connection.")
Exit Sub
End If
success = imap.Login(mailbox,token.StringOf("access_token"))
If (success <> True) Then
imap.Disconnect()
Debug.WriteLine("IMAP authentication failed. Check the mailbox, scope, expiry, and account policy.")
Exit Sub
End If
Dim capabilities As String = imap.Capability()
If ((imap.LastMethodSuccess <> True) Or (imap.HasCapability("X-GM-EXT-1",capabilities) <> True)) Then
imap.Logout()
imap.Disconnect()
Debug.WriteLine("Gmail IMAP extensions are unavailable.")
Exit Sub
End If
' Choose archive or trash. This example changes one message's location.
Dim action As String = "archive"
If ((action <> "archive") And (action <> "trash")) Then
imap.Logout()
imap.Disconnect()
Debug.WriteLine("Choose archive or trash.")
Exit Sub
End If
If (imap.HasCapability("MOVE",capabilities) <> True) Then
imap.Logout()
imap.Disconnect()
Debug.WriteLine("The server must support IMAP MOVE.")
Exit Sub
End If
Dim folders As New Chilkat.Mailboxes
success = imap.MbxList(False,"","*",folders)
If (success <> True) Then
imap.Logout()
imap.Disconnect()
Debug.WriteLine("Could not list Gmail folders.")
Exit Sub
End If
Dim role As String = "\All"
If (action = "trash") Then
role = "\Trash"
End If
Dim destination As String = ""
Dim count As Integer = 0
Dim i As Integer = 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
End While
If (count <> 1) Then
imap.Logout()
imap.Disconnect()
Debug.WriteLine("Could not identify the destination folder uniquely.")
Exit Sub
End If
success = imap.SelectMailbox("INBOX")
If (success <> True) Then
imap.Logout()
imap.Disconnect()
Debug.WriteLine("Could not open INBOX for the move.")
Exit Sub
End If
' Obtain this decimal string from the Gmail message/thread ID example.
Dim gmailMessageId As String = "1278455344230334865"
Dim idText As New Chilkat.StringBuilder
idText.Append(gmailMessageId)
Dim validation As New Chilkat.JsonObject
Dim matches As Integer = idText.RegexMatch("\A[1-9][0-9]{0,19}\z",validation,1000)
If (matches <> 1) Then
imap.Logout()
imap.Disconnect()
Debug.WriteLine("Expected a decimal Gmail message ID string.")
Exit Sub
End If
Dim target As New Chilkat.MessageSet
Dim joined1 As New Chilkat.StringBuilder
joined1.Append("X-GM-MSGID ")
joined1.Append(gmailMessageId)
success = imap.QueryMbx(joined1.GetAsString(),True,target)
If (success <> True) Then
imap.Logout()
imap.Disconnect()
Debug.WriteLine("Could not find the Gmail message in the selected mailbox.")
Exit Sub
End If
If (target.Count <> 1) Then
imap.Logout()
imap.Disconnect()
Debug.WriteLine("Expected one message in the selected mailbox. Matches: " & target.Count)
Exit Sub
End If
Dim uid As String = 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
imap.Logout()
imap.Disconnect()
Debug.WriteLine("Move failed; verify the mailbox state before retrying after a connection loss.")
Exit Sub
End If
Debug.WriteLine("Completed " & action & " for Gmail message " & gmailMessageId)
imap.Logout()
imap.Disconnect()