/// <summary>
        /// Creates <see cref="ImapClient"/> instance.
        /// </summary>
        /// <param name="credentials"><see cref="MailCredentials"/> instance.</param>
        /// <param name="errorMessages"><see cref="ImapErrorMessages"/> instance.</param>
        /// <param name="userConnection"><see cref="UserConnection"/> instance.</param>
        /// <param name="login">Flag indicates if need to login to imap server.</param>
        public ImapClient(MailCredentials credentials, ImapErrorMessages errorMessages, UserConnection userConnection, bool login = true)
        {
            _userConnection     = userConnection;
            _client             = CreateImapClient(credentials);
            _remoteChangesCount = 0;
            LocalChangesCount   = 0;
            string errorMessage = string.Empty;

            try {
                _currentMailboxName = credentials.SenderEmailAddress;
                if (login)
                {
                    string oauthClassName = GetOAuthClientClassNameBySender(_currentMailboxName);
                    if (oauthClassName.IsNotNullOrEmpty())
                    {
                        errorMessage = LocInvalidOAuthCredentials.ToString();
#if !NETSTANDARD2_0 // TODO #CRM-42481
                        OAuthClient oauthClient = (OAuthClient)Activator.CreateInstance(Type.GetType("Terrasoft.Configuration." + oauthClassName), credentials.SenderEmailAddress, userConnection);
                        string      token       = oauthClient.GetAccessToken();
#else
                        string token = string.Empty;
#endif
                        string xoAuthKey = OAuth2.GetXOAuthKeyStatic(credentials.SenderEmailAddress, token);
                        _client.Login(credentials.UserName, xoAuthKey, AuthenticationMethods.SaslOAuth2, MailBee.AuthenticationOptions.None, null);
                    }
                    else
                    {
                        errorMessage = LocLoginOrPwdWrong.ToString();
                        _client.Login(credentials.UserName, credentials.UserPassword);
                    }
                }
            } catch (Exception ex) {
                throw new ImapException(errorMessage, ex);
            }
        }
        private Imap CreateImapClient(MailCredentials credentials)
        {
            Imap client = new Imap();

            SetImapLogging(client, credentials.SenderEmailAddress);
            if (credentials.StartTls)
            {
                client.SslMode = SslStartupMode.UseStartTlsIfSupported;
            }
            client.SslProtocol = SecurityProtocol.Auto;
            client.Timeout     = credentials.Timeout > 0 ? credentials.Timeout : _defaultCredentialsTimeout;
            try {
                if (credentials.Port > 0)
                {
                    client.Connect(credentials.Host, credentials.Port);
                }
                else
                {
                    client.Connect(credentials.Host);
                }
            } catch (Exception ex) {
                throw new ImapException(LocCanNotConnect.ToString(), ex);
            }
            if (credentials.UseSsl && !client.IsSslConnection)
            {
                throw new ImapException(LocServerNotSupportSslConnection);
            }
            return(client);
        }
 /// <summary>
 /// Creates <see cref="ImapClient"/> instance.
 /// </summary>
 /// <param name="credentials"><see cref="MailCredentials"/> instance.</param>
 /// <param name="errorMessages"><see cref="ImapErrorMessages"/> instance.</param>
 /// <param name="userConnection"><see cref="UserConnection"/> instance.</param>
 /// <param name="mailboxSettingsId">Mailbox synchronization settings unique identifier.</param>
 /// <param name="login">Flag indicates if need to login to imap server.</param>
 public ImapClient(MailCredentials credentials, ImapErrorMessages errorMessages, UserConnection userConnection, Guid mailboxSettingsId,
                   bool login = true)
     : this(credentials, errorMessages, userConnection, login)
 {
     CurrentMailboxSettingsId = mailboxSettingsId;
 }