ImapBeginExecuteCustomCommand Method

Note: This API is now obsolete.

Begins an asynchronous request for sending the specified user-defined command to the server and getting the response.

Namespace: MailBee.ImapMail
Assembly: MailBee.NET (in MailBee.NET.dll) Version: 12.5.0 build 687 for .NET 4.5
Syntax
[ObsoleteAttribute("This method is obsolete in .NET 4.5+. Use ExecuteCustomCommanAsync instead.")]
public IAsyncResult BeginExecuteCustomCommand(
	string command,
	string commandID,
	AsyncCallback callback,
	Object state
)

Parameters

command
Type: SystemString
User-defined command text (without line terminator).
commandID
Type: SystemString
The ID (tag in IMAP4 terms) which will be prepended to the command text, or an empty string to let MailBee autogenerate the ID value, or a null reference (Nothing in Visual Basic) to send command without ID.
callback
Type: SystemAsyncCallback
The AsyncCallback delegate. You can leave it a null reference (Nothing in Visual Basic) if you do not use callbacks.
state
Type: SystemObject
An object that contains state information for this request. You can leave it a null reference (Nothing in Visual Basic).

Return Value

Type: IAsyncResult
An IAsyncResult that references the asynchronous user-defined command execution.
Exceptions
ExceptionCondition
MailBeeInvalidStateExceptionThere is already an operation in progress.
Remarks
This method is an asynchronous version of ExecuteCustomCommand(String, String).
Examples

This console sample shows how manage folders (create, delete, rename, subscribe, unsubscribe), expunge deleted messages, and issue NOOP asynchronously.

BeginExecuteCustomCommand(String, String, AsyncCallback, Object) topic also includes the second WinForms sample of sending NOOP command asynchronously (see below).

using System;
using MailBee;
using MailBee.ImapMail;
using MailBee.Mime;

class Sample
{
    static void Main(string[] args)
    {
        Console.WriteLine("Input a new folder name to manage");
        string folderName = Console.ReadLine();

        Imap imp = new Imap();

        // Connect to the server and log in the account.
        imp.Connect("imap.domain.com");
        imp.Login("jdoe@domain.com", "secret");

        // Issue NOOP
        imp.BeginExecuteCustomCommand("NOOP", string.Empty, null, null);
        imp.EndExecuteCustomCommand();

        // Create a new folder
        imp.BeginExecuteCustomCommand(
            "CREATE " + ImapUtils.ToUtf7QuotedString(folderName),
            string.Empty, null, null);
        imp.EndExecuteCustomCommand();

        // Select the created folder (to demonstrate asynchronous Expunge).
        imp.BeginSelectFolder(folderName, false, null, null);
        imp.EndSelectFolder();

        // Expunge deleted messages from this folder. This will actually do
        // nothing since the folder is empty but it shows the idea.
        imp.BeginExecuteCustomCommand("EXPUNGE", string.Empty, null, null);
        imp.EndExecuteCustomCommand();

        // Deselect the folder (we could also call BeginClose(true, ...)
        // to close the folder and expunge the deleted messages instead of
        // sending EXPUNGE and calling BeginClose(false, ...).
        imp.BeginClose(false, null, null);
        imp.EndClose();

        // Subscribe the created folder
        imp.BeginExecuteCustomCommand(
            "SUBSCRIBE " + ImapUtils.ToUtf7QuotedString(folderName),
            string.Empty, null, null);
        imp.EndExecuteCustomCommand();

        // Unsubscribe the created folder
        imp.BeginExecuteCustomCommand(
            "UNSUBSCRIBE " + ImapUtils.ToUtf7QuotedString(folderName),
            string.Empty, null, null);
        imp.EndExecuteCustomCommand();

        // Rename the created folder into "MyFolder".
        imp.BeginExecuteCustomCommand(
            "RENAME " + ImapUtils.ToUtf7QuotedString(folderName) + " " +
            ImapUtils.ToQuotedString("MyFolder"), string.Empty, null, null);
        imp.EndExecuteCustomCommand();

        // Delete the created folder
        imp.BeginExecuteCustomCommand(
            "DELETE " + ImapUtils.ToQuotedString("MyFolder"), string.Empty,
            null, null);
        imp.EndExecuteCustomCommand();

        // Disconnect from the server.
        imp.Disconnect();
    }
}
Examples
This WinForms sample shows how to send NOOP command asynchronously to keep the connection with the server alive for a long period of time. The same approach can be used to execute any IMAP4 command asynchronously.
// To use the code below, import MailBee namespaces at the top of your code
using MailBee;
using MailBee.ImapMail;

// Put the code below inside your class.

System.Timers.Timer t = null;
Imap imp = null;

// A callback function. Since it's called on Imap worker thread,
// WinForms timer cannot be used here because it can only be used
// on message loop thread.
private void ExecuteCustomCommandCallback(IAsyncResult result)
{
    Imap imp = (Imap)result.AsyncState;
    imp.EndExecuteCustomCommand();
    imp.Log.WriteLine("NOOP done");
    t.Start();
}

// System.Timers.Timer.Elapsed event handler.
private void OnElapsed(object sender, System.Timers.ElapsedEventArgs e)
{
    t.Stop();

    // Execute NOOP command in Imap worker thread.
    imp.BeginExecuteCustomCommand("NOOP", string.Empty,
        new AsyncCallback(ExecuteCustomCommandCallback), imp);
}

// The actual code.
private void Form1_Load(object sender, System.EventArgs e)
{
    imp = new Imap();

    // Enable logging to monitor background activity.
    imp.Log.Filename = @"C:\Temp\log.txt";
    imp.Log.Enabled = true;
    imp.Log.Clear();

    // Connect to the server, login and select inbox.
    imp.Connect("imap.domain.com");
    imp.Login("jdoe", "secret");
    imp.SelectFolder("Inbox");

    // Initialize timer to raise Elapsed event every 10 seconds.
    t = new System.Timers.Timer();
    t.Elapsed += new System.Timers.ElapsedEventHandler(OnElapsed);
    t.Interval = 10000;
    t.Start();
}

// Shutdown Imap component on form closing.
private void Form1_Closing(object sender,
    System.ComponentModel.CancelEventArgs e)
{
    t.Stop();
    if (imp.IsBusy)
    {
        imp.EndExecuteCustomCommand();
    }
    // Disconnect from the server.
    imp.Disconnect();
}
See Also