Sample code for 30+ languages & platforms
Classic ASP Requires Chilkat v11.1.0+

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.

Google documentation

Chilkat Classic ASP Downloads

Classic ASP
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<%
success = 0

'  Assumes Chilkat is unlocked. See Global Unlock Sample.

set token = Server.CreateObject("Chilkat.JsonObject")
success = token.LoadFile("gmail-mail-token.json")
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Could not load the protocol token from the private working directory.") & "</pre>"
    Response.End
End If

If ((token.JsonTypeOf("access_token") <> 1) Or (token.StringOfEquals("access_token","",1) = 1)) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Authorize or refresh the mailbox first.") & "</pre>"
    Response.End
End If

mailbox = "user@example.com"
set imap = Server.CreateObject("Chilkat.Imap")
imap.Ssl = 1
imap.Port = 993
imap.RequireSslCertVerify = 1
imap.AuthMethod = "XOAUTH2"
success = imap.Connect("imap.gmail.com")
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Could not establish the Gmail IMAP TLS connection.") & "</pre>"
    Response.End
End If

success = imap.Login(mailbox,token.StringOf("access_token"))
If (success <> 1) Then
    success = imap.Disconnect()
    Response.Write "<pre>" & Server.HTMLEncode( "IMAP authentication failed. Check the mailbox, scope, expiry, and account policy.") & "</pre>"
    Response.End
End If

capabilities = imap.Capability()
If ((imap.LastMethodSuccess <> 1) Or (imap.HasCapability("X-GM-EXT-1",capabilities) <> 1)) Then
    success = imap.Logout()
    success = imap.Disconnect()
    Response.Write "<pre>" & Server.HTMLEncode( "Gmail IMAP extensions are unavailable.") & "</pre>"
    Response.End
End If

'  Choose archive or trash. This example changes one message's location.
action = "archive"
If ((action <> "archive") And (action <> "trash")) Then
    success = imap.Logout()
    success = imap.Disconnect()
    Response.Write "<pre>" & Server.HTMLEncode( "Choose archive or trash.") & "</pre>"
    Response.End
End If

If (imap.HasCapability("MOVE",capabilities) <> 1) Then
    success = imap.Logout()
    success = imap.Disconnect()
    Response.Write "<pre>" & Server.HTMLEncode( "The server must support IMAP MOVE.") & "</pre>"
    Response.End
End If

set folders = Server.CreateObject("Chilkat.Mailboxes")
success = imap.MbxList(0,"","*",folders)
If (success <> 1) Then
    success = imap.Logout()
    success = imap.Disconnect()
    Response.Write "<pre>" & Server.HTMLEncode( "Could not list Gmail folders.") & "</pre>"
    Response.End
End If

role = "\All"
If (action = "trash") Then
    role = "\Trash"
End If

destination = ""
count = 0
i = 0
Do While (i < folders.Count)
    If ((folders.HasFlag(i,role) = 1) And (folders.IsSelectable(i) = 1)) Then
        destination = folders.GetName(i)
        count = count + 1
    End If

    i = i + 1
Loop
If (count <> 1) Then
    success = imap.Logout()
    success = imap.Disconnect()
    Response.Write "<pre>" & Server.HTMLEncode( "Could not identify the destination folder uniquely.") & "</pre>"
    Response.End
End If

success = imap.SelectMailbox("INBOX")
If (success <> 1) Then
    success = imap.Logout()
    success = imap.Disconnect()
    Response.Write "<pre>" & Server.HTMLEncode( "Could not open INBOX for the move.") & "</pre>"
    Response.End
End If

'  Obtain this decimal string from the Gmail message/thread ID example.
gmailMessageId = "1278455344230334865"
set idText = Server.CreateObject("Chilkat.StringBuilder")
success = idText.Append(gmailMessageId)
set validation = Server.CreateObject("Chilkat.JsonObject")
matches = idText.RegexMatch("\A[1-9][0-9]{0,19}\z",validation,1000)
If (matches <> 1) Then
    success = imap.Logout()
    success = imap.Disconnect()
    Response.Write "<pre>" & Server.HTMLEncode( "Expected a decimal Gmail message ID string.") & "</pre>"
    Response.End
End If

set target = Server.CreateObject("Chilkat.MessageSet")
set joined1 = Server.CreateObject("Chilkat.StringBuilder")
success = joined1.Append("X-GM-MSGID ")
success = joined1.Append(gmailMessageId)
success = imap.QueryMbx(joined1.GetAsString(),1,target)
If (success <> 1) Then
    success = imap.Logout()
    success = imap.Disconnect()
    Response.Write "<pre>" & Server.HTMLEncode( "Could not find the Gmail message in the selected mailbox.") & "</pre>"
    Response.End
End If

If (target.Count <> 1) Then
    success = imap.Logout()
    success = imap.Disconnect()
    Response.Write "<pre>" & Server.HTMLEncode( "Expected one message in the selected mailbox. Matches: " & target.Count) & "</pre>"
    Response.End
End If

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 <> 1) Then
    success = imap.Logout()
    success = imap.Disconnect()
    Response.Write "<pre>" & Server.HTMLEncode( "Move failed; verify the mailbox state before retrying after a connection loss.") & "</pre>"
    Response.End
End If

Response.Write "<pre>" & Server.HTMLEncode( "Completed " & action & " for Gmail message " & gmailMessageId) & "</pre>"
success = imap.Logout()
success = imap.Disconnect()

%>
</body>
</html>