private List<AccountInfo> ToAccountInfo(IEnumerable<object[]> objectList, List<SignatureDto> signatures)
        {
            var accounts = new List<AccountInfo>();

            foreach (var r in objectList)
            {
                if (r.Length != SELECT_ACCOUNT_FIELDS_COUNT)
                {
                    _log.Error("ToAccountInfo Count of returned fields Length = {0} not equal to SELECT_ACCOUNT_FIELDS_COUNT = {1}",
                        r.Length, SELECT_ACCOUNT_FIELDS_COUNT);
                    return null;
                }

                var mailboxId = Convert.ToInt32(r[(int)MailAccountFieldSelectPosition.Id]);
                var accountIndex = accounts.FindIndex(a => a.Id == mailboxId);

                var signature = signatures.First(s => s.MailboxId == mailboxId);
                var isAlias = Convert.ToBoolean(r[(int)MailAccountFieldSelectPosition.IsAlias]);

                if (!isAlias)
                {
                    var groupAddress = (string)(r[(int)MailAccountFieldSelectPosition.GroupAddress]);
                    MailAddressInfo group = null;

                    if (!string.IsNullOrEmpty(groupAddress))
                    {
                        group = new MailAddressInfo(
                            Convert.ToInt32(r[(int)MailAccountFieldSelectPosition.GroupId]),
                            groupAddress,
                            Convert.ToInt32(r[(int)MailAccountFieldSelectPosition.DomainId]));
                    }

                    if (accountIndex == -1)
                    {
                        var authErrorType = MailBox.AuthProblemType.NoProblems;

                        if (r[(int)MailAccountFieldSelectPosition.AuthError] != null)
                        {
                            var authErrorDate = Convert.ToDateTime(r[(int)MailAccountFieldSelectPosition.AuthError]);

                            if (DateTime.UtcNow - authErrorDate > AuthErrorDisableTimeout)
                                authErrorType = MailBox.AuthProblemType.TooManyErrors;
                            else if (DateTime.UtcNow - authErrorDate > AuthErrorWarningTimeout)
                                authErrorType = MailBox.AuthProblemType.ConnectError;
                        }

                        var account = new AccountInfo(
                            mailboxId,
                            (string)(r[(int)MailAccountFieldSelectPosition.Address]),
                            (string)(r[(int)MailAccountFieldSelectPosition.Name]),
                            Convert.ToBoolean(r[(int)MailAccountFieldSelectPosition.Enabled]),
                            Convert.ToBoolean(r[(int)MailAccountFieldSelectPosition.QoutaError]),
                            authErrorType, signature,
                            !string.IsNullOrEmpty((string)(r[(int)MailAccountFieldSelectPosition.RefreshToken])),
                            (string)(r[(int)MailAccountFieldSelectPosition.EmailInFolder]),
                            Convert.ToBoolean(r[(int)MailAccountFieldSelectPosition.IsTeamlabMailbox]),
                            Convert.ToInt32(r[(int) MailAccountFieldSelectPosition.DomainTenant]) == Defines.SHARED_TENANT_ID);

                        if (group != null) account.Groups.Add(group);

                        accounts.Add(account);
                    }
                    else if (group != null)
                    {
                        accounts[accountIndex].Groups.Add(group);
                    }
                }
                else
                {
                    var alias = new MailAddressInfo(
                            Convert.ToInt32(r[(int)MailAccountFieldSelectPosition.AliasId]),
                            (string)(r[(int)MailAccountFieldSelectPosition.AliasName]) + '@' +
                            (string)(r[(int)MailAccountFieldSelectPosition.DomainName]),
                            Convert.ToInt32(r[(int)MailAccountFieldSelectPosition.DomainId]));

                    accounts[accountIndex].Aliases.Add(alias);
                }
            }
            return accounts;
        }
Exemplo n.º 2
0
        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);
        }
Exemplo n.º 3
0
        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);

        }
Exemplo n.º 4
0
        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);
        }
Exemplo n.º 5
0
        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));
            }
        }