Sample code for 30+ languages & platforms
B4X

Move IMAP Messages to Another Mailbox

See more IMAP Examples

Demonstrates the Chilkat Imap.MoveMessages method, which moves the messages in a MessageSet to another mailbox on the server. The first argument is the MessageSet and the second is the destination folder name. This example searches for newsletter messages and moves them to the Archive mailbox.

Background: A true server-side move (the IMAP MOVE extension) is atomic and efficient: the message never leaves the server, so no download/re-upload is involved. If the server lacks the MOVE capability, the equivalent is copy-then-delete-then-expunge — Chilkat handles that fallback for you, but the destination mailbox must already exist.

Chilkat B4X Downloads

B4X
Dim success As Boolean = False

'  Demonstrates the Imap.MoveMessages method, which moves the messages in a MessageSet to
'  another mailbox on the server.  The 1st argument is the MessageSet and the 2nd is the
'  destination folder name.

Dim imap As ChilkatImap
imap.Initialize("imap")

imap.Ssl = True
imap.Port = 993

success = imap.Connect("imap.example.com")
If success = False Then
    Log(imap.LastErrorText)
    Return
End If

success = imap.Login("user@example.com", "myPassword")
If success = False Then
    Log(imap.LastErrorText)
    Return
End If


success = imap.SelectMailbox("Inbox")
If success = False Then
    Log(imap.LastErrorText)
    Return
End If


'  Find the messages to move (here, messages from a particular sender, by UID).
Dim msgSet As ChilkatMessageSet
msgSet.Initialize
success = imap.QueryMbx("FROM " & Chr(34) & "newsletter@example.com" & Chr(34), True, msgSet)
If success = False Then
    Log(imap.LastErrorText)
    Return
End If


'  Move them to the "Archive" mailbox.
success = imap.MoveMessages(msgSet, "Archive")
If success = False Then
    Log(imap.LastErrorText)
    Return
End If


Log("Moved " & msgSet.Count & " messages to Archive.")

success = imap.Disconnect
If success = False Then
    Log(imap.LastErrorText)
    Return
End If