MailBee.NET Objects 7.2

Imap.LoggedIn Event

Occurs when the component successfully authenticates the user on the server and logs in the user account.

public event LoggedInEventHandler LoggedIn;

Event Data

The event handler receives an argument of type LoggedInEventArgs containing data related to this event. The following LoggedInEventArgs properties provide information specific to this event.

Property Description
Protocol Gets application-level protocol of the current connection.
RemoteEndPoint Gets a reference to the end point of the server host.
RemoteHostName Gets the host name of the server.
State Gets a reference to the object which was supplied by the developer in state parameter of asynchronous methods of the mailer components.

Remarks

Usually, this event is raised on successful completion of Login method call. However, it's also raised when the server authenticates the client automatically (by external means, such as if the client connects from the trusted IP address) and sends PREAUTH response.

Example

This sample connects to the IMAP4 server and logs in the user account. The sample also checks if the IsLoggedIn status was set to true after calling Connect. This is required to make sure the client has not already been authenticated via PREAUTH.

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

class Sample
{
    private static string accountName = "Preauthenticated user";

    // LoggedIn event handler.
    private static void OnLoggedIn(object sender, LoggedInEventArgs e)
    {
        Console.WriteLine("Logged in as " + accountName);
    }

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

        // Subscribe to the LoggedIn event.
        imp.LoggedIn += new LoggedInEventHandler(OnLoggedIn);

        // Connect to the server. It's possible the server will automatically
        // authenticate us in some cases. In other samples, we do not check
        // if the auto-login occurred but real-world applications should check this.
        imp.Connect("imap4.somedomain.com");

        if (!imp.IsLoggedIn)
        {
            // Authenticate the user via Login()
            accountName = "jdoe";
            imp.Login(accountName, "secret");
        }

        imp.Disconnect();
    }
}
[Visual Basic]
Imports System
Imports MailBee
Imports MailBee.ImapMail

Module Sample
    Dim accountName As String = "Preauthenticated user"

    ' LoggedIn event handler.
    Private Sub OnLoggedIn(ByVal sender As Object, ByVal e As LoggedInEventArgs)
        Console.WriteLine("Logged in as " & accountName)
    End Sub

    ' The actual code.
    Sub Main(ByVal args As String())
        Dim imp As New Imap

        ' Subscribe to the LoggedIn event.
        AddHandler imp.LoggedIn, AddressOf OnLoggedIn

        ' Connect to the server. It's possible the server will automatically
        ' authenticate us in some cases. In other samples, we do not check
        ' if the auto-login occurred but real-world applications should check this.
        imp.Connect("imap4.somedomain.com")

        If Not imp.IsLoggedIn Then
            ' Authenticate the user via Login()
            accountName = "jdoe"
            imp.Login(accountName, "secret")
        End If

        imp.Disconnect()
    End Sub
End Module

See Also

Imap Class | MailBee.ImapMail Namespace | Login