/// <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);
            }
        }
 /// <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;
 }