예제 #1
0
        public static int AddMailAccount(MailAccount item)
        {
            // check account
            int accountCheck = SecurityContext.CheckAccount(DemandAccount.NotDemo | DemandAccount.IsActive);
            if (accountCheck < 0) return accountCheck;

            // check package
            int packageCheck = SecurityContext.CheckPackage(item.PackageId, DemandPackage.IsActive);
            if (packageCheck < 0) return packageCheck;

            // check quota
            QuotaValueInfo quota = PackageController.GetPackageQuota(item.PackageId, Quotas.MAIL_ACCOUNTS);
            if (quota.QuotaExhausted)
                return BusinessErrorCodes.ERROR_MAIL_ACCOUNTS_RESOURCE_QUOTA_LIMIT;

            // check if mail resource is available
            int serviceId = PackageController.GetPackageServiceId(item.PackageId, ResourceGroups.Mail);
            if (serviceId == 0)
                return BusinessErrorCodes.ERROR_MAIL_RESOURCE_UNAVAILABLE;

            // check package items
            if (PackageController.GetPackageItemByName(item.PackageId, item.Name, typeof(MailAccount)) != null)
                return BusinessErrorCodes.ERROR_MAIL_ACCOUNTS_PACKAGE_ITEM_EXISTS;

            // place log record
            TaskManager.StartTask("MAIL_ACCOUNT", "ADD", item.Name);
            int itemId = 0;
            try
            {

                // check service items
                MailServer mail = new MailServer();
                ServiceProviderProxy.Init(mail, serviceId);
                if (mail.AccountExists(item.Name))
                    return BusinessErrorCodes.ERROR_MAIL_ACCOUNTS_SERVICE_ITEM_EXISTS;

                // add domain if not exists
                string domainName = item.Name.Substring(item.Name.IndexOf("@") + 1);
                int domainResult = AddMailDomain(item.PackageId, serviceId, domainName);
                if (domainResult < 0)
                    return domainResult;

                // create service item
                item.Enabled = true;
                item.MaxMailboxSize = GetMaxMailBoxSize(item.PackageId, item);

                // add service item
                mail.CreateAccount(item);

                // save item
                item.Password = CryptoUtils.Encrypt(item.Password);
                item.ServiceId = serviceId;
                itemId = PackageController.AddPackageItem(item);

                TaskManager.ItemId = itemId;
                                
            }
            catch (Exception ex)
            {
                TaskManager.WriteError(ex);
                if (ex.InnerException != null &&
                    ex.InnerException.Message.Contains("The maximum number of domains allowed has been reached"))
                {
                    return BusinessErrorCodes.ERROR_MAIL_LICENSE_DOMAIN_QUOTA;
                }
                if (ex.Message != null && ex.Message.Contains("The maximum number of users for the server has been reached"))
                {
                    return BusinessErrorCodes.ERROR_MAIL_LICENSE_USERS_QUOTA;
                }
            }
            finally
            {
                TaskManager.CompleteTask();
            }
            return itemId;
        }