public MailAccountData CreateAccountSimple(string email, string password)
        {
            if (string.IsNullOrEmpty(email)) throw new ArgumentException(@"Empty email", "email");

            string errorText = null;
            MailBox mbox = null;
            try
            {
                mbox = MailBoxManager.SearchMailboxSettings(email, password, Username, TenantId);
            }
            catch (ImapConnectionException exImap)
            {
                errorText = GetFormattedTextError(exImap, ServerType.Imap, exImap is ImapConnectionTimeoutException);
            }
            catch (Pop3ConnectionException exPop)
            {
                errorText = GetFormattedTextError(exPop, ServerType.Pop3, exPop is Pop3ConnectionTimeoutException);
            }
            catch (SmtpConnectionException exSmtp)
            {
                errorText = GetFormattedTextError(exSmtp, ServerType.Smtp, exSmtp is SmtpConnectionTimeoutException);
            }
            catch (ItemNotFoundException exProvider)
            {
                errorText = GetFormattedTextError(exProvider);
            }
            catch (Exception ex)
            {
                errorText = GetFormattedTextError(ex);
            }

            if (!string.IsNullOrEmpty(errorText))
                throw new Exception(errorText);

            try
            {
                if (mbox == null)
                    throw new Exception();

                mbox.InServerId = MailBoxManager.SaveMailServerSettings(mbox.EMail,
                                                                        new MailServerSettings
                                                                            {
                                                                                AccountName = mbox.Account,
                                                                                AccountPass = mbox.Password,
                                                                                AuthenticationType =
                                                                                    mbox.AuthenticationTypeIn,
                                                                                EncryptionType =
                                                                                    mbox.IncomingEncryptionType,
                                                                                Port = mbox.Port,
                                                                                Url = mbox.Server
                                                                            },
                                                                        mbox.Imap ? "imap" : "pop3",
                                                                        AuthorizationServiceType.Unknown);

                mbox.SmtpServerId = MailBoxManager.SaveMailServerSettings(mbox.EMail,
                                                                          new MailServerSettings
                                                                              {
                                                                                  AccountName = mbox.SmtpAccount,
                                                                                  AccountPass = mbox.SmtpPassword,
                                                                                  AuthenticationType =
                                                                                      mbox.AuthenticationTypeSmtp,
                                                                                  EncryptionType =
                                                                                      mbox.OutcomingEncryptionType,
                                                                                  Port = mbox.SmtpPort,
                                                                                  Url = mbox.SmtpServer
                                                                              },
                                                                          "smtp", AuthorizationServiceType.Unknown);

                MailBoxManager.SaveMailBox(mbox);
                var account = new AccountInfo(mbox.MailBoxId, mbox.EMailView, mbox.Name, mbox.Enabled, mbox.QuotaError,
                                               MailBox.AuthProblemType.NoProblems, new SignatureDto(mbox.MailBoxId, TenantId, "", false), 
                                               false, mbox.EMailInFolder, false, false);

                return account.ToAddressData().FirstOrDefault();

            }
            catch (Exception ex)
            {
                //TODO: change AttachmentsUnknownError to common unknown error text
                errorText = GetFormattedTextError(ex, MailApiResource.AttachmentsUnknownError);
            }

            throw new Exception(errorText);
        }
        public MailAccountData CreateAccount(string name,
            string email,
            string account,
            string password,
            int port,
            string server,
            string smtp_account,
            string smtp_password,
            int smtp_port,
            string smtp_server,
            bool smtp_auth,
            bool imap,
            bool restrict,
            EncryptionType incoming_encryption_type,
            EncryptionType outcoming_encryption_type,
            SaslMechanism auth_type_in,
            SaslMechanism auth_type_smtp)
        {
            string errorText = null;
            var mbox = new MailBox
                {
                    Name = name,
                    EMail = new MailAddress(email),
                    Account = account,
                    Password = password,
                    Port = port,
                    Server = server,
                    SmtpAccount = smtp_account,
                    SmtpPassword = smtp_password,
                    SmtpPort = smtp_port,
                    SmtpServer = smtp_server,
                    SmtpAuth = smtp_auth,
                    Imap = imap,
                    Restrict = restrict,
                    TenantId = TenantId,
                    UserId = Username,
                    BeginDate = restrict ? 
                        DateTime.Now.Subtract(new TimeSpan(MailBox.DefaultMailLimitedTimeDelta)) : 
                        new DateTime(MailBox.DefaultMailBeginTimestamp),
                    IncomingEncryptionType = incoming_encryption_type,
                    OutcomingEncryptionType = outcoming_encryption_type,
                    AuthenticationTypeIn = auth_type_in,
                    AuthenticationTypeSmtp = auth_type_smtp
                };

            try
            {
                MailServerHelper.Test(mbox);
            }
            catch (ImapConnectionException exImap)
            {
                errorText = GetFormattedTextError(exImap, ServerType.Imap, exImap is ImapConnectionTimeoutException);
            }
            catch (Pop3ConnectionException exPop3)
            {
                errorText = GetFormattedTextError(exPop3, ServerType.Pop3, exPop3 is Pop3ConnectionTimeoutException);
            }
            catch (SmtpConnectionException exSmtp)
            {
                errorText = GetFormattedTextError(exSmtp, ServerType.Smtp, exSmtp is SmtpConnectionTimeoutException);
            }
            catch (Exception ex)
            {
                errorText = GetFormattedTextError(ex);
            }

            if(!string.IsNullOrEmpty(errorText))
                throw new Exception(errorText);

            try
            {
                mbox.InServerId = MailBoxManager.SaveMailServerSettings(mbox.EMail,
                                                                        new MailServerSettings
                                                                            {
                                                                                AccountName = mbox.Account,
                                                                                AccountPass = mbox.Password,
                                                                                AuthenticationType =
                                                                                    mbox.AuthenticationTypeIn,
                                                                                EncryptionType =
                                                                                    mbox.IncomingEncryptionType,
                                                                                Port = mbox.Port,
                                                                                Url = mbox.Server
                                                                            },
                                                                        imap ? "imap" : "pop3",
                                                                        AuthorizationServiceType.Unknown);
                mbox.SmtpServerId = MailBoxManager.SaveMailServerSettings(mbox.EMail,
                                                                          new MailServerSettings
                                                                              {
                                                                                  AccountName = mbox.SmtpAccount,
                                                                                  AccountPass = mbox.SmtpPassword,
                                                                                  AuthenticationType =
                                                                                      mbox.AuthenticationTypeSmtp,
                                                                                  EncryptionType =
                                                                                      mbox.OutcomingEncryptionType,
                                                                                  Port = mbox.SmtpPort,
                                                                                  Url = mbox.SmtpServer
                                                                              },
                                                                          "smtp", AuthorizationServiceType.Unknown);

                MailBoxManager.SaveMailBox(mbox);
                var accountInfo = new AccountInfo(mbox.MailBoxId, mbox.EMailView, mbox.Name, mbox.Enabled, mbox.QuotaError,
                                               MailBox.AuthProblemType.NoProblems, new SignatureDto(mbox.MailBoxId, TenantId, "", false), 
                                               false, mbox.EMailInFolder, false, false);

                return accountInfo.ToAddressData().FirstOrDefault();
            }
            catch (Exception ex)
            {
                //TODO: change AttachmentsUnknownError to common unknown error text
                errorText = GetFormattedTextError(ex, MailApiResource.AttachmentsUnknownError);
            }

            throw new Exception(errorText);

        }
        public MailAccountData UpdateAccount(string name,
            string email,
            string account,
            string password,
            int port,
            string server,
            string smtp_account,
            string smtp_password,
            int smtp_port,
            string smtp_server,
            bool smtp_auth,
            bool restrict,
            EncryptionType incoming_encryption_type,
            EncryptionType outcoming_encryption_type,
            SaslMechanism auth_type_in,
            SaslMechanism auth_type_smtp)
        {
            if (string.IsNullOrEmpty(email))
                throw new ArgumentException();

            var mbox = MailBoxManager.GetMailBox(TenantId, Username, new MailAddress(email));

            if (null == mbox)
                throw new ArgumentException("Mailbox with specified email doesn't exist.");

            if (mbox.IsTeamlab)
                throw new ArgumentException("Mailbox with specified email can't be updated");

            if (string.IsNullOrEmpty(password))
                password = mbox.Password;
            if (string.IsNullOrEmpty(smtp_password))
                smtp_password = mbox.SmtpPassword;

            string errorText;

            mbox.Account = account;
            mbox.Name = name;
            mbox.Password = password;
            mbox.SmtpAccount = smtp_account;
            mbox.SmtpPassword = smtp_password;
            mbox.Port = port;
            mbox.Server = server;
            mbox.SmtpPort = smtp_port;
            mbox.SmtpServer = smtp_server;
            mbox.SmtpAuth = smtp_auth;
            mbox.Restrict = restrict;
            mbox.BeginDate = mbox.Restrict ? 
                DateTime.Now.Subtract(new TimeSpan(mbox.MailLimitedTimeDelta)) : 
                mbox.MailBeginTimestamp;
            mbox.IncomingEncryptionType = incoming_encryption_type;
            mbox.OutcomingEncryptionType = outcoming_encryption_type;
            mbox.AuthenticationTypeIn = auth_type_in;
            mbox.AuthenticationTypeSmtp = auth_type_smtp;

            mbox.InServerId = MailBoxManager.SaveMailServerSettings(mbox.EMail,
                        new MailServerSettings
                        {
                            AccountName = mbox.Account,
                            AccountPass = mbox.Password,
                            AuthenticationType = mbox.AuthenticationTypeIn,
                            EncryptionType = mbox.IncomingEncryptionType,
                            Port = mbox.Port,
                            Url = mbox.Server
                        },
                        mbox.Imap ? "imap" : "pop3", AuthorizationServiceType.Unknown);
            mbox.SmtpServerId = MailBoxManager.SaveMailServerSettings(mbox.EMail,
                                    new MailServerSettings
                                    {
                                        AccountName = mbox.SmtpAccount,
                                        AccountPass = mbox.SmtpPassword,
                                        AuthenticationType = mbox.AuthenticationTypeSmtp,
                                        EncryptionType = mbox.OutcomingEncryptionType,
                                        Port = mbox.SmtpPort,
                                        Url = mbox.SmtpServer
                                    },
                                    "smtp", AuthorizationServiceType.Unknown);

            try
            {
                if (!string.IsNullOrEmpty(mbox.RefreshToken) || MailServerHelper.Test(mbox))
                {
                    if (!MailBoxManager.SaveMailBox(mbox))
                        throw new Exception("Failed to_addresses update account");
                }

                var accountInfo = new AccountInfo(mbox.MailBoxId, mbox.EMailView, mbox.Name, mbox.Enabled, mbox.QuotaError,
                                               MailBox.AuthProblemType.NoProblems, new SignatureDto(mbox.MailBoxId, TenantId, "", false),
                                               false, mbox.EMailInFolder, false, false);

                return accountInfo.ToAddressData().FirstOrDefault();
            }
            catch (ImapConnectionException exImap)
            {
                errorText = GetFormattedTextError(exImap, ServerType.Imap, exImap is ImapConnectionTimeoutException);
            }
            catch (Pop3ConnectionException exPop3)
            {
                errorText = GetFormattedTextError(exPop3, ServerType.Pop3, exPop3 is Pop3ConnectionTimeoutException);
            }
            catch (SmtpConnectionException exSmtp)
            {
                errorText = GetFormattedTextError(exSmtp, ServerType.Smtp, exSmtp is SmtpConnectionTimeoutException);
            }
            catch (Exception ex)
            {
                errorText = GetFormattedTextError(ex);
            }

            throw new Exception(errorText);
        }
        public MailAccountData CreateAccountOAuth(string email, string token, byte type)
        {
            if (string.IsNullOrEmpty(token)) throw new ArgumentException(@"Empty oauth token", "token");
            if (string.IsNullOrEmpty(email)) throw new ArgumentException(@"Empty email", "email");

            var beginDate = DateTime.Now.Subtract(new TimeSpan(MailBox.DefaultMailLimitedTimeDelta));

            var mboxImap = MailBoxManager.ObtainMailboxSettings(TenantId, Username, email, "", (AuthorizationServiceType) type, true, false);
            mboxImap.RefreshToken = token;
            mboxImap.BeginDate = beginDate; // Apply restrict for download

            try
            {
                mboxImap.InServerId = MailBoxManager.SaveMailServerSettings(mboxImap.EMail,
                                        new MailServerSettings
                                        {
                                            AccountName = mboxImap.Account,
                                            AccountPass = mboxImap.Password,
                                            AuthenticationType = mboxImap.AuthenticationTypeIn,
                                            EncryptionType = mboxImap.IncomingEncryptionType,
                                            Port = mboxImap.Port,
                                            Url = mboxImap.Server
                                        },
                                        "imap",
                                        (AuthorizationServiceType)type);
                mboxImap.SmtpServerId = MailBoxManager.SaveMailServerSettings(mboxImap.EMail,
                                        new MailServerSettings
                                        {
                                            AccountName = mboxImap.SmtpAccount,
                                            AccountPass = mboxImap.SmtpPassword,
                                            AuthenticationType = mboxImap.AuthenticationTypeSmtp,
                                            EncryptionType = mboxImap.OutcomingEncryptionType,
                                            Port = mboxImap.SmtpPort,
                                            Url = mboxImap.SmtpServer
                                        },
                                        "smtp",
                                        (AuthorizationServiceType)type);

                MailBoxManager.SaveMailBox(mboxImap);
                var account = new AccountInfo(mboxImap.MailBoxId, mboxImap.EMailView, mboxImap.Name, mboxImap.Enabled, mboxImap.QuotaError,
                                               MailBox.AuthProblemType.NoProblems, new SignatureDto(mboxImap.MailBoxId, TenantId, "", false),
                                               true, mboxImap.EMailInFolder, false, false);

                return account.ToAddressData().FirstOrDefault();
            }
            catch (Exception imapException)
            {
                throw new Exception(GetFormattedTextError(imapException, ServerType.ImapOAuth, imapException is ImapConnectionTimeoutException));
            }
        }