MailBee.NET Objects 4.0

Smtp.Login Method 

Authenticates the user on the SMTP server if ESMTP authentication is enabled.

public bool Login();

Return Value

true if the method succeeds, ESMTP authentication was disabled, or IgnoreLoginFailure is true; otherwise, false.

Remarks

If ESMTP auhtentication is enabled (AuthMethods is not None), this method will attempt to authenticate on the SMTP server using the best (most secure) authentication method specified in AuthMethods value and supported by the server.

If the login attempt fails, it might still be possible to send mail (with some restrictions, such as sending to local recipients only). Due to this, MailBee makes it possible to ignore authentication errors if IgnoreLoginFailure is set to true. ErrorOccurred warning event, however, will still be issued to let the application know about the problem.

Note   Another alternative to Login method is using POP-before-SMTP authentication. It can be used if the mail server does not support ESMTP authentication, but still requires users to authnticate themselves in order to send mail. See AuthPopBeforeSmtp method documentation for more information.

Exceptions

Exception TypeCondition
MailBeeExceptionAn error occurred and ThrowExceptions is true.

Example

This sample connects to the SMTP server, sends greeting, and attempts to authenticate the user on the server. If the login fails, no exception is thrown (IgnoreLoginFailure is set to true) but the application still gets notification about the login failure through handling ErrorOccurred event).

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

class Sample
{
    // ErrorOccurred event handler.
    private static void OnErrorOccurred(object sender, ErrorEventArgs e)
    {
        if (e.Reason is IMailBeeLoginException)
        {
            Console.WriteLine(e.Reason.Message); // Login error.
        }
    }

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

        // Specify SMTP server and authentication settings.
        SmtpServer server = new SmtpServer("smtp.somedomain.com", "jdoe", "badpassword");
        server.IgnoreLoginFailure = true;
        mailer.SmtpServers.Add(server);

        // Subscribe to the ErrorOccurred event.
        mailer.ErrorOccurred += new ErrorEventHandler(OnErrorOccurred);

        mailer.Connect();
        mailer.Hello();

        // Authenticate the user "jdoe".
        mailer.Login();

        mailer.Disconnect();
    }
}
[Visual Basic]
Imports System
Imports MailBee
Imports MailBee.SmtpMail

Class Sample
    ' ErrorOccurred event handler.
    Private Shared Sub OnErrorOccurred(ByVal sender As Object, ByVal e As ErrorEventArgs)
        If (TypeOf e.Reason Is IMailBeeLoginException) Then
            Console.WriteLine(e.Reason.Message) ' Login error.
        End If
    End Sub

    ' The actual code.
    Shared Sub Main()
        Dim mailer As New Smtp

        ' Specify SMTP server and authentication settings.
        Dim server As New SmtpServer("smtp.somedomain.com", "jdoe", "badpassword")
        server.IgnoreLoginFailure = True
        mailer.SmtpServers.Add(server)

        ' Subscribe to the ErrorOccurred event.
        AddHandler mailer.ErrorOccurred, AddressOf OnErrorOccurred

        mailer.Connect()
        mailer.Hello()

        ' Authenticate the user "jdoe".
        mailer.Login()

        mailer.Disconnect()
    End Sub
End Class

See Also

Smtp Class | MailBee.SmtpMail Namespace | AuthPopBeforeSmtp