/// <summary>
/// Establishes a connection to the specified POP3 server.
/// </summary>
/// <remarks>
/// <para>Establishes a connection to an POP3 or POP3/S server. If the schema
/// in the uri is "pop", a clear-text connection is made and defaults to using
/// port 110 if no port is specified in the URI. However, if the schema in the
/// uri is "pops", an SSL connection is made using the
/// <see cref="ClientCertificates"/> and defaults to port 995 unless a port
/// is specified in the URI.</para>
/// <para>It should be noted that when using a clear-text POP3 connection,
/// if the server advertizes support for the STLS extension, the client
/// will automatically switch into TLS mode before authenticating unless
/// the <paramref name="uri"/> contains a query string to disable it.</para>
/// If a successful connection is made, the <see cref="AuthenticationMechanisms"/>
/// and <see cref="Capabilities"/> properties will be populated.
/// </remarks>
/// <param name="uri">The server URI. The <see cref="System.Uri.Scheme"/> should either
/// be "pop" to make a clear-text connection or "pops" to make an SSL connection.</param>
/// <param name="cancellationToken">A cancellation token.</param>
/// <exception cref="System.ArgumentNullException">
/// The <paramref name="uri"/> is <c>null</c>.
/// </exception>
/// <exception cref="System.ArgumentException">
/// The <paramref name="uri"/> is not an absolute URI.
/// </exception>
/// <exception cref="System.ObjectDisposedException">
/// The <see cref="Pop3Client"/> has been disposed.
/// </exception>
/// <exception cref="System.InvalidOperationException">
/// The <see cref="Pop3Client"/> is already connected.
/// </exception>
/// <exception cref="System.OperationCanceledException">
/// The operation was canceled via the cancellation token.
/// </exception>
/// <exception cref="System.IO.IOException">
/// An I/O error occurred.
/// </exception>
/// <exception cref="Pop3CommandException">
/// A POP3 command failed.
/// </exception>
/// <exception cref="Pop3ProtocolException">
/// A POP3 protocol error occurred.
/// </exception>
public void Connect(Uri uri, CancellationToken cancellationToken)
{
CheckDisposed ();
if (uri == null)
throw new ArgumentNullException ("uri");
if (!uri.IsAbsoluteUri)
throw new ArgumentException ("The uri must be absolute.", "uri");
if (IsConnected)
throw new InvalidOperationException ("The Pop3Client is already connected.");
var scheme = uri.Scheme.ToLowerInvariant ();
var pops = scheme == "pops" || scheme == "pop3s";
var port = uri.Port > 0 ? uri.Port : (pops ? 995 : 110);
var query = uri.ParsedQuery ();
#if !NETFX_CORE && !WINDOWS_APP && !WINDOWS_PHONE_APP
var ipAddresses = Dns.GetHostAddresses (uri.DnsSafeHost);
Socket socket = null;
#endif
Stream stream;
string value;
var starttls = !pops && (!query.TryGetValue ("starttls", out value) || Convert.ToBoolean (value));
#if !NETFX_CORE && !WINDOWS_APP && !WINDOWS_PHONE_APP
for (int i = 0; i < ipAddresses.Length; i++) {
socket = new Socket (ipAddresses[i].AddressFamily, SocketType.Stream, ProtocolType.Tcp);
cancellationToken.ThrowIfCancellationRequested ();
try {
socket.Connect (ipAddresses[i], port);
break;
} catch (Exception) {
if (i + 1 == ipAddresses.Length)
throw;
}
}
if (pops) {
var ssl = new SslStream (new NetworkStream (socket, true), false, ValidateRemoteCertificate);
ssl.AuthenticateAsClient (uri.Host, ClientCertificates, SslProtocols.Default, true);
stream = ssl;
} else {
stream = new NetworkStream (socket, true);
}
#else
socket = new StreamSocket ();
cancellationToken.ThrowIfCancellationRequested ();
socket.ConnectAsync (new HostName (uri.DnsSafeHost), port.ToString (), pops ? SocketProtectionLevel.Tls12 : SocketProtectionLevel.PlainSocket)
.AsTask (cancellationToken)
.GetAwaiter ()
.GetResult ();
stream = new DuplexStream (socket.InputStream.AsStreamForRead (), socket.OutputStream.AsStreamForWrite ());
#endif
probed = ProbedCapabilities.None;
host = uri.Host;
logger.LogConnect (uri);
engine.Connect (new Pop3Stream (stream, logger), cancellationToken);
engine.QueryCapabilities (cancellationToken);
if (starttls && (engine.Capabilities & Pop3Capabilities.StartTLS) != 0) {
SendCommand (cancellationToken, "STLS");
#if !NETFX_CORE && !WINDOWS_APP && !WINDOWS_PHONE_APP
var tls = new SslStream (stream, false, ValidateRemoteCertificate);
tls.AuthenticateAsClient (uri.Host, ClientCertificates, SslProtocols.Tls, true);
engine.Stream.Stream = tls;
#else
socket.UpgradeToSslAsync (SocketProtectionLevel.Tls12, new HostName (uri.DnsSafeHost))
.AsTask (cancellationToken)
.GetAwaiter ()
.GetResult ();
#endif
// re-issue a CAPA command
engine.QueryCapabilities (cancellationToken);
}
}