MailBee.NET Objects 4.0

Pop3.BeginConnect Method 

Begins an asynchronous request for a connecting to a POP3 server.

public IAsyncResult BeginConnect(
   string serverName,
   int port,
   bool pipelining,
   AsyncCallback callback,
   object state
);

Parameters

serverName
The name or IP address of the POP3 server.
port
The port on which to communicate with the server. The standard POP3 port is 110.
pipelining
Specifies whether to use commands pipelining if it's supported by the server.
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 connection.

Remarks

This method is an asynchronous version of Connect.

A reference to the state object will be available in the events raised by this method through the State property value. This is also valid for the rest of asynchronous methods in MailBee.

Exceptions

Exception TypeCondition
MailBeeInvalidStateExceptionThere is already an operation in progress.

Example

Asynchronous connecting to a POP3 server in WinForms application. This sample also handles Connected event. Wait method is used to wait for the asynchronous method completion, since .NET's standard WaitOne cannot process events.

[C#]
// To use the code below, import MailBee namespaces at the top of your code
using MailBee;
using MailBee.Pop3Mail;

// Put the code below inside your class

// "Connected" event handler
private void OnConnected(object sender, ConnectedEventArgs e)
{
    MessageBox.Show("Connected to the server");
}

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

    // Let MailBee process events
    pop.RaiseEventsViaMessageLoop = false;

    pop.Connected += new ConnectedEventHandler(OnConnected);

    // Initiate an asynchronous connection
    pop.BeginConnect("pop.somehost.com", 110, true, null, null);

    // Simulate some lengthy work here...
    for (int i = 0; i < 100; i++)
    {
        // Make a portion of the work
        System.Threading.Thread.Sleep(10);

        // Process events which were raised during execution of the work above
        pop.Wait(0);
    }

    // If the connection was not established during execution of the lengthy 
    // work, wait until it's established
    pop.Wait();

    // End the connection request
    pop.EndConnect();

    // Connected to the server!

    // Disconnect from the server
    pop.Disconnect();
}
[Visual Basic]
' To use the code below, import MailBee namespaces at the top of your code
Imports MailBee
Imports MailBee.Pop3Mail

' Put the code below inside your class.

' "Connected" event handler
Private Sub OnConnected(ByVal sender As System.Object, ByVal e As ConnectedEventArgs)
    MsgBox("Connected to the server")
End Sub

' The actual code
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As ConnectedEventArgs)
    Dim pop As New Pop3

    ' Let MailBee process events
    pop.RaiseEventsViaMessageLoop = False

    AddHandler pop.Connected, AddressOf OnConnected

    ' Initiate an asynchronous connection
    pop.BeginConnect("pop.somehost.com", 110, True, Nothing, Nothing)

    ' Simulate some lengthy work here...
    Dim i As Integer
    For i = 1 To 100
        ' Make a portion of the work
        System.Threading.Thread.Sleep(10)
        
        ' Process events which were raised during execution of the work above
        pop.Wait(0)
    Next
    
    ' If the connection was not established during execution of the lengthy 
    ' work, wait until it's established
    pop.Wait()
    
    ' End the connection request
    pop.EndConnect()
    
    ' Connected to the server!
    
    ' Disconnect from the server
    pop.Disconnect()
End Sub
Asynchronous connecting to a POP3 server in non-WinForms application. Applications which do not have message loop (such as console or web applications) can raise events on any thread and do not require any special events processing.
[C#]
using System;
using MailBee;
using MailBee.Pop3Mail;
using MailBee.Mime;

class Sample
{
    // "Connected" event handler
    private static void OnConnected(object sender, ConnectedEventArgs e)
    {
        Console.WriteLine("Connected to the server");
    }

    // The actual code
    static void Main(string[] args)
    {
        Pop3 pop = new Pop3();

        pop.Connected += new ConnectedEventHandler(OnConnected);

        // Initiate an asynchronous connection
        pop.BeginConnect("pop.somehost.com", 110, true, null, null);

        // Simulate some lengthy work here...
        System.Threading.Thread.Sleep(1000);

        // If the connection was not established during execution of the lengthy work,
        // wait until it's established, and end the connection request
        pop.EndConnect();

        // Connected to the server!

        // Disconnect from the server
        pop.Disconnect();
    }
}
[Visual Basic]
Imports System
Imports MailBee
Imports MailBee.Pop3Mail
Imports MailBee.Mime

Public Class Sample
    ' "Connected" event handler
    Public Shared Sub OnConnected(ByVal sender As System.Object, ByVal e As ConnectedEventArgs)
        Console.WriteLine("Connected to the server")
    End Sub
    
    ' The actual code
    Shared Sub Main(ByVal args As String())
        Dim pop As New Pop3
        AddHandler pop.Connected, AddressOf OnConnected

        ' Initiate an asynchronous connection
        pop.BeginConnect("pop.somehost.com", 110, True, Nothing, Nothing)

        ' Simulate some lengthy work here...
        System.Threading.Thread.Sleep(1000)

        ' If the connection was not established during execution of the lengthy work,
        ' wait until it's established, and end the connection request
        pop.EndConnect()

        ' Connected to the server!

        ' Disconnect from the server
        pop.Disconnect()
    End Sub
End Class

See Also

Pop3 Class | MailBee.Pop3Mail Namespace | Connect | RaiseEventsViaMessageLoop | Wait