MailBee.NET Objects 3.1

Imap.BeginCopyOrMoveMessages Method 

Begins an asynchronous request for copying or moving the specified messages from the currently selected folder to the specified folder.

public IAsyncResult BeginCopyOrMoveMessages(
   string messageIndexSet,
   bool indexIsUid,
   string targetFolderName,
   UidPlusResult result,
   bool move,
   AsyncCallback callback,
   object state
);

Parameters

messageIndexSet
A message sequence string containing ordinal message numbers or UIDs. Can be composed manually or using ToString.
indexIsUid
If true, messageIndexSet is treated as a sequence of UIDs; otherwise, as a sequence of ordinal message numbers.
targetFolderName
The full name of the destination folder.
result
A reference to the UidPlusResult object to be filled with the outcome of the copy/move operation reported by UIDPLUS enabled server (the outcome includes the UIDs of the source messages being copied/moved, the UIDs assigned to the copied/moved messages in the destination folder, and the UIDVALIDITY of the destination folder), or a null reference (Nothing in Visual Basic) if the application does not need this information.
move
If true, the messages will be moved; otherwise, copied.
callback
The AsyncCallback delegate. You can leave it a null reference (Nothing in Visual Basic) if you do not use callbacks.
state
An object that contains state information for this request. You can leave it a null reference (Nothing in Visual Basic).

Return Value

An IAsyncResult that references the asynchronous copying or moving messages process.

Remarks

This method is an asynchronous version of CopyMessages (if move is false) or MoveMessages (if move is true).

Exceptions

Exception Type Condition
MailBeeInvalidStateException There is already an operation in progress.

Example

This console sample demonstrates asynchronous moving the first and the last messages from the Inbox folder into the Trash folder. A callback function is used as well.

The sample assumes "Trash" folder had already been created in the past. To learn how to make sure the folder exists, see UploadMessage or UploadMessage samples.

[C#]
using System;
using MailBee;
using MailBee.ImapMail;

class Sample
{
    // A callback function.
    private static void CopyOrMoveMessagesCallback(IAsyncResult result)
    {
        Imap imp = (Imap)result.AsyncState;
        try
        {
            imp.EndCopyOrMoveMessages();
            Console.WriteLine("The messages moved successfully");
        }
        catch (MailBeeException e)
        {
            // In callback functions (generally speaking, in worker threads),
            // it's better to handle exceptions. If exception is not handled,
            // the worker thread silently dies and the application may never
            // know that something went wrong.
            Console.WriteLine(e.Message);
        }
    }

    // The actual code.
    static void Main(string[] args)
    {
        Imap imp = new Imap();

        // Connect to the server, login and select inbox.
        imp.Connect("imap4.somehost.com");
        imp.Login("jdoe", "secret");
        imp.SelectFolder("INBOX");

        // Initiate an asynchronous messages moving attempt.
        // We move the first and the last messages.
        IAsyncResult ar = imp.BeginCopyOrMoveMessages(
            "1," + imp.MessageCount.ToString(), false,
            "Trash", null, true,
            new AsyncCallback(CopyOrMoveMessagesCallback), imp);

        // Simulate some lengthy work here. At the same time,
        // messages are moved on another thread.
        System.Threading.Thread.Sleep(3000);

        // If the messages moving attempt is still in progress,
        // then wait until it's finished.
        while (imp.IsBusy) ar.AsyncWaitHandle.WaitOne();

        // Disconnect from the server.
        imp.Disconnect();
    }
}
[Visual Basic]
Imports System
Imports MailBee
Imports MailBee.ImapMail

Module Sample
    ' A callback function.
    Private Sub CopyOrMoveMessagesCallback(ByVal result As IAsyncResult)
        Dim imp As Imap
        imp = CType(result.AsyncState, Imap)

        Try
            imp.EndCopyOrMoveMessages()
            Console.WriteLine("The messages moved successfully")
        Catch e As MailBeeException
            ' In callback functions (generally speaking, in worker threads),
            ' it's better to handle exceptions. If exception is not handled,
            ' the worker thread silently dies and the application may never
            ' know that something went wrong.
            Console.WriteLine(e.Message)
        End Try
    End Sub

    ' The actual code.
    Sub Main(ByVal args As String())
        Dim imp As New Imap

        ' Connect to the server, login and select inbox.
        imp.Connect("imap4.somehost.com")
        imp.Login("jdoe", "secret")
        imp.SelectFolder("INBOX")

        ' Initiate an asynchronous messages moving attempt.
        ' We move the first and the last messages.
        Dim ar As IAsyncResult = imp.BeginCopyOrMoveMessages("1," & _
            imp.MessageCount.ToString(), False, "Trash", Nothing, _
            True, AddressOf CopyOrMoveMessagesCallback, imp)

        ' Simulate some lengthy work here. At the same time,
        ' messages are moved on another thread.
        System.Threading.Thread.Sleep(3000)

        ' If the messages moving attempt is still in progress,
        ' then wait until it's finished.
        While imp.IsBusy
            ar.AsyncWaitHandle.WaitOne()
        End While

        ' Disconnect from the server.
        imp.Disconnect()
    End Sub
End Module

See Also

Imap Class | MailBee.ImapMail Namespace | CopyMessages | MoveMessages