Click or drag to resize
MimeKit

IMailServiceAuthenticate(String, String, CancellationToken) Method

Authenticate using the specified user name and password.

Namespace: MailKit
Assembly: MailKit (in MailKit.dll) Version: 4.7.1
Syntax
C#
void Authenticate(
	string userName,
	string password,
	CancellationToken cancellationToken = default
)

Parameters

userName  String
The user name.
password  String
The password.
cancellationToken  CancellationToken  (Optional)
The cancellation token.
Exceptions
ExceptionCondition
ArgumentNullException

userName is null.

-or-

password is null.

ObjectDisposedException The MailService has been disposed.
InvalidOperationException The MailService is not connected or is already authenticated.
OperationCanceledException The operation was canceled via the cancellation token.
AuthenticationException Authentication using the supplied credentials has failed.
SaslException A SASL authentication error occurred.
IOException An I/O error occurred.
ProtocolException A protocol error occurred.
Remarks

Authenticates using the supplied credentials.

If the server supports one or more SASL authentication mechanisms, then the SASL mechanisms that both the client and server support (not including any OAUTH mechanisms) are tried in order of greatest security to weakest security. Once a SASL authentication mechanism is found that both client and server support, the credentials are used to authenticate.

If the server does not support SASL or if no common SASL mechanisms can be found, then the default login command is used as a fallback.

Tip  Tip
To prevent the usage of certain authentication mechanisms, simply remove them from the AuthenticationMechanisms hash set before calling this method.
Example
C#
public static void SendMessage (MimeMessage message)
{
    using (var client = new SmtpClient ()) {
        client.Connect ("smtp.gmail.com", 465, SecureSocketOptions.SslOnConnect);

        client.Authenticate ("username", "password");

        client.Send (message);

        client.Disconnect (true);
    }
}
See Also