Click or drag to resize
MimeKit

MailServiceConnect(Uri, CancellationToken) Method

Establish a connection to the specified mail server.

Namespace: MailKit
Assembly: MailKit (in MailKit.dll) Version: 4.10.0
Syntax
C#
public void Connect(
	Uri uri,
	CancellationToken cancellationToken = default
)

Parameters

uri  Uri
The server URI.
cancellationToken  CancellationToken  (Optional)
The cancellation token.
Exceptions
ExceptionCondition
ArgumentNullException The uri is .
ArgumentException The uri is not an absolute URI.
ObjectDisposedException The MailService has been disposed.
InvalidOperationException The MailService is already connected.
OperationCanceledException The operation was canceled via the cancellation token.
SocketException A socket error occurred trying to connect to the remote host.
IOException An I/O error occurred.
ProtocolException A protocol error occurred.
Remarks
Establishes a connection to the specified mail server.
Example
C#
public static void SendMessage (MimeMessage message)
{
    using (var client = new SmtpClient ()) {
        // Note: since GMail requires SSL at connection time, use the "smtps"
        // protocol instead of "smtp".
        var uri = new Uri ("smtps://smtp.gmail.com:465");

        client.Connect (uri);

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

        client.Send (message);

        client.Disconnect (true);
    }
}
See Also