MailBee.NET Objects 4.0

Pop3.BeginExecuteCustomCommand Method 

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

public IAsyncResult BeginExecuteCustomCommand(
   string commandString,
   bool multiLineResponse,
   AsyncCallback callback,
   object state
);

Parameters

commandString
User-defined command text (including line terminator).
multiLineResponse
true if the given command returns multi-line response on success; false if the given command always produces single-line response.
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

Sending a mail message to the POP3 server in WinForms application (the POP3 server must support XTND XMIT extension). Sending itself is performed in a few stages:

  1. Send XTND XMIT command
  2. If the server returned positive response indicating XTND XMIT extension is supported, send message source data (encoded as multi-line POP3 request)
  3. If the server returned positive response, the message was accepted for delivery
Callback functions are actively used, and the application message loop never gets blocked (no waiting for an asynchronous method completion is used). Thus, we do need to care about dead-locks which would occur if an event was raised while the message loop was blocked by somewhat like WaitOne method call. This sample's code does not handle any events, but it's safe to use them if needed.
[C#]
// To use the code below, import MailBee namespaces at the top of your code.
using MailBee;
using MailBee.Pop3Mail;
using MailBee.Mime;

// Put the code below inside your class.

// XTND XMIT callback function.
private void XtndXmitCallback(IAsyncResult result)
{
    Pop3 pop = (Pop3)result.AsyncState;

    // Process the results of XTND XMIT command.
    try
    {
        pop.EndExecuteCustomCommand();
    }
    catch (MailBeePop3NegativeResponseException)
    {
        MessageBox.Show("XTND XMIT command is not supported. The server responded: " +
            pop.GetServerResponse());
        pop.Disconnect();
        return;
    }

    // Create a mail message to be sent.
    MailMessage msg = new MailMessage();
    msg.From.Email = "jdoe@mydomain.com";
    msg.To.AsString = "kathy@herdomain.com";
    msg.Subject = "Meeting request";
    msg.BodyPlainText = "Hi, Kathy,\r\nCan we meet tomorrow?\r\n\r\nRegards,\r\nJohn";

    // Encode the mail message source as multi-line POP3 request.
    string messageSource = System.Text.Encoding.Default.GetString(msg.GetMessageRawData());
    messageSource = messageSource.Replace("\r\n.", "\r\n..");

    // Send the mail message asynchronously.
    pop.BeginExecuteCustomCommand(messageSource + "\r\n.\r\n",
        false, new AsyncCallback(SubmissionCallback), pop);
}

// Mail message submission callback function.
private void SubmissionCallback(IAsyncResult result)
{
    Pop3 pop = (Pop3)result.AsyncState;

    // Process the results of the mail message submission.
    try
    {
        pop.EndExecuteCustomCommand();
        MessageBox.Show("Message submitted for delivery");
    }
    catch (MailBeePop3NegativeResponseException)
    {
        MessageBox.Show("Message was not submitted. The server responded: " +
            pop.GetServerResponse());
    }

    pop.Disconnect();
}

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

    pop.Connect("pop.mydomain.com");
    pop.Login("jdoe", "secret");

    // Issue XTND XMIT command asynchronously.
    pop.BeginExecuteCustomCommand("XTND XMIT\r\n", false,
        new AsyncCallback(XtndXmitCallback), pop);
}
[Visual Basic]
' To use the code below, import MailBee namespaces at the top of your code.
Imports MailBee
Imports MailBee.Pop3Mail
Imports MailBee.Mime

' Put the code below inside your class.

' XTND XMIT callback function.
Private Sub XtndXmitCallback(ByVal result As IAsyncResult)
    Dim pop As New Pop3
    pop = result.AsyncState

    ' Process the results of XTND XMIT command.
    Try
        pop.EndExecuteCustomCommand()
    Catch MailBeePop3NegativeResponseException As Exception
        MsgBox("XTND XMIT command is not supported. The server responded: " & _
            pop.GetServerResponse())
        pop.Disconnect()
        Return
    End Try

    ' Create a mail message to be sent.
    Dim msg As New MailMessage
    msg.From.Email = "jdoe@mydomain.com"
    msg.To.AsString = "kathy@herdomain.com"
    msg.Subject = "Meeting request"
    msg.BodyPlainText = "Hi, Kathy,\r\nCan we meet tomorrow?\r\n\r\nRegards,\r\nJohn"
    
    ' Encode the mail message source as multi-line POP3 request.
    Dim messageSource As String = System.Text.Encoding.Default.GetString(msg.GetMessageRawData())
    messageSource = messageSource.Replace("\r\n.", "\r\n..")

    ' Send the mail message asynchronously.
    pop.BeginExecuteCustomCommand(messageSource + "\r\n.\r\n", False, _
        New AsyncCallback(AddressOf SubmissionCallback), pop)
End Sub

' Mail message submission callback function.
Private Sub SubmissionCallback(ByVal result As IAsyncResult)
    Dim pop As New Pop3
    pop = result.AsyncState

    ' Process the results of the mail message submission.
    Try
        pop.EndExecuteCustomCommand()
        MsgBox("Message submitted for delivery")
    Catch MailBeePop3NegativeResponseException As Exception
        MsgBox("Message was not submitted. The server responded: " & _
            pop.GetServerResponse())
    End Try
    
    pop.Disconnect()
End Sub

' The actual code.
Private Sub Form11_Load(ByVal sender As Object, ByVal e As System.EventArgs)
    Dim pop As New Pop3
    pop.Connect("pop.mydomain.com")
    pop.Login("jdoe", "secret")

    ' Issue XTND XMIT command asynchronously.
    pop.BeginExecuteCustomCommand("XTND XMIT\r\n", False, _
        New AsyncCallback(AddressOf XtndXmitCallback), pop)
End Sub

See Also

Pop3 Class | MailBee.Pop3Mail Namespace | ExecuteCustomCommand