Smtp ClientConnect Async(Stream, String, Int 32, Secure Socket Options, Cancellation Token) Method
Asynchronously establish a connection to the specified SMTP or SMTP/S server using the provided socket.
Definition
Namespace: MailKit.Net.Smtp
Assembly: MailKit (in MailKit.dll) Version: 5.0.0
An asynchronous task context.
IMailServiceConnectAsync(Stream, String, Int32, SecureSocketOptions, CancellationToken)
Assembly: MailKit (in MailKit.dll) Version: 5.0.0
C#
public override Task ConnectAsync(
Stream stream,
string host,
int port = 0,
SecureSocketOptions options = SecureSocketOptions.Auto,
CancellationToken cancellationToken = default
)Parameters
- stream Stream
- The stream to use for the connection.
- host String
- The host name to connect to.
- port Int32 (Optional)
- The port to connect to. If the specified port is 0, then the default port will be used.
- options SecureSocketOptions (Optional)
- The secure socket options to when connecting.
- cancellationToken CancellationToken (Optional)
- The cancellation token.
Return Value
TaskAn asynchronous task context.
Implements
IMailServiceConnectAsync(Stream, String, Int32, SecureSocketOptions, CancellationToken)IMailServiceConnectAsync(Stream, String, Int32, SecureSocketOptions, CancellationToken)
Remarks
Establishes a connection to the specified SMTP or SMTP/S server using the provided socket.
If the options has a value of Auto, then the port is used to determine the default security options. If the port has a value of 465, then the default options used will be SslOnConnect. All other values will use StartTlsWhenAvailable.
Once a connection is established, properties such as AuthenticationMechanisms and Capabilities will be populated.
Exceptions
| ArgumentNullException |
stream is . -or- host is . |
| ArgumentOutOfRangeException | port is not between 0 and 65535. |
| ArgumentException | The host is a zero-length string. |
| ObjectDisposedException | The SmtpClient has been disposed. |
| InvalidOperationException | The SmtpClient is already connected. |
| NotSupportedException | options was set to StartTls and the SMTP server does not support the STARTTLS extension. |
| OperationCanceledException | The operation was canceled. |
| SslHandshakeException | An error occurred during the SSL/TLS negotiations. |
| IOException | An I/O error occurred. |
| SmtpCommandException | An SMTP command failed. |
| SmtpProtocolException | An SMTP protocol error occurred. |