Example #1
0
    public bool AddNewWebMailDomain()
    {
        bool result = false;
        // create webmail domain
        string domainName   = textDomainName.Value;
        string incomingMail = txtIncomingMail.Value;
        string outgoingMail = txtOutgoingMail.Value;
        bool   useSmtpAuth  = intReqSmtpAuthentication.Checked;
        int    incomingPort = 0;

        int.TryParse(intIncomingMailPort.Value, out incomingPort);
        int outgoingPort = 0;

        int.TryParse(intOutgoingMailPort.Value, out outgoingPort);
        IncomingMailProtocol mail_protocol = IncomingMailProtocol.Pop3;

        switch (intIncomingMailProtocol.SelectedIndex)
        {
        case 0:
            mail_protocol = IncomingMailProtocol.Pop3;
            break;

        case 1:
            mail_protocol = IncomingMailProtocol.Imap4;
            break;

        case 2:
            mail_protocol = IncomingMailProtocol.WMServer;
            break;
        }

        try
        {
            Domain dom = Domain.GetDomain(domainName);
            if (dom == null)
            {
                Domain new_dom = new Domain(0, domainName, mail_protocol, incomingMail, incomingPort, outgoingMail, outgoingPort, useSmtpAuth);


                new_dom.Global_addr_book = false;

                new_dom.Save();
                result = true;
            }
            else
            {
                result = false;
                AdminPanelUtils.SaveState("SessPageErrorMessage", "Such domain already exists.", Page.Session);
            }
        }
        catch (Exception error)
        {
            result = false;
            Log.WriteException(error);
            AdminPanelUtils.SetPageErrorMessage(this.Page, (error.InnerException != null ? error.InnerException.Message : error.Message));
        }
        return(result);
    }
Example #2
0
    public void UpdateWebMailDomain()
    {
        try
        {
            string domainName   = textDomainName.Value;
            string incomingMail = txtIncomingMail.Value;
            string outgoingMail = txtOutgoingMail.Value;
            int    domainID     = Int32.Parse(hidWebMailDomainID.Value);

            if (!AdminPanelUtils.IsSuperAdmin(Page.Session, (Page as DefaultPage).Settings))
            {
                if (!Subadmin.IsSubadminDomain(int.Parse(Session["AdminID"].ToString()), domainID))
                {
                    return;
                }
            }

            bool useSmtpAuth  = intReqSmtpAuthentication.Checked;
            int  incomingPort = 0;
            int.TryParse(intIncomingMailPort.Value, out incomingPort);
            int outgoingPort = 0;
            int.TryParse(intOutgoingMailPort.Value, out outgoingPort);
            IncomingMailProtocol mail_protocol = IncomingMailProtocol.Pop3;
            switch (intIncomingMailProtocol.SelectedIndex)
            {
            case 0:
                mail_protocol = IncomingMailProtocol.Pop3;
                break;

            case 1:
                mail_protocol = IncomingMailProtocol.Imap4;
                break;

            case 2:
                mail_protocol = IncomingMailProtocol.WMServer;
                break;
            }
            Domain dom = Domain.GetDomain(domainID);

            dom.Name = domainName;
            dom.MailIncomingProtocol       = mail_protocol;
            dom.MailIncomingHost           = incomingMail;
            dom.MailIncomingPort           = incomingPort;
            dom.MailOutgoingHost           = outgoingMail;
            dom.MailOutgoingPort           = outgoingPort;
            dom.MailOutgoingAuthentication = useSmtpAuth;


            dom.Global_addr_book = false;

            dom.Save();
        }
        catch (Exception error)
        {
            Log.WriteException(error);
            AdminPanelUtils.SetPageErrorMessage(this.Page, (error.InnerException != null ? error.InnerException.Message : error.Message));
        }
    }
Example #3
0
        public Domain(int id_account, string name, IncomingMailProtocol mail_protocol,
                      string mail_inc_host, int mail_inc_port, string mail_out_host,
                      int mail_out_port, bool mail_out_auth)
        {
            _id                    = id_account;
            _name                  = name;
            _mailProtocol          = mail_protocol;
            _mailIncHost           = mail_inc_host;
            _mailIncPort           = mail_inc_port;
            _mailOutHost           = mail_out_host;
            _mailOutPort           = mail_out_port;
            _mailOutAuthentication = mail_out_auth;

            WebmailSettings settings = (new WebMailSettingsCreator()).CreateWebMailSettings();

            _url       = string.Empty;
            _site_name = settings.SiteName;
            _settings_mail_protocol          = settings.IncomingMailProtocol;
            _settings_mail_inc_host          = settings.IncomingMailServer;
            _settings_mail_inc_port          = settings.IncomingMailPort;
            _settings_mail_out_host          = settings.OutgoingMailServer;
            _settings_mail_out_port          = settings.OutgoingMailPort;
            _settings_mail_out_auth          = settings.ReqSmtpAuth;
            _allow_direct_mode               = settings.AllowDirectMode;
            _direct_mode_id_def              = settings.DirectModeIsDefault;
            _attachment_size_limit           = settings.AttachmentSizeLimit;
            _allow_attachment_limit          = settings.EnableAttachmentSizeLimit;
            _mailbox_size_limit              = settings.MailboxSizeLimit;
            _allow_mailbox_limit             = settings.EnableMailboxSizeLimit;
            _take_quota                      = settings.TakeImapQuota;
            _allow_new_users_change_settings = settings.AllowUsersChangeEmailSettings;
            _allow_auto_reg_on_login         = settings.AllowNewUsersRegister;
            _allow_users_add_accounts        = settings.AllowUsersAddNewAccounts;
            _allow_users_change_account_def  = settings.AllowUsersChangeAccountsDef;
            _def_user_charset                = settings.DefaultUserCharset;
            _allow_users_change_charset      = settings.AllowUsersChangeCharset;
            _def_user_timezone               = settings.DefaultTimeZone;
            _allow_users_change_timezone     = settings.AllowUsersChangeTimeZone;
            _msgs_per_page                   = settings.MailsPerPage;
            _skin = settings.DefaultSkin;
            _allow_users_change_skin = settings.AllowUsersChangeSkin;
            _lang = settings.DefaultLanguage;
            _allow_users_change_lang = settings.AllowUsersChangeLanguage;
            _show_text_labels        = settings.ShowTextLabels;
            _allow_ajax                    = settings.AllowAjax;
            _allow_editor                  = settings.AllowDhtmlEditor;
            _allow_contacts                = settings.AllowContacts;
            _allow_calendar                = settings.AllowCalendar;
            _hide_login_mode               = settings.HideLoginMode;
            _domain_to_use                 = settings.DefaultDomainOptional;
            _allow_choosing_lang           = settings.AllowLanguageOnLogin;
            _allow_advanced_login          = settings.AllowAdvancedLogin;
            _allow_auto_detect_and_correct = settings.AutomaticCorrectLoginSettings;
            _viewmode  = settings.ViewMode;
            _save_mail = SaveMail.Always;
        }
Example #4
0
        public Account(string dataFolder)
        {
            WebmailSettings settings;

            if (dataFolder != string.Empty)
            {
                settings = (new WebMailSettingsCreator()).CreateWebMailSettings(dataFolder);
            }
            else
            {
                settings = (new WebMailSettingsCreator()).CreateWebMailSettings();
            }
            _id                    = -1;
            _idUser                = -1;
            _idDomain              = 0;
            _defaultAccount        = true;
            _deleted               = false;
            _email                 = "";
            _mailProtocol          = settings.IncomingMailProtocol;
            _mailIncHost           = settings.IncomingMailServer;
            _mailIncLogin          = "";
            _mailIncPassword       = "";
            _mailIncPort           = settings.IncomingMailPort;
            _mailOutHost           = settings.OutgoingMailServer;
            _mailOutLogin          = "";
            _mailOutPassword       = "";
            _mailOutPort           = settings.OutgoingMailPort;
            _mailOutAuthentication = settings.ReqSmtpAuth;
            _friendlyName          = "";
            _useFriendlyName       = true;
            _defaultOrder          = DefaultOrder.DateDesc;
            _getMailAtLogin        = true;
            _mailMode              = MailMode.LeaveMessagesOnServer;
            _mailsOnServerDays     = 1;
            _signature             = "";
            _signatureType         = SignatureType.Plain;
            _signatureOptions      = SignatureOptions.DontAddSignature;
            _user                  = null;
            _delimiter             = "/";
            _mailbox_size          = 0;
            _mailing_list          = false;
            _imap_quota            = -1;
            _namespace             = "";
        }
Example #5
0
 public Account(int id_account, bool def_acct, bool deleted, string email, IncomingMailProtocol mail_protocol,
                string mail_inc_host, string mail_inc_login, string mail_inc_pass, int mail_inc_port, string mail_out_host,
                string mail_out_login, string mail_out_pass, int mail_out_port, bool mail_out_auth, string friendly_nm,
                bool use_friendly_nm, DefaultOrder def_order, bool getmail_at_login, MailMode mail_mode,
                short mails_on_server_days, string signature, SignatureType signature_type, SignatureOptions signature_opt,
                string delimiter, long mailbox_size, int idDomain, bool mailing_list, int imap_quota, string Namespace)
 {
     _id                    = id_account;
     _defaultAccount        = def_acct;
     _deleted               = deleted;
     _email                 = email;
     _mailProtocol          = mail_protocol;
     _mailIncHost           = mail_inc_host;
     _mailIncLogin          = mail_inc_login;
     _mailIncPassword       = mail_inc_pass;
     _mailIncPort           = mail_inc_port;
     _mailOutHost           = mail_out_host;
     _mailOutLogin          = mail_out_login;
     _mailOutPassword       = mail_out_pass;
     _mailOutPort           = mail_out_port;
     _mailOutAuthentication = mail_out_auth;
     _friendlyName          = friendly_nm;
     _useFriendlyName       = use_friendly_nm;
     _defaultOrder          = def_order;
     _getMailAtLogin        = getmail_at_login;
     _mailMode              = mail_mode;
     _mailsOnServerDays     = mails_on_server_days;
     _signature             = signature;
     _signatureType         = signature_type;
     _signatureOptions      = signature_opt;
     _delimiter             = delimiter;
     _mailbox_size          = mailbox_size;
     _idDomain              = idDomain;
     _mailing_list          = mailing_list;
     _imap_quota            = imap_quota;
     _namespace             = Namespace;
 }
Example #6
0
        public Domain(int id_account, string name, IncomingMailProtocol mail_protocol,
                      string mail_inc_host, int mail_inc_port, string mail_out_host,
                      int mail_out_port, bool mail_out_auth, string url, string site_name,
                      IncomingMailProtocol settings_mail_protocol, string settings_mail_inc_host,
                      int settings_mail_inc_port, string settings_mail_out_host,
                      int settings_mail_out_port, bool settings_mail_out_auth,
                      bool allow_direct_mode, bool direct_mode_id_def, long attachment_size_limit,
                      bool allow_attachment_limit, long mailbox_size_limit, bool allow_mailbox_limit,
                      bool take_quota, bool allow_new_users_change_settings, bool allow_auto_reg_on_login,
                      bool allow_users_add_accounts, bool allow_users_change_account_def,
                      int def_user_charset, bool allow_users_change_charset, int def_user_timezone,
                      bool allow_users_change_timezone, int msgs_per_page, string skin,
                      bool allow_users_change_skin, string lang, bool allow_users_change_lang,
                      bool show_text_labels, bool allow_ajax, bool allow_editor, bool allow_contacts,
                      bool allow_calendar, LoginMode hide_login_mode,
                      string domain_to_use, bool allow_choosing_lang, bool allow_advanced_login,
                      bool allow_auto_detect_and_correct, bool contacts_sharing, ViewMode viewmode, SaveMail save_mail)
        {
            _id                    = id_account;
            _name                  = name;
            _mailProtocol          = mail_protocol;
            _mailIncHost           = mail_inc_host;
            _mailIncPort           = mail_inc_port;
            _mailOutHost           = mail_out_host;
            _mailOutPort           = mail_out_port;
            _mailOutAuthentication = mail_out_auth;

            _url       = url;
            _site_name = site_name;
            _settings_mail_protocol          = settings_mail_protocol;
            _settings_mail_inc_host          = settings_mail_inc_host;
            _settings_mail_inc_port          = settings_mail_inc_port;
            _settings_mail_out_host          = settings_mail_out_host;
            _settings_mail_out_port          = settings_mail_out_port;
            _settings_mail_out_auth          = settings_mail_out_auth;
            _allow_direct_mode               = allow_direct_mode;
            _direct_mode_id_def              = direct_mode_id_def;
            _attachment_size_limit           = attachment_size_limit;
            _allow_attachment_limit          = allow_attachment_limit;
            _mailbox_size_limit              = mailbox_size_limit;
            _allow_mailbox_limit             = allow_mailbox_limit;
            _take_quota                      = take_quota;
            _allow_new_users_change_settings = allow_new_users_change_settings;
            _allow_auto_reg_on_login         = allow_auto_reg_on_login;
            _allow_users_add_accounts        = allow_users_add_accounts;
            _allow_users_change_account_def  = allow_users_change_account_def;
            _def_user_charset                = def_user_charset;
            _allow_users_change_charset      = allow_users_change_charset;
            _def_user_timezone               = def_user_timezone;
            _allow_users_change_timezone     = allow_users_change_timezone;
            _msgs_per_page                   = msgs_per_page;
            _skin = skin;
            _allow_users_change_skin = allow_users_change_skin;
            _lang = lang;
            _allow_users_change_lang = allow_users_change_lang;
            _show_text_labels        = show_text_labels;
            _allow_ajax                    = allow_ajax;
            _allow_editor                  = allow_editor;
            _allow_contacts                = allow_contacts;
            _allow_calendar                = allow_calendar;
            _hide_login_mode               = hide_login_mode;
            _domain_to_use                 = domain_to_use;
            _allow_choosing_lang           = allow_choosing_lang;
            _allow_advanced_login          = allow_advanced_login;
            _allow_auto_detect_and_correct = allow_auto_detect_and_correct;
            _global_addr_book              = contacts_sharing;
            _viewmode  = viewmode;
            _save_mail = save_mail;
        }
Example #7
0
        public static Account LoginAccount(string email, string login, string password, string incomingMailServer,
                                           IncomingMailProtocol incomingMailProtocol, int incomingPort, string outgoingMailServer,
                                           int outgoingMailPort, bool useSmtpAuthentication, bool signAutomatically, bool advanced_login,
                                           string language)
        {
            WebmailSettings settings  = (new WebMailSettingsCreator()).CreateWebMailSettings();
            string          tempLogin = login;

            if (!advanced_login)
            {
                switch (settings.HideLoginMode)
                {
                case LoginMode.HideLoginFieldLoginIsAccount:
                    login     = EmailAddress.Parse(email).GetAccountName();
                    tempLogin = null;
                    break;

                case LoginMode.HideLoginFieldLoginIsEmail:
                    login     = email;
                    tempLogin = null;
                    break;

                case LoginMode.HideEmailField:
                case LoginMode.HideEmailFieldDisplayDomainAfterLogin:
                    email = string.Format("{0}@{1}", login, settings.DefaultDomainOptional);
                    break;

                case LoginMode.HideEmailFieldLoginIsLoginAndDomain:
                case LoginMode.HideEmailFieldDisplayDomainAfterLoginAndLoginIsLoginAndDomain:
                    email     = string.Format("{0}@{1}", login, settings.DefaultDomainOptional);
                    login     = email;
                    tempLogin = email;
                    break;
                }
            }

            WebmailResourceManager resMan = (new WebmailResourceManagerCreator()).CreateResourceManager();

            string domainName = EmailAddress.GetDomainFromEmail(email);
            Domain domain     = LoadDomainFromDb(domainName);

/*
 *                      if (domain != null && (domain.MailIncomingProtocol == IncomingMailProtocol.WMServer ||
 *                              incomingMailProtocol == IncomingMailProtocol.WMServer) && settings.EnableWmServer)
 *                      {
 *                              tempLogin = EmailAddress.GetAccountNameFromEmail(tempLogin);
 *                              if (tempLogin != null && tempLogin != EmailAddress.GetAccountNameFromEmail(email))
 *                              {
 *                                      throw new WebMailException(resMan.GetString("ErrorPOP3IMAP4Auth"));
 *                              }
 *                      }
 */
            Account result = LoadFromDb(email, tempLogin, null);

            if (result == null)
            {
                if (!settings.AllowNewUsersRegister)
                {
                    throw new WebMailException(resMan.GetString("ErrorPOP3IMAP4Auth"));
                }
                if (domain != null)
                {
                    if (settings.AllowNewUsersRegister && domain.MailIncomingProtocol == IncomingMailProtocol.WMServer)
                    {
                        throw new WebMailException(resMan.GetString("ErrorPOP3IMAP4Auth"));
                    }
                }

                Account acct = new Account();
                if (domain != null && (domain.MailIncomingProtocol != IncomingMailProtocol.WMServer || settings.EnableWmServer))
                {
                    acct.Email = email;
                    if (domain.MailIncomingProtocol == IncomingMailProtocol.WMServer)
                    {
                        acct.MailIncomingLogin = EmailAddress.GetAccountNameFromEmail(email);
                    }
                    else
                    {
                        acct.MailIncomingLogin = login;
                    }
                    acct.MailIncomingPassword       = password;
                    acct.MailIncomingHost           = domain.MailIncomingHost;
                    acct.MailIncomingPort           = domain.MailIncomingPort;
                    acct.MailIncomingProtocol       = domain.MailIncomingProtocol;
                    acct.MailOutgoingHost           = domain.MailOutgoingHost;
                    acct.MailOutgoingPort           = domain.MailOutgoingPort;
                    acct.MailOutgoingAuthentication = domain.MailOutgoingAuthentication;
                    acct.IDDomain = domain.ID;
                }
                else
                {
                    acct.Email                      = email;
                    acct.MailIncomingLogin          = login;
                    acct.MailIncomingPassword       = password;
                    acct.MailIncomingHost           = incomingMailServer;
                    acct.MailIncomingPort           = incomingPort;
                    acct.MailIncomingProtocol       = incomingMailProtocol;
                    acct.MailOutgoingHost           = outgoingMailServer;
                    acct.MailOutgoingPort           = outgoingMailPort;
                    acct.MailOutgoingAuthentication = useSmtpAuthentication;
                    acct.IDDomain                   = 0;
                }

                bool isWmServer = (acct.MailIncomingProtocol == IncomingMailProtocol.WMServer);
                bool isIMAP4    = (acct.MailIncomingProtocol == IncomingMailProtocol.Imap4);
                if (!isWmServer)
                {
                    MailServerStorage mss = MailServerStorageCreator.CreateMailServerStorage(acct);
                    try
                    {
                        mss.Connect();
                    }
                    finally
                    {
                        if (mss.IsConnected())
                        {
                            mss.Disconnect();
                        }
                    }
                }
                User usr = null;
                try
                {
                    usr = User.CreateUser();
                    FolderSyncType syncType = Folder.DefaultInboxSyncType;
                    if (settings.DirectModeIsDefault)
                    {
                        syncType = FolderSyncType.DirectMode;
                    }
                    if (isWmServer)
                    {
                        syncType = FolderSyncType.AllHeadersOnly;
                    }
                    if (isIMAP4)
                    {
                        syncType = FolderSyncType.DirectMode;
                    }

                    result = usr.CreateAccount(acct, syncType, advanced_login);
                }
                catch (WebMailException)
                {
                    if (null != usr)
                    {
                        User.DeleteUserSettings(usr.ID);
                    }
                    throw;
                }
            }
            else
            {
                if (result.UserOfAccount.Deleted)
                {
                    throw new WebMailException((new WebmailResourceManagerCreator()).CreateResourceManager().GetString("ErrorMaximumUsersLicenseIsExceeded"));
                }

                if (result.Deleted)
                {
                    throw new WebMailException("Your account is inactive, please contact the system administrator on this.");
                }

                if (string.Compare(result.MailIncomingPassword, password, false, CultureInfo.InvariantCulture) != 0)
                {
                    result.MailIncomingPassword = password;
                    MailServerStorage mss = MailServerStorageCreator.CreateMailServerStorage(result);
                    try
                    {
                        mss.Connect();
                    }
                    finally
                    {
                        if (mss.IsConnected())
                        {
                            mss.Disconnect();
                        }
                    }
                }

                if (result.DefaultAccount == false)
                {
                    DbManagerCreator creator = new DbManagerCreator();
                    DbManager        dbMan   = creator.CreateDbManager();
                    int nonDefaultCount;
                    try
                    {
                        dbMan.Connect();
                        nonDefaultCount = dbMan.GetNotDefaultAccountCount(email, login, password);
                    }
                    finally
                    {
                        dbMan.Disconnect();
                    }
                    if (nonDefaultCount > 1)
                    {
                        throw new WebMailException((new WebmailResourceManagerCreator()).CreateResourceManager().GetString("PROC_CANT_LOG_NONDEF"));
                    }
                }
            }
            if ((result != null) && (result.UserOfAccount != null) && (result.UserOfAccount.Settings != null))
            {
                result.UserOfAccount.Settings.LastLogin = DateTime.Now;
                result.UserOfAccount.Settings.LoginsCount++;
                if (!string.IsNullOrEmpty(language) && settings.AllowLanguageOnLogin)
                {
                    result.UserOfAccount.Settings.DefaultLanguage = language;
                }

                if (result.MailIncomingProtocol == IncomingMailProtocol.Imap4)
                {
                    ImapStorage imapStorage = new ImapStorage(result);
                    try
                    {
                        imapStorage.Connect();
                        result.Namespace = imapStorage.GetNamespace();

                        if (settings.TakeImapQuota)
                        {
                            if (imapStorage.IsQuotaSupported())
                            {
                                if (result.Imap_quota != 0)
                                {
                                    result.Imap_quota = 1;
                                    result.UserOfAccount.Settings.MailboxLimit = imapStorage.GetMailStorageSize();
                                }
                            }
                            else
                            {
                                result.Imap_quota = -1;
                            }
                        }
                    }
                    finally
                    {
                        imapStorage.Disconnect();
                    }
                }

                result.Update(true);
            }

            // Create Trash Folder if Trash not found in the list
            if (result != null && result.MailIncomingProtocol == IncomingMailProtocol.WMServer)
            {
                MailServerStorage wmServerStorage = MailServerStorageCreator.CreateMailServerStorage(result);

                try
                {
                    wmServerStorage.Connect();
                    FolderCollection fc = wmServerStorage.GetFolders();

                    FileSystem fs  = new FileSystem(result.Email, result.ID, true);
                    Folder     fld = fc[FolderType.Trash];
                    if (fld == null)
                    {
                        string       fullPath = result.Delimiter + Constants.FolderNames.Trash;
                        const string name     = Constants.FolderNames.Trash;
                        if (!settings.StoreMailsInDb)
                        {
                            fs.CreateFolder(Constants.FolderNames.Trash);
                        }
                        fld = new Folder(result.ID, -1, fullPath, name);
                        wmServerStorage.CreateFolder(fld);

                        DbManagerCreator creator = new DbManagerCreator();
                        DbManager        dbMan   = creator.CreateDbManager();
                        try
                        {
                            dbMan.Connect();
                            dbMan.CreateFolder(result.ID, -1, FolderType.Trash, name, fullPath, FolderSyncType.AllHeadersOnly, false, 3);
                        }
                        finally
                        {
                            dbMan.Disconnect();
                        }
                    }
                }
                catch { }
            }

            return(result);
        }
Example #8
0
        public Account CreateAccount(bool def_acct, bool deleted, string email, IncomingMailProtocol mail_protocol,
                                     string mail_inc_host, string mail_inc_login, string mail_inc_pass, int mail_inc_port,
                                     string mail_out_host, string mail_out_login, string mail_out_pass, int mail_out_port,
                                     bool mail_out_auth, string friendly_nm, bool use_friendly_nm, DefaultOrder def_order,
                                     bool getmail_at_login, MailMode mail_mode, short mails_on_server_days, string signature,
                                     SignatureType signature_type, SignatureOptions signature_opt, string delimiter, long mailbox_size,
                                     FolderSyncType inboxSyncType, bool advanced_login, int id_domain, bool mailing_list, int imap_quota, string Namespace)
        {
            WebmailResourceManager resMan = (new WebmailResourceManagerCreator()).CreateResourceManager();

            //----- validate incoming data -----
            if ((email == null) || (email.Trim().Length == 0))
            {
                if (advanced_login)
                {
                    throw new WebMailException(resMan.GetString("WarningEmailFieldBlank"));
                }
            }
            Regex r = new Regex(@"[\w!#\$%\^\{}`~&'\+-=_\.]+@[\w-\.]+");
            Match m = r.Match(email);

            if (!m.Success)
            {
                throw new WebMailException("WarningCorrectEmail");
            }
            if ((mail_inc_login == null) || (mail_inc_login.Trim().Length == 0))
            {
                if (advanced_login)
                {
                    throw new WebMailException(resMan.GetString("WarningLoginFieldBlank"));
                }
            }
            if (mail_inc_login.IndexOfAny(new char[] { '"', '/', '\\', '*', '?', '<', '>', '|', ':' }) >= 0)
            {
                throw new WebMailException(resMan.GetString("WarningCorrectLogin"));
            }
            if (string.IsNullOrEmpty(mail_inc_pass))
            {
                throw new WebMailException(resMan.GetString("WarningPassBlank"));
            }
            if ((mail_inc_host == null) || (mail_inc_host.Trim().Length == 0))
            {
                throw new WebMailException(resMan.GetString("WarningIncServerBlank"));
            }
            r = new Regex(@"[^(\w-\.)]+");
            m = r.Match(mail_inc_host);
            if (m.Success)
            {
                if (advanced_login)
                {
                    throw new WebMailException(resMan.GetString("WarningCorrectIncServer"));
                }
            }
            if ((mail_inc_port < 0) || (mail_inc_port > 65535))
            {
                if (advanced_login)
                {
                    throw new WebMailException(resMan.GetString("WarningIncPortNumber"));
                }
            }
            if ((mail_out_host == null) || (mail_out_host.Trim().Length == 0))
            {
                if (advanced_login)
                {
                    throw new WebMailException(resMan.GetString("WarningOutServerBlank"));
                }
            }
            r = new Regex(@"[^(\w-\.)]+");
            m = r.Match(mail_out_host);
            if (m.Success)
            {
                if (advanced_login)
                {
                    throw new WebMailException(resMan.GetString("WarningCorrectOutServer"));
                }
            }
            if ((mail_out_port < 0) || (mail_out_port > 65535))
            {
                if (advanced_login)
                {
                    throw new WebMailException(resMan.GetString("WarningOutPortNumber"));
                }
            }
            //------ end validate incoming data --------


            WebmailSettings  settings   = (new WebMailSettingsCreator()).CreateWebMailSettings();
            Account          newAccount = null;
            DbManagerCreator creator    = new DbManagerCreator();
            DbManager        dbMan      = creator.CreateDbManager();

            try
            {
                dbMan.Connect();

                if (settings.EnableWmServer && mail_protocol == IncomingMailProtocol.WMServer)
                {
                    string          emailDomain  = EmailAddress.GetDomainFromEmail(email);
                    string          emailUser    = EmailAddress.GetAccountNameFromEmail(email);
                    WMServerStorage storage      = new WMServerStorage(null);
                    string[]        domains      = storage.GetDomainList();
                    bool            domainExists = false;

                    foreach (string domain in domains)
                    {
                        if (string.Compare(emailDomain, domain, true, CultureInfo.InvariantCulture) == 0)
                        {
                            Domain dom = Domain.GetDomain(emailDomain);
                            if (dom != null)
                            {
                                id_domain = dom.ID;
                            }

                            WMServerUser[] users             = storage.GetUserList(emailDomain);
                            bool           userExists        = false;
                            bool           mailingListExists = false;
                            foreach (WMServerUser user in users)
                            {
                                if (string.Compare(emailUser, user.Name, true, CultureInfo.InvariantCulture) == 0)
                                {
                                    if (string.Compare("U", user.Type, true, CultureInfo.InvariantCulture) == 0)
                                    {
                                        userExists = true;
                                    }
                                    else
                                    {
                                        mailingListExists = true;
                                    }
                                    break;
                                }
                            }
                            if (!userExists && !mailing_list || !mailingListExists && mailing_list)
                            {
                                if (!mailingListExists)
                                {
                                    storage.AddUser(emailDomain, emailUser, mail_inc_pass);
                                }
                                else
                                {
                                    throw new WebMailException(resMan.GetString("ErrorPOP3IMAP4Auth"));
                                }
                            }
                            domainExists = true;
                            break;
                        }
                    }

                    if (!domainExists)
                    {
                        throw new WebMailException(resMan.GetString("ErrorDomainExist"));
                    }
                }

                if (mail_protocol == IncomingMailProtocol.WMServer)
                {
                    delimiter = ".";
                }

                newAccount = dbMan.CreateAccount(_id, def_acct, deleted, email, mail_protocol, mail_inc_host,
                                                 mail_inc_login, mail_inc_pass, mail_inc_port, mail_out_host, mail_out_login, mail_out_pass,
                                                 mail_out_port, mail_out_auth, friendly_nm, use_friendly_nm, def_order, getmail_at_login,
                                                 mail_mode, mails_on_server_days, signature, signature_type, signature_opt, delimiter,
                                                 mailbox_size, id_domain, mailing_list, imap_quota, Namespace);
                newAccount.UserOfAccount = this;

                dbMan.DbAccount = newAccount;
                FileSystem fs = new FileSystem(email, newAccount.ID, true);
                if (mail_protocol != IncomingMailProtocol.WMServer)
                {
                    fs.CreateAccount();
                }

                if (mail_protocol == IncomingMailProtocol.Pop3)
                {
                    if (settings.AllowDirectMode && settings.DirectModeIsDefault)
                    {
                        inboxSyncType = FolderSyncType.DirectMode;
                    }

                    // -- this fix for gmail pop3 --
                    if (mail_inc_host == "pop.gmail.com")
                    {
                        inboxSyncType = FolderSyncType.AllEntireMessages;
                    }
                    // -----------------------------

                    if (!settings.StoreMailsInDb)
                    {
                        fs.CreateFolder(Constants.FolderNames.Inbox);
                    }
                    dbMan.CreateFolder(newAccount.ID, -1, FolderType.Inbox, Constants.FolderNames.Inbox, Constants.FolderNames.Inbox, inboxSyncType, false, 0);

                    if (!settings.StoreMailsInDb)
                    {
                        fs.CreateFolder(Constants.FolderNames.SentItems);
                    }
                    dbMan.CreateFolder(newAccount.ID, -1, FolderType.SentItems, Constants.FolderNames.SentItems, Constants.FolderNames.SentItems, FolderSyncType.DontSync, false, 1);

                    if (!settings.StoreMailsInDb)
                    {
                        fs.CreateFolder(Constants.FolderNames.Drafts);
                    }
                    dbMan.CreateFolder(newAccount.ID, -1, FolderType.Drafts, Constants.FolderNames.Drafts, Constants.FolderNames.Drafts, FolderSyncType.DontSync, false, 2);

                    if (!settings.StoreMailsInDb)
                    {
                        fs.CreateFolder(Constants.FolderNames.Spam);
                    }
                    dbMan.CreateFolder(newAccount.ID, -1, FolderType.Spam, Constants.FolderNames.Spam, Constants.FolderNames.Spam, FolderSyncType.DontSync, false, 3);

                    if (!settings.StoreMailsInDb)
                    {
                        fs.CreateFolder(Constants.FolderNames.Trash);
                    }
                    dbMan.CreateFolder(newAccount.ID, -1, FolderType.Trash, Constants.FolderNames.Trash, Constants.FolderNames.Trash, FolderSyncType.DontSync, false, 4);
                }
                else if (mail_protocol == IncomingMailProtocol.WMServer)
                {
                    MailServerStorage wmServerStorage = MailServerStorageCreator.CreateMailServerStorage(newAccount);
                    try
                    {
                        wmServerStorage.Connect();
                        FolderCollection fc = wmServerStorage.GetFolders();

                        Folder fld = fc[FolderType.SentItems];
                        if (fld == null)
                        {
                            string       fullPath = newAccount.Delimiter + Constants.FolderNames.SentItems;
                            const string name     = Constants.FolderNames.SentItems;
                            if (!settings.StoreMailsInDb)
                            {
                                fs.CreateFolder(Constants.FolderNames.SentItems);
                            }
                            fld = new Folder(newAccount.ID, -1, fullPath, name);
                            wmServerStorage.CreateFolder(fld);
                            dbMan.CreateFolder(newAccount.ID, -1, FolderType.SentItems, name, fullPath, FolderSyncType.AllHeadersOnly, false, 1);
                        }

                        fld = fc[FolderType.Drafts];
                        if (fld == null)
                        {
                            string       fullPath = newAccount.Delimiter + Constants.FolderNames.Drafts;
                            const string name     = Constants.FolderNames.Drafts;
                            if (!settings.StoreMailsInDb)
                            {
                                fs.CreateFolder(Constants.FolderNames.Drafts);
                            }
                            fld = new Folder(newAccount.ID, -1, fullPath, name);
                            wmServerStorage.CreateFolder(fld);
                            dbMan.CreateFolder(newAccount.ID, -1, FolderType.Drafts, name, fullPath, FolderSyncType.AllHeadersOnly, false, 2);
                        }

                        fld = fc[FolderType.Spam];
                        if (fld == null)
                        {
                            string       fullPath = newAccount.Delimiter + Constants.FolderNames.Spam;
                            const string name     = Constants.FolderNames.Spam;
                            if (!settings.StoreMailsInDb)
                            {
                                fs.CreateFolder(Constants.FolderNames.Spam);
                            }
                            fld = new Folder(newAccount.ID, -1, fullPath, name);
                            wmServerStorage.CreateFolder(fld);
                            dbMan.CreateFolder(newAccount.ID, -1, FolderType.Spam, name, fullPath, FolderSyncType.AllHeadersOnly, false, 3);
                        }

                        fld = fc[FolderType.Trash];
                        if (fld == null)
                        {
                            string       fullPath = newAccount.Delimiter + Constants.FolderNames.Trash;
                            const string name     = Constants.FolderNames.Trash;
                            if (!settings.StoreMailsInDb)
                            {
                                fs.CreateFolder(Constants.FolderNames.Trash);
                            }
                            fld = new Folder(newAccount.ID, -1, fullPath, name);
                            wmServerStorage.CreateFolder(fld);
                            dbMan.CreateFolder(newAccount.ID, -1, FolderType.Trash, name, fullPath, FolderSyncType.AllHeadersOnly, false, 4);
                        }

                        fld = fc[FolderType.Quarantine];
                        if (fld == null)
                        {
                            string       fullPath = newAccount.Delimiter + Constants.FolderNames.Quarantine;
                            const string name     = Constants.FolderNames.Quarantine;
                            if (!settings.StoreMailsInDb)
                            {
                                fs.CreateFolder(Constants.FolderNames.Quarantine);
                            }
                            fld = new Folder(newAccount.ID, -1, fullPath, name);
                            wmServerStorage.CreateFolder(fld);
                            dbMan.CreateFolder(newAccount.ID, -1, FolderType.Quarantine, name, fullPath, FolderSyncType.AllHeadersOnly, false, 5);
                        }

                        dbMan.CreateFoldersTree(fc);
                    }
                    catch (Exception ex)
                    {
                        Log.WriteException(ex);
                        throw new WebMailException(ex);
                    }
                }
                else if (mail_protocol == IncomingMailProtocol.Imap4)
                {
                    MailServerStorage imapStorage = MailServerStorageCreator.CreateMailServerStorage(newAccount);
                    try
                    {
                        imapStorage.Connect();
                        FolderCollection fc  = imapStorage.GetFolders();
                        Folder           fld = fc[FolderType.Inbox];
                        if (fld != null)
                        {
                            fld.SyncType = FolderSyncType.DirectMode;
                        }

//                        if (settings.AllowDirectMode && settings.DirectModeIsDefault)
//                        {
                        fc.ReSetSyncTypeToDirectMode();
//                        }

                        fld = fc[FolderType.Drafts];
                        if (fld == null)
                        {
                            if (!settings.StoreMailsInDb)
                            {
                                fs.CreateFolder(Constants.FolderNames.Drafts);
                            }
                            dbMan.CreateFolder(newAccount.ID, -1, FolderType.Drafts, Constants.FolderNames.Drafts, Constants.FolderNames.Drafts, FolderSyncType.DontSync, false, 2);
                        }

                        fld = fc[FolderType.SentItems];
                        if (fld == null)
                        {
                            if (!settings.StoreMailsInDb)
                            {
                                fs.CreateFolder(Constants.FolderNames.SentItems);
                            }
                            dbMan.CreateFolder(newAccount.ID, -1, FolderType.SentItems, Constants.FolderNames.SentItems, Constants.FolderNames.SentItems, FolderSyncType.DontSync, false, 1);
                        }

                        fld = fc[FolderType.Spam];
                        if (fld == null)
                        {
                            if (!settings.StoreMailsInDb)
                            {
                                fs.CreateFolder(Constants.FolderNames.Spam);
                            }
                            dbMan.CreateFolder(newAccount.ID, -1, FolderType.Spam, Constants.FolderNames.Spam, Constants.FolderNames.Spam, FolderSyncType.DontSync, false, 3);
                        }

                        fld = fc[FolderType.Trash];
                        if (fld == null && settings.Imap4DeleteLikePop3)
                        {
                            if (!settings.StoreMailsInDb)
                            {
                                fs.CreateFolder(Constants.FolderNames.Trash);
                            }
                            dbMan.CreateFolder(newAccount.ID, -1, FolderType.Trash, Constants.FolderNames.Trash, Constants.FolderNames.Trash, FolderSyncType.DontSync, false, 4);
                        }

                        newAccount.Delimiter = (fc.Count > 0) ? fc[0].ImapFolder.Delimiter : newAccount.Delimiter;

                        if (!settings.StoreMailsInDb)
                        {
                            fs.CreateFoldersTree(fc);
                        }
                        dbMan.CreateFoldersTree(fc);
                    }
                    catch (Exception ex)
                    {
                        Log.WriteException(ex);
                        throw new WebMailException(ex);
                    }
                    finally
                    {
                        imapStorage.Disconnect();
                    }
                }
            }
            catch (IOException ex)
            {
                Log.WriteException(ex);
                throw new WebMailException(ex);
            }
            catch (UnauthorizedAccessException ex)
            {
                Log.WriteException(ex);
                throw new WebMailException(ex);
            }
            catch (WebMailException ex)
            {
                Log.WriteException(ex);
                if (newAccount != null)
                {
                    if (dbMan.IsConnected)
                    {
                        dbMan.DeleteAccount(newAccount.ID);
                    }
                    else
                    {
                        dbMan.Connect();
                        dbMan.DeleteAccount(newAccount.ID);
                    }
                }
                throw;
            }
            finally
            {
                dbMan.Disconnect();
            }
            return(newAccount);
        }