MailBee.NET Objects 4.0

Imap.BeginExecuteCustomCommand Method 

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

public IAsyncResult BeginExecuteCustomCommand(
   string command,
   string commandID,
   AsyncCallback callback,
   object state
);

Parameters

command
User-defined command text (without line terminator).
commandID
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
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 user-defined command execution.

Remarks

This method is an asynchronous version of ExecuteCustomCommand.

Exceptions

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

Example

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

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

[C#]
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();
    }
}
[Visual Basic]
Imports System
Imports MailBee
Imports MailBee.ImapMail
Imports MailBee.Mime

Module Sample
    Sub Main(ByVal args As String())
        Console.WriteLine("Input a new folder name to manage")
        Dim folderName As String = Console.ReadLine()

        Dim imp As 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, Nothing, Nothing)
        imp.EndExecuteCustomCommand()

        ' Create a new folder
        imp.BeginExecuteCustomCommand( _
            "CREATE " & ImapUtils.ToUtf7QuotedString(folderName), _
            String.Empty, Nothing, Nothing)
        imp.EndExecuteCustomCommand()

        ' Select the created folder (to demonstrate asynchronous Expunge).
        imp.BeginSelectFolder(folderName, False, Nothing, Nothing)
        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, Nothing, Nothing)
        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, Nothing, Nothing)
        imp.EndClose()

        ' Subscribe the created folder
        imp.BeginExecuteCustomCommand( _
            "SUBSCRIBE " & ImapUtils.ToUtf7QuotedString(folderName), _
            String.Empty, Nothing, Nothing)
        imp.EndExecuteCustomCommand()

        ' Unsubscribe the created folder
        imp.BeginExecuteCustomCommand( _
            "UNSUBSCRIBE " & ImapUtils.ToUtf7QuotedString(folderName), _
            String.Empty, Nothing, Nothing)
        imp.EndExecuteCustomCommand()

        ' Rename the created folder into "MyFolder".
        imp.BeginExecuteCustomCommand( _
            "RENAME " & ImapUtils.ToUtf7QuotedString(folderName) & " " & _
            ImapUtils.ToQuotedString("MyFolder"), String.Empty, Nothing, Nothing)
        imp.EndExecuteCustomCommand()

        ' Delete the created folder
        imp.BeginExecuteCustomCommand( _
            "DELETE " & ImapUtils.ToQuotedString("MyFolder"), String.Empty, _
            Nothing, Nothing)
        imp.EndExecuteCustomCommand()

        ' Disconnect from the server.
        imp.Disconnect()
    End Sub
End Module
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.
[C#]
// 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();
}
[Visual Basic]
Imports MailBee
Imports MailBee.ImapMail

' Put the code below inside your class.

Dim t As System.Timers.Timer = Nothing
Dim imp As Imap = Nothing

' 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 Sub ExecuteCustomCommandCallback(ByVal result As IAsyncResult)
    Dim imp As Imap = CType(result.AsyncState, Imap)
    imp.EndExecuteCustomCommand()
    imp.Log.WriteLine("NOOP done")
    t.Start()
End Sub

' System.Timers.Timer.Elapsed event handler.
Private Sub OnElapsed(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs)
    t.Stop()

    ' Execute NOOP command in Imap worker thread.
    imp.BeginExecuteCustomCommand("NOOP", String.Empty, _
        New AsyncCallback(AddressOf ExecuteCustomCommandCallback), imp)
End Sub

' The actual code.
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs)
    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
    AddHandler t.Elapsed, AddressOf OnElapsed
    t.Interval = 10000
    t.Start()
End Sub

' Shutdown Imap component on form closing.
Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs)
    t.Stop()
    If imp.IsBusy Then
        imp.EndExecuteCustomCommand()
    End If
    ' Disconnect from the server.
    imp.Disconnect()
End Sub

See Also

Imap Class | MailBee.ImapMail Namespace | ExecuteCustomCommand