MailBee.NET Objects 4.0

Imap.BeginLogin Method 

Begins an asynchronous request for a logging in an account on an IMAP4 server.

public IAsyncResult BeginLogin(
   string accountName,
   string password,
   AuthenticationMethods authMethods,
   AuthenticationOptions authOptions,
   SaslMethod authUserDefined,
   AsyncCallback callback,
   object state
);

Parameters

accountName
The user account name on the server.
password
The password of the user account on the server.
authMethods
A set of authentication methods which can be used when logging in a mailbox.
authOptions
Specifies the options which affect login process.
authUserDefined
A reference to the instance of user defined authentication method, or a null reference (Nothing in Visual Basic) if user defined authentication is not used.
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 login process.

Remarks

This method is an asynchronous version of Login.

Exceptions

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

Example

This sample demonstrates asynchronous logging in an account and use of a callback function in a console application.

[C#]
using System;
using MailBee;
using MailBee.ImapMail;

class Sample
{
    // A callback function.
    private static void LoginCallback(IAsyncResult result)
    {
        Imap imp = (Imap)result.AsyncState;
        imp.EndLogin();
        imp.SelectFolder("INBOX");
        Console.WriteLine("Message #" + imp.Unseen + " is first unseen");
    }

    // The actual code.
    static void Main(string[] args)
    {
        Imap imp = new Imap();

        imp.Connect("imap.somehost.com");

        // Initiate an asynchronous login attempt.
        IAsyncResult ar = imp.BeginLogin("jdoe", "secret",
            AuthenticationMethods.Auto, AuthenticationOptions.None,
            null, new AsyncCallback(LoginCallback), imp);

        // Simulate some lengthy work here. At the same time,
        // login is executed on another thread.
        System.Threading.Thread.Sleep(3000);

        // If the login attempt is still in progress, then wait until it's finished.
        while (imp.IsBusy) ar.AsyncWaitHandle.WaitOne();

        // Disconnect from the server.
        imp.Disconnect();
    }
}
[Visual Basic]
Imports System
Imports MailBee
Imports MailBee.ImapMail

Class Sample
    ' A callback function.
    Private Shared Sub LoginCallback(ByVal result As IAsyncResult)
        Dim imp As New Imap
        imp = result.AsyncState
        imp.EndLogin()
        imp.SelectFolder("INBOX")
        Console.WriteLine("Message #" & imp.Unseen & " is first unseen")
    End Sub

    ' The actual code.
    Shared Sub Main(ByVal args As String())
        Dim imp As New Imap
        
        imp.Connect("iamp.somehost.com")
        
        ' Initiate an asynchronous login attempt.
        Dim ar = imp.BeginLogin("jdoe", "secret", _
            AuthenticationMethods.Auto, AuthenticationOptions.None, _
            Nothing, New AsyncCallback(AddressOf LoginCallback), imp)

        ' Simulate some lengthy work here. At the same time 
        ' login is executed on another thread.
        System.Threading.Thread.Sleep(3000)

        ' If the login attempt is still in progress, then wait until it's finished.
        While imp.IsBusy
            ar.AsyncWaitHandle.WaitOne()
        End While

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

See Also

Imap Class | MailBee.ImapMail Namespace | Login | BeginConnect | SaslMethod