Click or drag to resize
MimeKit

MailServiceConnect Method (Uri, CancellationToken)

Establish a connection to the specified mail server.

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

Parameters

uri
Type: SystemUri
The server URI.
cancellationToken (Optional)
Type: System.ThreadingCancellationToken
The cancellation token.
Exceptions
ExceptionCondition
ArgumentNullException The uri is null.
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.
Examples
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