Sample code for 30+ languages & platforms
PowerBuilder 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 PowerBuilder Downloads

PowerBuilder
integer li_rc
long li_Success
oleobject loo_Token
string ls_Mailbox
oleobject loo_Imap
string ls_Capabilities
string ls_Action
oleobject loo_Folders
string ls_Role
string ls_Destination
long li_Count
long i
string ls_GmailMessageId
oleobject loo_IdText
oleobject loo_Validation
long li_Matches
oleobject loo_Target
oleobject loo_Joined1
string ls_Uid

li_Success = 0

//  Assumes Chilkat is unlocked. See Global Unlock Sample.

loo_Token = create oleobject
li_rc = loo_Token.ConnectToNewObject("Chilkat.JsonObject")
if li_rc < 0 then
    destroy loo_Token
    MessageBox("Error","Connecting to COM object failed")
    return
end if
li_Success = loo_Token.LoadFile("gmail-mail-token.json")
if li_Success <> 1 then
    Write-Debug "Could not load the protocol token from the private working directory."
    destroy loo_Token
    return
end if

if (loo_Token.JsonTypeOf("access_token") <> 1) OR (loo_Token.StringOfEquals("access_token","",1) = 1) then
    Write-Debug "Authorize or refresh the mailbox first."
    destroy loo_Token
    return
end if

ls_Mailbox = "user@example.com"
loo_Imap = create oleobject
li_rc = loo_Imap.ConnectToNewObject("Chilkat.Imap")

loo_Imap.Ssl = 1
loo_Imap.Port = 993
loo_Imap.RequireSslCertVerify = 1
loo_Imap.AuthMethod = "XOAUTH2"
li_Success = loo_Imap.Connect("imap.gmail.com")
if li_Success <> 1 then
    Write-Debug "Could not establish the Gmail IMAP TLS connection."
    destroy loo_Token
    destroy loo_Imap
    return
end if

li_Success = loo_Imap.Login(ls_Mailbox,loo_Token.StringOf("access_token"))
if li_Success <> 1 then
    loo_Imap.Disconnect()
    Write-Debug "IMAP authentication failed. Check the mailbox, scope, expiry, and account policy."
    destroy loo_Token
    destroy loo_Imap
    return
end if

ls_Capabilities = loo_Imap.Capability()
if (loo_Imap.LastMethodSuccess <> 1) OR (loo_Imap.HasCapability("X-GM-EXT-1",ls_Capabilities) <> 1) then
    loo_Imap.Logout()
    loo_Imap.Disconnect()
    Write-Debug "Gmail IMAP extensions are unavailable."
    destroy loo_Token
    destroy loo_Imap
    return
end if

//  Choose archive or trash. This example changes one message's location.
ls_Action = "archive"
if (ls_Action <> "archive") AND (ls_Action <> "trash") then
    loo_Imap.Logout()
    loo_Imap.Disconnect()
    Write-Debug "Choose archive or trash."
    destroy loo_Token
    destroy loo_Imap
    return
end if

if loo_Imap.HasCapability("MOVE",ls_Capabilities) <> 1 then
    loo_Imap.Logout()
    loo_Imap.Disconnect()
    Write-Debug "The server must support IMAP MOVE."
    destroy loo_Token
    destroy loo_Imap
    return
end if

loo_Folders = create oleobject
li_rc = loo_Folders.ConnectToNewObject("Chilkat.Mailboxes")

li_Success = loo_Imap.MbxList(0,"","*",loo_Folders)
if li_Success <> 1 then
    loo_Imap.Logout()
    loo_Imap.Disconnect()
    Write-Debug "Could not list Gmail folders."
    destroy loo_Token
    destroy loo_Imap
    destroy loo_Folders
    return
end if

ls_Role = "\All"
if ls_Action = "trash" then
    ls_Role = "\Trash"
end if

ls_Destination = ""
li_Count = 0
i = 0
do while (i < loo_Folders.Count)
    if (loo_Folders.HasFlag(i,ls_Role) = 1) AND (loo_Folders.IsSelectable(i) = 1) then
        ls_Destination = loo_Folders.GetName(i)
        li_Count = li_Count + 1
    end if

    i = i + 1
loop
if li_Count <> 1 then
    loo_Imap.Logout()
    loo_Imap.Disconnect()
    Write-Debug "Could not identify the destination folder uniquely."
    destroy loo_Token
    destroy loo_Imap
    destroy loo_Folders
    return
end if

li_Success = loo_Imap.SelectMailbox("INBOX")
if li_Success <> 1 then
    loo_Imap.Logout()
    loo_Imap.Disconnect()
    Write-Debug "Could not open INBOX for the move."
    destroy loo_Token
    destroy loo_Imap
    destroy loo_Folders
    return
end if

//  Obtain this decimal string from the Gmail message/thread ID example.
ls_GmailMessageId = "1278455344230334865"
loo_IdText = create oleobject
li_rc = loo_IdText.ConnectToNewObject("Chilkat.StringBuilder")

loo_IdText.Append(ls_GmailMessageId)
loo_Validation = create oleobject
li_rc = loo_Validation.ConnectToNewObject("Chilkat.JsonObject")

li_Matches = loo_IdText.RegexMatch("\A[1-9][0-9]{0,19}\z",loo_Validation,1000)
if li_Matches <> 1 then
    loo_Imap.Logout()
    loo_Imap.Disconnect()
    Write-Debug "Expected a decimal Gmail message ID string."
    destroy loo_Token
    destroy loo_Imap
    destroy loo_Folders
    destroy loo_IdText
    destroy loo_Validation
    return
end if

loo_Target = create oleobject
li_rc = loo_Target.ConnectToNewObject("Chilkat.MessageSet")

loo_Joined1 = create oleobject
li_rc = loo_Joined1.ConnectToNewObject("Chilkat.StringBuilder")

loo_Joined1.Append("X-GM-MSGID ")
loo_Joined1.Append(ls_GmailMessageId)
li_Success = loo_Imap.QueryMbx(loo_Joined1.GetAsString(),1,loo_Target)
if li_Success <> 1 then
    loo_Imap.Logout()
    loo_Imap.Disconnect()
    Write-Debug "Could not find the Gmail message in the selected mailbox."
    destroy loo_Token
    destroy loo_Imap
    destroy loo_Folders
    destroy loo_IdText
    destroy loo_Validation
    destroy loo_Target
    destroy loo_Joined1
    return
end if

if loo_Target.Count <> 1 then
    loo_Imap.Logout()
    loo_Imap.Disconnect()
    Write-Debug "Expected one message in the selected mailbox. Matches: " + string(loo_Target.Count)
    destroy loo_Token
    destroy loo_Imap
    destroy loo_Folders
    destroy loo_IdText
    destroy loo_Validation
    destroy loo_Target
    destroy loo_Joined1
    return
end if

ls_Uid = loo_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.
li_Success = loo_Imap.MoveMessages(loo_Target,ls_Destination)
if li_Success <> 1 then
    loo_Imap.Logout()
    loo_Imap.Disconnect()
    Write-Debug "Move failed; verify the mailbox state before retrying after a connection loss."
    destroy loo_Token
    destroy loo_Imap
    destroy loo_Folders
    destroy loo_IdText
    destroy loo_Validation
    destroy loo_Target
    destroy loo_Joined1
    return
end if

Write-Debug "Completed " + ls_Action + " for Gmail message " + ls_GmailMessageId
loo_Imap.Logout()
loo_Imap.Disconnect()


destroy loo_Token
destroy loo_Imap
destroy loo_Folders
destroy loo_IdText
destroy loo_Validation
destroy loo_Target
destroy loo_Joined1