ImapBeginConnect Method

Note: This API is now obsolete.

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

Namespace: MailBee.ImapMail
Assembly: MailBee.NET (in MailBee.NET.dll) Version: 11.2.0 build 590 for .NET 4.5
Syntax
[ObsoleteAttribute("This method is obsolete in .NET 4.5+. Use ConnectAsync instead.")]
public IAsyncResult BeginConnect(
	string serverName,
	int port,
	Socket socketToUse,
	EndPoint localEndPoint,
	AsyncCallback callback,
	Object state
)

Parameters

serverName
Type: SystemString
The name or IP address of the IMAP4 server.
port
Type: SystemInt32
The port on which to communicate with the server. The standard IMAP4 port is 143.
socketToUse
Type: System.Net.SocketsSocket
Usually, a null reference (Nothing in Visual Basic). You can supply your own socket object here if you wish MailBee to use this object instead of creating a new one (typically, to fine-tune the WinSock subsystem when you need to deal with thousands of connections).
localEndPoint
Type: System.NetEndPoint
Usually, a null reference. Set it if you need to bind the connection to a certain local IP address/port to fine-tune the performance under heavy loads. If you supply your own socket object (via socketToUse argument), make sure you EITHER bind the local end point to this socket object in your own code (and pass a null reference as localEndPoint value) OR simply pass the local end point as localEndPoint value (not calling socket.Bind in your own code). Also, you can use no local end point at all. Just make sure you're not binding twice: directly in your code and via passing the local end point to this method.
callback
Type: SystemAsyncCallback
The AsyncCallback delegate. You can leave it a null reference 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 connection.
Exceptions
ExceptionCondition
MailBeeInvalidStateExceptionThere is already an operation in progress.
Remarks

This method is an asynchronous version of Connect(String, Int32, Socket, EndPoint).

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.

Examples
Asynchronous connecting to an IMAP4 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(Int32, Boolean) cannot process events.
// 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.

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

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

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

    imp.Connected += new ConnectedEventHandler(OnConnected);

    // Initiate an asynchronous connection.
    imp.BeginConnect("imap.somehost.com", 143, null, null, 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.
        imp.Wait(0);
    }

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

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

    // Connected to the server!

    // Disconnect from the server.
    imp.Disconnect();
}
Examples
Asynchronous connecting to an IMAP4 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.
using System;
using MailBee;
using MailBee.ImapMail;
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)
    {
        Imap imp = new Imap();

        imp.Connected += new ConnectedEventHandler(OnConnected);

        // Initiate an asynchronous connection.
        imp.BeginConnect("imap.somehost.com", 143, null, null, 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.
        imp.EndConnect();

        // Connected to the server!

        // Disconnect from the server.
        imp.Disconnect();
    }
}
See Also