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

Namespace: MailBee.SmtpMail
Assembly: MailBee.NET (in MailBee.NET.dll) Version: 12.3.1 build 666 for .NET 4.5
public bool Login()

Return Value

Type: Boolean
true if the method succeeds, ESMTP authentication was disabled, or IgnoreLoginFailure is true; otherwise, false.
MailBeeExceptionAn error occurred and ThrowExceptions is true.

By default, this method does nothing because authentication is optional in SMTP/ESMTP.

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 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(String, Int32, String, String) method documentation for more information.

For examples on using OAuth 2.0 authentication (XOAUTH2 in Gmail), see OAuth2 topic.

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).
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.
        // This constructor also sets server.AuthMethods = AuthenticationMethods.Auto
        SmtpServer server = new SmtpServer("", "jdoe", "badpassword");
        server.IgnoreLoginFailure = true;

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


        // Authenticate the user "jdoe".

See Also