Esempio n. 1
0
        public void RemoveNotificationAddress(string address)
        {
            if (!CoreContext.Configuration.Standalone)
            {
                throw new SecurityException("Only for standalone");
            }

            if (!IsAdmin)
            {
                throw new SecurityException("Need admin privileges.");
            }

            if (string.IsNullOrEmpty(address))
            {
                throw new ArgumentNullException("address");
            }

            var deleteAddress = address.ToLowerInvariant().Trim();
            var notificationAddressSettings = ServerNotificationAddressSettings.LoadForTenant(Tenant);

            if (notificationAddressSettings.NotificationAddress != deleteAddress)
            {
                throw new ArgumentException("Mailbox not exists");
            }

            var mailAddress = new MailAddress(deleteAddress);

            using (var daoFactory = new DaoFactory())
            {
                var serverDomainDao = daoFactory.CreateServerDomainDao(Tenant);
                var serverDomain    = serverDomainDao.GetDomains().FirstOrDefault(d => d.Name == mailAddress.Host);

                if (serverDomain == null)
                {
                    throw new ArgumentException("Domain not exists");
                }

                var serverDao = daoFactory.CreateServerDao();
                var server    = serverDao.Get(Tenant);

                if (server == null)
                {
                    throw new ArgumentException("Server not configured");
                }

                var engine = new Server.Core.ServerEngine(server.Id, server.ConnectionString);

                engine.RemoveMailbox(deleteAddress);
            }

            var addressSettings = notificationAddressSettings.GetDefault() as ServerNotificationAddressSettings;

            if (addressSettings != null && !addressSettings.SaveForTenant(Tenant))
            {
                throw new Exception("Could not delete notification address setting.");
            }
        }
Esempio n. 2
0
        public ServerNotificationAddressData CreateNotificationAddress(string localPart, string password, int domainId)
        {
            if (!CoreContext.Configuration.Standalone)
            {
                throw new SecurityException("Only for standalone");
            }

            if (!IsAdmin)
            {
                throw new SecurityException("Need admin privileges.");
            }

            if (string.IsNullOrEmpty(localPart))
            {
                throw new ArgumentNullException("localPart", @"Invalid address username.");
            }

            localPart = localPart.ToLowerInvariant().Trim();

            if (localPart.Length > 64)
            {
                throw new ArgumentException(@"Local part of address exceed limitation of 64 characters.", "localPart");
            }

            if (!Parser.IsEmailLocalPartValid(localPart))
            {
                throw new ArgumentException(@"Incorrect address username.", "localPart");
            }

            var trimPwd = Parser.GetValidPassword(password);

            if (domainId < 0)
            {
                throw new ArgumentException(@"Invalid domain id.", "domainId");
            }

            var notificationAddressSettings = ServerNotificationAddressSettings.LoadForTenant(Tenant);

            if (!string.IsNullOrEmpty(notificationAddressSettings.NotificationAddress))
            {
                RemoveNotificationAddress(notificationAddressSettings.NotificationAddress);
            }

            var utcNow = DateTime.UtcNow;

            using (var daoFactory = new DaoFactory())
            {
                var serverDomainDao = daoFactory.CreateServerDomainDao(Tenant);
                var serverDomain    = serverDomainDao.GetDomain(domainId);

                if (localPart.Length + serverDomain.Name.Length > 318) // 318 because of @ sign
                {
                    throw new ArgumentException(@"Address of mailbox exceed limitation of 319 characters.", "localPart");
                }

                var login = string.Format("{0}@{1}", localPart, serverDomain.Name);

                var serverDao = daoFactory.CreateServerDao();
                var server    = serverDao.Get(Tenant);

                if (server == null)
                {
                    throw new ArgumentException("Server not configured");
                }

                var engine = new Server.Core.ServerEngine(server.Id, server.ConnectionString);

                var maildir = PostfixMaildirUtil.GenerateMaildirPath(serverDomain.Name, localPart, utcNow);

                var serverMailbox = new Server.Core.Entities.Mailbox
                {
                    Name      = localPart,
                    Password  = trimPwd,
                    Login     = login,
                    LocalPart = localPart,
                    Domain    = serverDomain.Name,
                    Active    = true,
                    Quota     = 0,
                    Maldir    = maildir,
                    Modified  = utcNow,
                    Created   = utcNow,
                };

                var serverAddress = new Alias
                {
                    Name     = localPart,
                    Address  = login,
                    GoTo     = login,
                    Domain   = serverDomain.Name,
                    IsActive = true,
                    IsGroup  = false,
                    Modified = utcNow,
                    Created  = utcNow
                };

                engine.SaveMailbox(serverMailbox, serverAddress, false);

                notificationAddressSettings = new ServerNotificationAddressSettings {
                    NotificationAddress = login
                };

                notificationAddressSettings.SaveForTenant(Tenant);

                var mailboxServerDao = daoFactory.CreateMailboxServerDao();

                var smtpSettings = mailboxServerDao.GetServer(server.SmtpSettingsId);

                var address = new MailAddress(login);

                var notifyAddress = new ServerNotificationAddressData
                {
                    Email                  = address.ToString(),
                    SmtpPort               = smtpSettings.Port,
                    SmtpServer             = smtpSettings.Hostname,
                    SmtpAccount            = address.ToLogin(smtpSettings.Username),
                    SmptEncryptionType     = smtpSettings.SocketType,
                    SmtpAuth               = true,
                    SmtpAuthenticationType = smtpSettings.Authentication
                };

                return(notifyAddress);
            }
        }