Pop 3ClientConnect(String, Int 32, Secure Socket Options, Cancellation Token) Method
Establish a connection to the specified POP3 or POP3/S server.
Definition
Namespace: MailKit.Net.Pop3
Assembly: MailKit (in MailKit.dll) Version: 5.0.0
IMailServiceConnect(String, Int32, SecureSocketOptions, CancellationToken)
Assembly: MailKit (in MailKit.dll) Version: 5.0.0
C#
public override void Connect(
string host,
int port = 0,
SecureSocketOptions options = SecureSocketOptions.Auto,
CancellationToken cancellationToken = default
)Parameters
- 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.
Implements
IMailServiceConnect(String, Int32, SecureSocketOptions, CancellationToken)IMailServiceConnect(String, Int32, SecureSocketOptions, CancellationToken)
Remarks
Establishes a connection to the specified POP3 or POP3/S server.
If the port has a value of 0, then the options parameter is used to determine the default port to connect to. The default port used with SslOnConnect is 995. All other values will use a default port of 110.
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 995, 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.
Example
C#
public static void DownloadMessages ()
{
using (var client = new Pop3Client ()) {
client.Connect ("pop.gmail.com", 995, SecureSocketOptions.SslOnConnect);
client.Authenticate ("username", "password");
for (int i = 0; i < client.Count; i++) {
var message = client.GetMessage (i);
// write the message to a file
message.WriteTo (string.Format ("{0}.eml", i));
// mark the message for deletion
client.DeleteMessage (i);
}
client.Disconnect (true);
}
}
Exceptions
| ArgumentNullException | host is . |
| ArgumentOutOfRangeException | port is not between 0 and 65535. |
| ArgumentException | The host is a zero-length string. |
| ObjectDisposedException | The Pop3Client has been disposed. |
| InvalidOperationException | The Pop3Client is already connected. |
| OperationCanceledException | The operation was canceled via the cancellation token. |
| NotSupportedException | options was set to StartTls and the POP3 server does not support the STLS extension. |
| SocketException | A socket error occurred trying to connect to the remote host. |
| SslHandshakeException | An error occurred during the SSL/TLS negotiations. |
| IOException | An I/O error occurred. |
| Pop3CommandException | A POP3 command failed. |
| Pop3ProtocolException | A POP3 protocol error occurred. |