Mail ServiceConnect(Uri, Cancellation Token) Method
Establish a connection to the specified mail server.
Definition
Namespace: MailKit
Assembly: MailKit (in MailKit.dll) Version: 5.0.0
Assembly: MailKit (in MailKit.dll) Version: 5.0.0
C#
public void Connect(
Uri uri,
CancellationToken cancellationToken = default
)Parameters
- uri Uri
- The server URI.
- cancellationToken CancellationToken (Optional)
- The cancellation token.
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);
}
}
Exceptions
| 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. |