Click or drag to resize
MimeKit

MailServiceConnect Method (String, Int32, SecureSocketOptions, CancellationToken)

Establish a connection to the specified mail server.

Namespace:  MailKit
Assembly:  MailKit (in MailKit.dll) Version: 2.3.0
Syntax
C#
public abstract void Connect(
	string host,
	int port = 0,
	SecureSocketOptions options = SecureSocketOptions.Auto,
	CancellationToken cancellationToken = null
)

Parameters

host
Type: SystemString
The host name to connect to.
port (Optional)
Type: SystemInt32
The port to connect to. If the specified port is 0, then the default port will be used.
options (Optional)
Type: MailKit.SecuritySecureSocketOptions
The secure socket options to when connecting.
cancellationToken (Optional)
Type: System.ThreadingCancellationToken
The cancellation token.

Implements

IMailServiceConnect(String, Int32, SecureSocketOptions, CancellationToken)
Exceptions
ExceptionCondition
ArgumentNullExceptionhost is null.
ArgumentOutOfRangeExceptionport is not between 0 and 65535.
ArgumentException The host is a zero-length string.
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 ()) {
        client.Connect ("smtp.gmail.com", 465, SecureSocketOptions.SslOnConnect);

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

        client.Send (message);

        client.Disconnect (true);
    }
}
See Also