MoveMessages Method

Moves one or more messages from the currently selected mailbox to another mailbox.

The IMAP server must support MOVE extension for this method to work! Use GetCapabilities method to check this first.

To move a single message, set First and Last parameters to the same message number or UID.

To move an arbitrary set of messages, specify MessageSet property and pass -1 as First value (Last value is not important when First is -1). AsUID parameter is still honored when IDs are taken from MessageSet property (depending on AsUID value, they will be treated as UIDs or ordinal message numbers).

To move all messages starting from a particular one, set First to its UID or ordinal message number and Last to -1. This issues a special IMAP request MOVE X:* where X is your First value. Alternatively, you can set Last to MessageCount (like in the sample below) but it only works for ordinal message numbers and not UIDs (because you may not know the UID of the last message in the mailbox and requesting it from the server causes the overhead of an extra IMAP query).

To get the UID value assigned to the message in the destination folder, you can use UIDPlusResult property.

blnResult = ObjectName.CopyMessages(MailboxName, First, Last, AsUID)  
MailboxName As String Destination mailbox name. Messages will be copied to this mailbox  
First As Long Message number or UID of the first message in the moved range, or -1 to use MessageSet as input.  
Last As Long Message number or UID of the last message in the moved range. -1 means "last message in the mailbox".  
AsUID As Boolean If True, First and Last parameters must be specified as UIDs (unique-IDs). Otherwise, First and Last must be message numbers.  
Return value As Boolean True if successful, False if error has occurred. You can check ErrDesc property or log file to get more detailed error information.  

Usage example:

' This sample moves last 3 messages from "Inbox" folder
' into "Archive" folder. Both folders must exist and Inbox must contain 3 messages at least.

Dim Mailer, Caps, MoveSupported
'Using visual basic to create object
Set Mailer = CreateObject("MailBee.IMAP4")
'Using ASP to create object
'Set Mailer = Server.CreateObject("MailBee.IMAP4")
'In ASP use Response.Write instead of MsgBox

Mailer.EnableLogging = True ' Logging helps discover any problems
Mailer.LogFilePath = "C:\Temp\imap4_log.txt"

Mailer.LicenseKey = "put your license key here"
If Mailer.Connect("", 143, "user@login", "password") Then
  MoveSupported = False
  Caps = Mailer.GetCapabilities( , "MOVE")
  If Not IsEmpty(Caps) Then
    If UBound(Caps) >= Lbound(Caps) Then MoveSupported = True
  End If

  If MoveSupported Then
    If Mailer.SelectMailbox("Inbox") Then
      If Mailer.MoveMessages("Archive", Mailer.MessageCount - 2, Mailer.MessageCount, False) Then
          MsgBox "Messages moved successfully"
        If Mailer.ErrCode = 311 Or Mailer.ErrCode = 312 Then
          MsgBox Mailer.ServerStatusResponse 
          MsgBox "Error #" & CStr(Mailer.ErrCode)
        End If
      End If
    End If
    MsgBox "MOVE not supported :("
  End If
  MsgBox Mailer.ErrDesc
End If

See Also:

MessageCount Property

SelectMailbox Method

Copyright © 2002-2022, AfterLogic Corporation. All rights reserved.