예제 #1
0
        private void LoginPop3(bool enableUtf8 = true)
        {
            var secureSocketOptions = SecureSocketOptions.Auto;
            var sslProtocols        = SslProtocols.Default;

            switch (Account.Encryption)
            {
            case EncryptionType.StartTLS:
                secureSocketOptions = SecureSocketOptions.StartTlsWhenAvailable;
                sslProtocols        = SslProtocols.Tls | SslProtocols.Tls11 | SslProtocols.Tls12;
                break;

            case EncryptionType.SSL:
                secureSocketOptions = SecureSocketOptions.SslOnConnect;
                sslProtocols        = SslProtocols.Tls | SslProtocols.Tls11 | SslProtocols.Tls12;
                break;

            case EncryptionType.None:
                secureSocketOptions = SecureSocketOptions.None;
                sslProtocols        = SslProtocols.None;
                break;
            }

            Log.Debug("Pop.Connect({0}:{1}, {2})", Account.Server, Account.Port,
                      Enum.GetName(typeof(SecureSocketOptions), secureSocketOptions));
            try
            {
                Pop.SslProtocols = sslProtocols;

                var t = Pop.ConnectAsync(Account.Server, Account.Port, secureSocketOptions, CancelToken);

                if (!t.Wait(CONNECT_TIMEOUT, CancelToken))
                {
                    throw new TimeoutException("Pop.ConnectAsync timeout");
                }

                if (enableUtf8 && (Pop.Capabilities & Pop3Capabilities.UTF8) != Pop3Capabilities.None)
                {
                    Log.Debug("Pop.EnableUTF8");

                    t = Pop.EnableUTF8Async(CancelToken);

                    if (!t.Wait(ENABLE_UTF8_TIMEOUT, CancelToken))
                    {
                        throw new TimeoutException("Pop.EnableUTF8Async timeout");
                    }
                }

                Pop.Authenticated += PopOnAuthenticated;

                if (string.IsNullOrEmpty(Account.OAuthToken))
                {
                    Log.Debug("Pop.Authentication({0})", Account.Account);

                    Pop.AuthenticationMechanisms.Remove("XOAUTH2");

                    t = Pop.AuthenticateAsync(Account.Account, Account.Password, CancelToken);
                }
                else
                {
                    Log.Debug("Pop.AuthenticationByOAuth({0})", Account.Account);

                    t = Pop.AuthenticateAsync(Account.Account, Account.AccessToken, CancelToken);
                }

                if (!t.Wait(LOGIN_TIMEOUT, CancelToken))
                {
                    Pop.Authenticated -= PopOnAuthenticated;
                    throw new TimeoutException("Pop.AuthenticateAsync timeout");
                }

                Pop.Authenticated -= PopOnAuthenticated;
            }
            catch (AggregateException aggEx)
            {
                if (aggEx.InnerException != null)
                {
                    throw aggEx.InnerException;
                }
                throw new Exception("LoginPop3 failed", aggEx);
            }
        }