private void LoadSettings()
        {
            try{
                // ??? ToDo: calculate settings md5 hash here, see if changed at all

                lock(this){
                    m_SettingsDate = DateTime.Now;

                    DataRow dr = m_pApi.GetSettings();

                    //--- Try to get mailstore path from API init string ----------------------------------//
                    m_MailStorePath = "Settings\\MailStore";
                    // mailstorepath=
                    string[] parameters = m_ApiInitString.Replace("\r\n","\n").Split('\n');
                    foreach(string param in parameters){
                        if(param.ToLower().IndexOf("mailstorepath=") > -1){
                            m_MailStorePath = param.Substring(14);
                        }
                    }
                    // Fix mail store path, if isn't ending with \
                    if(m_MailStorePath.Length > 0 && !m_MailStorePath.EndsWith("\\")){
                        m_MailStorePath += "\\";
                    }
                    if(!Path.IsPathRooted(m_MailStorePath)){
                        m_MailStorePath = m_pOwnerServer.StartupPath + m_MailStorePath;
                    }
                    // Make path directory separator to suit for current platform
                    m_MailStorePath = API_Utlis.PathFix(m_MailStorePath);
                    //------------------------------------------------------------------------------------//

                    //--- System settings -----------------------------------------------------------------//
                    m_AuthType        = (MailServerAuthType_enum)ConvertEx.ToInt32(dr["ServerAuthenticationType"]);
                    m_Auth_Win_Domain = ConvertEx.ToString(dr["ServerAuthWinDomain"]);
                    //-------------------------------------------------------------------------------------//

                    #region SMTP

                    //------- SMTP Settings ---------------------------------------------//
                    try{
                        List<BindInfo> smtpIpBinds = new List<BindInfo>();
                        foreach(DataRow dr_Bind in dr.Table.DataSet.Tables["SMTP_Bindings"].Rows){
                            X509Certificate cert = null;
                            if(!dr_Bind.IsNull("SSL_Certificate") && ((byte[])dr_Bind["SSL_Certificate"]).Length > 0){
                                cert = new X509Certificate((byte[])dr_Bind["SSL_Certificate"]);
                            }

                            smtpIpBinds.Add(new BindInfo(
                                IPAddress.Parse(ConvertEx.ToString(dr_Bind["IP"])),
                                ConvertEx.ToInt32(dr_Bind["Port"]),
                                ConvertEx.ToBoolean(dr_Bind["SSL"]),
                                cert
                            ));
                        }
                        m_pSmtpServer.BindInfo = smtpIpBinds.ToArray();
                        m_pSmtpServer.MaxConnections      = ConvertEx.ToInt32(dr["SMTP_Threads"]);
                        m_pSmtpServer.MaxConnectionsPerIP = ConvertEx.ToInt32(dr["SMTP_MaxConnectionsPerIP"]);
                        m_pSmtpServer.SessionIdleTimeOut  = ConvertEx.ToInt32(dr["SMTP_SessionIdleTimeOut"]) * 1000; // Seconds to milliseconds
                        m_pSmtpServer.MaxMessageSize      = ConvertEx.ToInt32(dr["MaxMessageSize"]) * 1000000;       // Mb to byte.
                        m_pSmtpServer.MaxRecipients       = ConvertEx.ToInt32(dr["MaxRecipients"]);
                        m_pSmtpServer.MaxBadCommands      = ConvertEx.ToInt32(dr["SMTP_MaxBadCommands"]);
                        m_pSmtpServer.HostName            = ConvertEx.ToString(dr["SMTP_HostName"]);
                        m_pSmtpServer.GreetingText        = ConvertEx.ToString(dr["SMTP_GreetingText"]);
                        if(m_AuthType == MailServerAuthType_enum.Windows){
                            // For windows auth, we can allow only plain text login, because otherwise
                            // we can't do auth against windows (it requires user name and password).
                            m_pSmtpServer.SupportedAuthentications = LumiSoft.Net.AUTH.SaslAuthTypes.Login | LumiSoft.Net.AUTH.SaslAuthTypes.Plain;
                        }
                        else{
                            m_pSmtpServer.SupportedAuthentications = LumiSoft.Net.AUTH.SaslAuthTypes.All;
                        }
                        m_SMTP_RequireAuth    = ConvertEx.ToBoolean(dr["SMTP_RequireAuth"]);
                        m_SMTP_DefaultDomain  = ConvertEx.ToString(dr["SMTP_DefaultDomain"]);
                        m_pSmtpServer.Enabled = ConvertEx.ToBoolean(dr["SMTP_Enabled"]);
                    }
                    catch(Exception x){
                        OnError(x);
                    }
                    //-------------------------------------------------------------------//

                    #endregion

                    #region POP3

                    //------- POP3 Settings -------------------------------------//
                    try{
                        List<BindInfo> pop3IpBinds = new List<BindInfo>();
                        foreach(DataRow dr_Bind in dr.Table.DataSet.Tables["POP3_Bindings"].Rows){
                            X509Certificate cert = null;
                            if(!dr_Bind.IsNull("SSL_Certificate") && ((byte[])dr_Bind["SSL_Certificate"]).Length > 0){
                                cert = new X509Certificate((byte[])dr_Bind["SSL_Certificate"]);
                            }

                            pop3IpBinds.Add(new BindInfo(
                                IPAddress.Parse(ConvertEx.ToString(dr_Bind["IP"])),
                                ConvertEx.ToInt32(dr_Bind["Port"]),
                                ConvertEx.ToBoolean(dr_Bind["SSL"]),
                                cert
                            ));
                        }
                        m_pPop3Server.BindInfo = pop3IpBinds.ToArray();
                        m_pPop3Server.MaxConnections      = ConvertEx.ToInt32(dr["POP3_Threads"]);
                        m_pPop3Server.MaxConnectionsPerIP = ConvertEx.ToInt32(dr["POP3_MaxConnectionsPerIP"]);
                        m_pPop3Server.SessionIdleTimeOut  = ConvertEx.ToInt32(dr["POP3_SessionIdleTimeOut"]) * 1000; // Seconds to milliseconds
                        m_pPop3Server.MaxBadCommands      = ConvertEx.ToInt32(dr["POP3_MaxBadCommands"]);
                        m_pPop3Server.HostName            = ConvertEx.ToString(dr["POP3_HostName"]);
                        m_pPop3Server.GreetingText        = ConvertEx.ToString(dr["POP3_GreetingText"]);
                        if(m_AuthType == MailServerAuthType_enum.Windows){
                            // For windows auth, we can allow only plain text login, because otherwise
                            // we can't do auth against windows (it requires user name and password).
                            m_pPop3Server.SupportedAuthentications = LumiSoft.Net.AUTH.SaslAuthTypes.Login | LumiSoft.Net.AUTH.SaslAuthTypes.Plain;
                        }
                        else{
                            m_pPop3Server.SupportedAuthentications = LumiSoft.Net.AUTH.SaslAuthTypes.All;
                        }
                        m_pPop3Server.Enabled = ConvertEx.ToBoolean(dr["POP3_Enabled"]);
                    }
                    catch(Exception x){
                        OnError(x);
                    }
                    //-----------------------------------------------------------//

                    #endregion

                    #region IMAP

                    //------- IMAP Settings -------------------------------------//
                    try{
                        List<BindInfo> imapIpBinds = new List<BindInfo>();
                        foreach(DataRow dr_Bind in dr.Table.DataSet.Tables["IMAP_Bindings"].Rows){
                            X509Certificate cert = null;
                            if(!dr_Bind.IsNull("SSL_Certificate") && ((byte[])dr_Bind["SSL_Certificate"]).Length > 0){
                                cert = new X509Certificate((byte[])dr_Bind["SSL_Certificate"]);
                            }

                            imapIpBinds.Add(new BindInfo(
                                IPAddress.Parse(ConvertEx.ToString(dr_Bind["IP"])),
                                ConvertEx.ToInt32(dr_Bind["Port"]),
                                ConvertEx.ToBoolean(dr_Bind["SSL"]),
                                cert
                            ));
                        }
                        m_pImapServer.BindInfo = imapIpBinds.ToArray();
                        m_pImapServer.MaxConnections      = ConvertEx.ToInt32(dr["IMAP_Threads"]);
                        m_pImapServer.MaxConnectionsPerIP = ConvertEx.ToInt32(dr["IMAP_Threads"]);
                        m_pImapServer.SessionIdleTimeOut  = ConvertEx.ToInt32(dr["IMAP_SessionIdleTimeOut"]) * 1000; // Seconds to milliseconds
                        m_pImapServer.MaxBadCommands      = ConvertEx.ToInt32(dr["IMAP_MaxBadCommands"]);
                        m_pImapServer.HostName            = ConvertEx.ToString(dr["IMAP_HostName"]);
                        if(m_AuthType == MailServerAuthType_enum.Windows){
                            // For windows auth, we can allow only plain text login, because otherwise
                            // we can't do auth against windows (it requires user name and password).
                            m_pImapServer.SupportedAuthentications = LumiSoft.Net.AUTH.SaslAuthTypes.Login | LumiSoft.Net.AUTH.SaslAuthTypes.Plain;
                        }
                        else{
                            m_pImapServer.SupportedAuthentications = LumiSoft.Net.AUTH.SaslAuthTypes.All;
                        }
                        m_pImapServer.GreetingText = ConvertEx.ToString(dr["IMAP_GreetingText"]);
                        m_pImapServer.Enabled      = ConvertEx.ToBoolean(dr["IMAP_Enabled"]);
                    }
                    catch(Exception x){
                        OnError(x);
                    }
                    //-----------------------------------------------------------//

                    #endregion

                    #region Relay

                    //------- Relay ----------------------------------------------
                    try{
                        m_pRelayServer.HostName = ConvertEx.ToString(dr["Relay_HostName"]);
                        m_pRelayServer.SessionIdleTimeout = ConvertEx.ToInt32(dr["Relay_SessionIdleTimeOut"]);
                        m_pRelayServer.MaximumConnections = ConvertEx.ToInt32(dr["MaxRelayThreads"]);
                        m_pRelayServer.MaximumConnectionsPerIP = ConvertEx.ToInt32(dr["Relay_MaxConnectionsPerIP"]);
                        m_pRelayServer.UseSmartHost = ConvertEx.ToBoolean(dr["UseSmartHost"]);
                        m_pRelayServer.SmartHost = ConvertEx.ToString(dr["SmartHost"]);
                        m_pRelayServer.SmartHostPort = ConvertEx.ToInt32(dr["SmartHostPort"]);
                        m_pRelayServer.SmartHostUseSSL = ConvertEx.ToBoolean(dr["SmartHostUseSSL"]);
                        m_pRelayServer.SmartHostUserName = ConvertEx.ToString(dr["SmartHostUserName"]);
                        m_pRelayServer.SmartHostPassword = ConvertEx.ToString(dr["SmartHostPassword"]);
                        m_pRelayServer.Dns1 = ConvertEx.ToString(dr["Dns1"]);
                        m_pRelayServer.Dns2 = ConvertEx.ToString(dr["Dns2"]);
                        m_pRelayServer.RelayInterval = ConvertEx.ToInt32(dr["RelayInterval"]);
                        m_pRelayServer.RelayRetryInterval = ConvertEx.ToInt32(dr["RelayRetryInterval"]);
                        m_pRelayServer.UndeliveredWarningAfter = ConvertEx.ToInt32(dr["RelayUndeliveredWarning"]);
                        m_pRelayServer.UndeliveredAfter = ConvertEx.ToInt32(dr["RelayUndelivered"]) * 60;
                        m_pRelayServer.UndeliveredWarningMessage = null;
                        m_pRelayServer.UndeliveredMessage = null;
                        foreach(DataRow drReturnMessage in dr.Table.DataSet.Tables["ServerReturnMessages"].Rows){
                            if(drReturnMessage["MessageType"].ToString() == "delayed_delivery_warning"){
                                m_pRelayServer.UndeliveredWarningMessage = new ServerReturnMessage(drReturnMessage["Subject"].ToString(),drReturnMessage["BodyTextRtf"].ToString());
                            }
                            else if(drReturnMessage["MessageType"].ToString() == "undelivered"){
                                m_pRelayServer.UndeliveredMessage = new ServerReturnMessage(drReturnMessage["Subject"].ToString(),drReturnMessage["BodyTextRtf"].ToString());
                            }
                        }
                        if(dr["RelayLocalIP"].ToString().Length > 0){
                            m_pRelayServer.SendingIP = IPAddress.Parse(dr["RelayLocalIP"].ToString());
                        }
                        else{
                            m_pRelayServer.SendingIP = null;
                        }
                        m_pRelayServer.LogCommands = ConvertEx.ToBoolean(dr["LogRelayCmds"]);
                        if(dr["Relay_LogPath"].ToString().Length == 0){
                            m_pRelayServer.LogsPath = m_pOwnerServer.StartupPath + "Logs\\Relay\\";
                        }
                        else{
                            m_pRelayServer.LogsPath = dr["Relay_LogPath"].ToString() + "\\";
                        }
                        m_Relay_LogPath = m_pRelayServer.LogsPath;
                        m_pRelayServer.StoreUndeliveredMessages = ConvertEx.ToBoolean(dr["StoreUndeliveredMessages"]);
                        if(!m_pRelayServer.Running){
                            m_pRelayServer.Start();
                        }
                    }
                    catch(Exception x){
                        OnError(x);
                    }
                    //------------------------------------------------------------

                    #endregion

                    #region FETCH

                    //----- Fetch POP3 settings ----------------------------------//
                    try{
                        m_pFetchServer.Enabled       = ConvertEx.ToBoolean(dr["FetchPop3_Enabled"]);
                        m_pFetchServer.FetchInterval = ConvertEx.ToInt32(dr["FetchPOP3_Interval"]);
                    }
                    catch(Exception x){
                        OnError(x);
                    }
                    //------------------------------------------------------------//

                    #endregion

                    #region SIP

                    List<BindInfo> sipIpBinds = new List<BindInfo>();
                    foreach(DataRow dr_Bind in dr.Table.DataSet.Tables["SIP_Bindings"].Rows){
                        X509Certificate cert = null;
                        if(!dr_Bind.IsNull("SSL_Certificate") && ((byte[])dr_Bind["SSL_Certificate"]).Length > 0){
                            cert = new X509Certificate((byte[])dr_Bind["SSL_Certificate"]);
                        }

                        if(dr_Bind["Protocol"].ToString().ToUpper() == "TCP"){
                            sipIpBinds.Add(new BindInfo(
                                IPAddress.Parse(ConvertEx.ToString(dr_Bind["IP"])),
                                ConvertEx.ToInt32(dr_Bind["Port"]),
                                ConvertEx.ToBoolean(dr_Bind["SSL"]),
                                cert
                            ));
                        }
                        else{
                            sipIpBinds.Add(new BindInfo(
                                BindInfoProtocol.UDP,
                                IPAddress.Parse(ConvertEx.ToString(dr_Bind["IP"])),
                                ConvertEx.ToInt32(dr_Bind["Port"])
                            ));
                        }
                    }
                    m_pSipServer.BindInfo = sipIpBinds.ToArray();

                    m_pSipServer.Enabled           = ConvertEx.ToBoolean(dr["SIP_Enabled"]);
                    m_pSipServer.HostName          = dr["SIP_HostName"].ToString();
                    m_pSipServer.MinimumExpireTime = ConvertEx.ToInt32(dr["SIP_MinExpires"]);

                    #endregion

                    #region LOGGING

                    //----- Logging settings -------------------------------------//
                    try{
                        m_pSmtpServer.LogCommands  = ConvertEx.ToBoolean(dr["LogSMTPCmds"],false);
                        m_pPop3Server.LogCommands  = ConvertEx.ToBoolean(dr["LogPOP3Cmds"],false);
                        m_pImapServer.LogCommands  = ConvertEx.ToBoolean(dr["LogIMAPCmds"],false);
                        m_pFetchServer.LogCommands = ConvertEx.ToBoolean(dr["LogFetchPOP3Cmds"],false);

                        m_SMTP_LogPath   = ConvertEx.ToString(dr["SMTP_LogPath"])   + "\\";
                        m_POP3_LogPath   = ConvertEx.ToString(dr["POP3_LogPath"])   + "\\";
                        m_IMAP_LogPath   = ConvertEx.ToString(dr["IMAP_LogPath"])   + "\\";
                        m_Server_LogPath = ConvertEx.ToString(dr["Server_LogPath"]) + "\\";
                        m_Fetch_LogPath  = ConvertEx.ToString(dr["FetchPOP3_LogPath"]) + "\\";

                        //----- If no log path, use default ----------------
                        if(dr["SMTP_LogPath"].ToString().Trim().Length == 0){
                            m_SMTP_LogPath = m_pOwnerServer.StartupPath + "Logs\\SMTP\\";
                        }
                        if(dr["POP3_LogPath"].ToString().Trim().Length == 0){
                            m_POP3_LogPath = m_pOwnerServer.StartupPath + "Logs\\POP3\\";
                        }
                        if(dr["IMAP_LogPath"].ToString().Trim().Length == 0){
                            m_IMAP_LogPath = m_pOwnerServer.StartupPath + "Logs\\IMAP\\";
                        }
                        if(dr["Server_LogPath"].ToString().Trim().Length == 0){
                            m_Server_LogPath = m_pOwnerServer.StartupPath + "Logs\\Server\\";
                        }
                        if(dr["FetchPOP3_LogPath"].ToString().Trim().Length == 0){
                            m_Fetch_LogPath = m_pOwnerServer.StartupPath + "Logs\\FetchPOP3\\";
                        }
                        m_pFetchServer.LogPath = m_Fetch_LogPath;
                    }
                    catch(Exception x){
                        OnError(x);
                    }
                    //------------------------------------------------------------//

                    #endregion

            //		SCore.WriteLog(m_Server_LogPath + "server.log","//---- Server settings loaded " + DateTime.Now);
                }
            }
            catch(Exception x){
                Error.DumpError(x,new System.Diagnostics.StackTrace());
            }
        }
Example #2
0
        private void LoadSettings()
		{
			try{
				lock(this){
					DataRow dr = m_pApi.GetSettings();

                    // See if settings changed. Skip loading if steeings has not changed.
                    if(Convert.ToDateTime(dr["SettingsDate"]).Equals(m_SettingsDate)){
                        return;
                    }
                    m_SettingsDate = Convert.ToDateTime(dr["SettingsDate"]);
                                  
                    //--- Try to get mailstore path from API init string ----------------------------------//
                    m_MailStorePath = "Settings\\MailStore";
                    // mailstorepath=
			        string[] parameters = m_ApiInitString.Replace("\r\n","\n").Split('\n');
			        foreach(string param in parameters){
                        if(param.ToLower().IndexOf("mailstorepath=") > -1){
					        m_MailStorePath = param.Substring(14);
                        }
			        }
                    // Fix mail store path, if isn't ending with \
			        if(m_MailStorePath.Length > 0 && !m_MailStorePath.EndsWith("\\")){
				        m_MailStorePath += "\\"; 
			        }
                    if(!Path.IsPathRooted(m_MailStorePath)){
				        m_MailStorePath = m_pOwnerServer.StartupPath + m_MailStorePath;
			        }
                    // Make path directory separator to suit for current platform
                    m_MailStorePath = API_Utlis.PathFix(m_MailStorePath);                    
                    //------------------------------------------------------------------------------------//

                    //--- System settings -----------------------------------------------------------------//
                    m_AuthType         = (MailServerAuthType_enum)ConvertEx.ToInt32(dr["ServerAuthenticationType"]);
                    m_Auth_Win_Domain  = ConvertEx.ToString(dr["ServerAuthWinDomain"]);
                    m_Auth_LDAP_Server = ConvertEx.ToString(dr["LdapServer"]);
                    m_Auth_LDAP_DN     = ConvertEx.ToString(dr["LdapDN"]);
                    //-------------------------------------------------------------------------------------//

                    #region General

                    List<string> dnsServers = new List<string>();
                    foreach(DataRow drX in dr.Table.DataSet.Tables["DnsServers"].Rows){
                        dnsServers.Add(drX["IP"].ToString());
                    }
                    LumiSoft.Net.DNS.Client.Dns_Client.DnsServers = dnsServers.ToArray();

                    #endregion

                    #region SMTP

                    //------- SMTP Settings ---------------------------------------------//
                    try{
                        List<IPBindInfo> smtpIpBinds = new List<IPBindInfo>();
                        foreach(DataRow dr_Bind in dr.Table.DataSet.Tables["SMTP_Bindings"].Rows){
                            smtpIpBinds.Add(new IPBindInfo(
                                ConvertEx.ToString(dr_Bind["HostName"]),
                                IPAddress.Parse(ConvertEx.ToString(dr_Bind["IP"])),
                                ConvertEx.ToInt32(dr_Bind["Port"]),
                                ParseSslMode(dr_Bind["SSL"].ToString()),
                                PaseCertificate(dr_Bind["SSL_Certificate"])
                            ));
                        }                        
                        m_pSmtpServer.Bindings = smtpIpBinds.ToArray();                        
					    m_pSmtpServer.MaxConnections      = ConvertEx.ToInt32(dr["SMTP_Threads"]);
                        m_pSmtpServer.MaxConnectionsPerIP = ConvertEx.ToInt32(dr["SMTP_MaxConnectionsPerIP"]);
					    m_pSmtpServer.SessionIdleTimeout  = ConvertEx.ToInt32(dr["SMTP_SessionIdleTimeOut"]);
					    m_pSmtpServer.MaxMessageSize      = ConvertEx.ToInt32(dr["MaxMessageSize"]) * 1000000;       // Mb to byte.
					    m_pSmtpServer.MaxRecipients       = ConvertEx.ToInt32(dr["MaxRecipients"]);
					    m_pSmtpServer.MaxBadCommands      = ConvertEx.ToInt32(dr["SMTP_MaxBadCommands"]);
                        m_pSmtpServer.MaxTransactions     = ConvertEx.ToInt32(dr["SMTP_MaxTransactions"]);
                        m_pSmtpServer.GreetingText        = ConvertEx.ToString(dr["SMTP_GreetingText"]);                        
                        m_pSmtpServer.ServiceExtentions   = new string[]{
                            SMTP_ServiceExtensions.PIPELINING,
                            SMTP_ServiceExtensions.SIZE,
                            SMTP_ServiceExtensions.STARTTLS,
                            SMTP_ServiceExtensions._8BITMIME,
                            SMTP_ServiceExtensions.BINARYMIME,
                            SMTP_ServiceExtensions.CHUNKING,
                            SMTP_ServiceExtensions.DSN
                        };
                        m_SMTP_RequireAuth                = ConvertEx.ToBoolean(dr["SMTP_RequireAuth"]);					
					    m_SMTP_DefaultDomain              = ConvertEx.ToString(dr["SMTP_DefaultDomain"]);                        
                        if(ConvertEx.ToBoolean(dr["SMTP_Enabled"])){
                            m_pSmtpServer.Start();
                        }
                        else{
                            m_pSmtpServer.Stop();
                        }
                    }
                    catch(Exception x){
                        OnError(x);
                    }
                    //-------------------------------------------------------------------//

                    #endregion

                    #region POP3

                    //------- POP3 Settings -------------------------------------//
                    try{
					    List<IPBindInfo> pop3IpBinds = new List<IPBindInfo>();
                        foreach(DataRow dr_Bind in dr.Table.DataSet.Tables["POP3_Bindings"].Rows){
                            pop3IpBinds.Add(new IPBindInfo(
                                ConvertEx.ToString(dr_Bind["HostName"]),
                                IPAddress.Parse(ConvertEx.ToString(dr_Bind["IP"])),
                                ConvertEx.ToInt32(dr_Bind["Port"]),
                                ParseSslMode(dr_Bind["SSL"].ToString()),
                                PaseCertificate(dr_Bind["SSL_Certificate"])
                            ));
                        }
                        m_pPop3Server.Bindings = pop3IpBinds.ToArray();
					    m_pPop3Server.MaxConnections      = ConvertEx.ToInt32(dr["POP3_Threads"]);
                        m_pPop3Server.MaxConnectionsPerIP = ConvertEx.ToInt32(dr["POP3_MaxConnectionsPerIP"]);
					    m_pPop3Server.SessionIdleTimeout  = ConvertEx.ToInt32(dr["POP3_SessionIdleTimeOut"]);
					    m_pPop3Server.MaxBadCommands      = ConvertEx.ToInt32(dr["POP3_MaxBadCommands"]);
                        m_pPop3Server.GreetingText        = ConvertEx.ToString(dr["POP3_GreetingText"]);
                        if(ConvertEx.ToBoolean(dr["POP3_Enabled"])){
                            m_pPop3Server.Start();
                        }
                        else{
                            m_pPop3Server.Stop();
                        }
                    }
                    catch(Exception x){
                        OnError(x);
                    }
                    //-----------------------------------------------------------//

                    #endregion

                    #region IMAP

                    //------- IMAP Settings -------------------------------------//
                    try{
					    List<IPBindInfo> imapIpBinds = new List<IPBindInfo>();
                        foreach(DataRow dr_Bind in dr.Table.DataSet.Tables["IMAP_Bindings"].Rows){
                            imapIpBinds.Add(new IPBindInfo(
                                ConvertEx.ToString(dr_Bind["HostName"]),
                                IPAddress.Parse(ConvertEx.ToString(dr_Bind["IP"])),
                                ConvertEx.ToInt32(dr_Bind["Port"]),
                                ParseSslMode(dr_Bind["SSL"].ToString()),
                                PaseCertificate(dr_Bind["SSL_Certificate"])
                            ));
                        }
                        m_pImapServer.Bindings = imapIpBinds.ToArray();
					    m_pImapServer.MaxConnections      = ConvertEx.ToInt32(dr["IMAP_Threads"]);
                        m_pImapServer.MaxConnectionsPerIP = ConvertEx.ToInt32(dr["IMAP_Threads"]);
					    m_pImapServer.SessionIdleTimeout  = ConvertEx.ToInt32(dr["IMAP_SessionIdleTimeOut"]);
					    m_pImapServer.MaxBadCommands      = ConvertEx.ToInt32(dr["IMAP_MaxBadCommands"]);                        
                        m_pImapServer.GreetingText = ConvertEx.ToString(dr["IMAP_GreetingText"]);
					    if(ConvertEx.ToBoolean(dr["IMAP_Enabled"])){
                            m_pImapServer.Start();
                        }
                        else{
                            m_pImapServer.Stop();
                        }
                    }
                    catch(Exception x){
                        OnError(x);
                    }
                    //-----------------------------------------------------------//

                    #endregion

                    #region Relay

                    //------- Relay ----------------------------------------------
                    try{
                        List<IPBindInfo> relayIpBinds = new List<IPBindInfo>();
                        foreach(DataRow dr_Bind in dr.Table.DataSet.Tables["Relay_Bindings"].Rows){
                            relayIpBinds.Add(new IPBindInfo(
                                ConvertEx.ToString(dr_Bind["HostName"]),
                                IPAddress.Parse(ConvertEx.ToString(dr_Bind["IP"])),
                                0,
                                SslMode.None,
                                null
                            ));
                        }

                        List<Relay_SmartHost> relaySmartHosts = new List<Relay_SmartHost>();
                        foreach(DataRow drX in dr.Table.DataSet.Tables["Relay_SmartHosts"].Rows){
                            relaySmartHosts.Add(new Relay_SmartHost(
                                ConvertEx.ToString(drX["Host"]),
                                ConvertEx.ToInt32(drX["Port"]),
                                (SslMode)Enum.Parse(typeof(SslMode),drX["SslMode"].ToString()),
                                ConvertEx.ToString(drX["UserName"]),
                                ConvertEx.ToString(drX["Password"])
                            ));
                        }
                        
                        m_pRelayServer.RelayMode = (Relay_Mode)Enum.Parse(typeof(Relay_Mode),dr["Relay_Mode"].ToString());
                        m_pRelayServer.SmartHostsBalanceMode = (BalanceMode)Enum.Parse(typeof(BalanceMode),dr["Relay_SmartHostsBalanceMode"].ToString());
                        m_pRelayServer.SmartHosts = relaySmartHosts.ToArray();
                        m_pRelayServer.SessionIdleTimeout = ConvertEx.ToInt32(dr["Relay_SessionIdleTimeOut"]);
                        m_pRelayServer.MaxConnections = ConvertEx.ToInt32(dr["MaxRelayThreads"]);
                        m_pRelayServer.MaxConnectionsPerIP = ConvertEx.ToInt32(dr["Relay_MaxConnectionsPerIP"]);    
                        m_pRelayServer.RelayInterval = ConvertEx.ToInt32(dr["RelayInterval"]);
                        m_pRelayServer.RelayRetryInterval = ConvertEx.ToInt32(dr["RelayRetryInterval"]);
                        m_pRelayServer.DelayedDeliveryNotifyAfter = ConvertEx.ToInt32(dr["RelayUndeliveredWarning"]);
                        m_pRelayServer.UndeliveredAfter = ConvertEx.ToInt32(dr["RelayUndelivered"]) * 60;
                        m_pRelayServer.DelayedDeliveryMessage = null;
                        m_pRelayServer.UndeliveredMessage = null;
                        foreach(DataRow drReturnMessage in dr.Table.DataSet.Tables["ServerReturnMessages"].Rows){
                            if(drReturnMessage["MessageType"].ToString() == "delayed_delivery_warning"){
                                m_pRelayServer.DelayedDeliveryMessage = new ServerReturnMessage(drReturnMessage["Subject"].ToString(),drReturnMessage["BodyTextRtf"].ToString());
                            }
                            else if(drReturnMessage["MessageType"].ToString() == "undelivered"){
                                m_pRelayServer.UndeliveredMessage = new ServerReturnMessage(drReturnMessage["Subject"].ToString(),drReturnMessage["BodyTextRtf"].ToString());
                            }                            
                        }
                        m_pRelayServer.Bindings = relayIpBinds.ToArray();
                        if(ConvertEx.ToBoolean(dr["LogRelayCmds"])){
                            m_pRelayServer.Logger = new LumiSoft.Net.Log.Logger();
                            m_pRelayServer.Logger.WriteLog += new EventHandler<LumiSoft.Net.Log.WriteLogEventArgs>(m_pRelayServer_WriteLog);
                        }
                        else{
                            if(m_pRelayServer.Logger != null){
                                m_pRelayServer.Logger.Dispose();
                                m_pRelayServer.Logger = null;
                            }                            
                        }               
                        if(dr["Relay_LogPath"].ToString().Length == 0){
						    m_Relay_LogPath = m_pOwnerServer.StartupPath + "Logs\\Relay\\";
					    }
					    else{
						    m_Relay_LogPath = dr["Relay_LogPath"].ToString() + "\\";
					    }
                        m_pRelayServer.StoreUndeliveredMessages = ConvertEx.ToBoolean(dr["StoreUndeliveredMessages"]);
                        if(!m_pRelayServer.IsRunning){
                            m_pRelayServer.Start();
                        }
                    }
                    catch(Exception x){
                        OnError(x);
                    }
                    //------------------------------------------------------------

                    #endregion

                    #region FETCH

                    //----- Fetch POP3 settings ----------------------------------//
                    try{
                        m_pFetchServer.Enabled       = ConvertEx.ToBoolean(dr["FetchPop3_Enabled"]);
                        m_pFetchServer.FetchInterval = ConvertEx.ToInt32(dr["FetchPOP3_Interval"]);
                    }                    
                    catch(Exception x){
                        OnError(x);
                    }
                    //------------------------------------------------------------//

                    #endregion

                    #region SIP

                    List<IPBindInfo> sipIpBinds = new List<IPBindInfo>();
                    foreach(DataRow dr_Bind in dr.Table.DataSet.Tables["SIP_Bindings"].Rows){
                        if(dr_Bind["Protocol"].ToString().ToUpper() == "TCP"){
                            sipIpBinds.Add(new IPBindInfo(
                                ConvertEx.ToString(dr_Bind["HostName"]),
                                IPAddress.Parse(ConvertEx.ToString(dr_Bind["IP"])),
                                ConvertEx.ToInt32(dr_Bind["Port"]),
                                ParseSslMode(dr_Bind["SSL"].ToString()),
                                PaseCertificate(dr_Bind["SSL_Certificate"])
                            ));
                        }
                        else{
                            sipIpBinds.Add(new IPBindInfo(
                                ConvertEx.ToString(dr_Bind["HostName"]),
                                BindInfoProtocol.UDP,
                                IPAddress.Parse(ConvertEx.ToString(dr_Bind["IP"])),
                                ConvertEx.ToInt32(dr_Bind["Port"])
                            ));
                        }
                    }
                    m_pSipServer.Stack.BindInfo = sipIpBinds.ToArray();

                    if(ConvertEx.ToBoolean(dr["SIP_Enabled"])){
                        m_pSipServer.Stack.Start();
                    }
                    else{
                        m_pSipServer.Stack.Stop();
                    }
                    m_pSipServer.Stack.MinimumExpireTime = ConvertEx.ToInt32(dr["SIP_MinExpires"]);
                    m_pSipServer.ProxyMode               = (SIP_ProxyMode)Enum.Parse(typeof(SIP_ProxyMode),dr["SIP_ProxyMode"].ToString());

                    #endregion

                    #region LOGGING

                    //----- Logging settings -------------------------------------//
                    try{
                        if(ConvertEx.ToBoolean(dr["LogSMTPCmds"],false)){                            
                            m_pSmtpServer.Logger = new LumiSoft.Net.Log.Logger();
                            m_pSmtpServer.Logger.WriteLog += new EventHandler<LumiSoft.Net.Log.WriteLogEventArgs>(SMTP_Server_SessionLog);
                        }
                        else{
                            m_pSmtpServer.Logger = null;
                        }
                        if(ConvertEx.ToBoolean(dr["LogPOP3Cmds"],false)){                            
                            m_pPop3Server.Logger = new LumiSoft.Net.Log.Logger();
                            m_pPop3Server.Logger.WriteLog += new EventHandler<LumiSoft.Net.Log.WriteLogEventArgs>(POP3_Server_SessionLog);
                        }
                        else{
                            m_pPop3Server.Logger = null;
                        }
                        if(ConvertEx.ToBoolean(dr["LogIMAPCmds"],false)){                            
                            m_pImapServer.Logger = new LumiSoft.Net.Log.Logger();
                            m_pImapServer.Logger.WriteLog += new EventHandler<LumiSoft.Net.Log.WriteLogEventArgs>(IMAP_Server_SessionLog);
                        }
                        else{
                            m_pImapServer.Logger = null;
                        }
					    m_pFetchServer.LogCommands = ConvertEx.ToBoolean(dr["LogFetchPOP3Cmds"],false);
    					
					    m_SMTP_LogPath   = API_Utlis.PathFix(ConvertEx.ToString(dr["SMTP_LogPath"]) + "\\");
					    m_POP3_LogPath   = API_Utlis.PathFix(ConvertEx.ToString(dr["POP3_LogPath"]) + "\\");
					    m_IMAP_LogPath   = API_Utlis.PathFix(ConvertEx.ToString(dr["IMAP_LogPath"]) + "\\");
					    m_Server_LogPath = API_Utlis.PathFix(ConvertEx.ToString(dr["Server_LogPath"]) + "\\");
					    m_Fetch_LogPath  = API_Utlis.PathFix(ConvertEx.ToString(dr["FetchPOP3_LogPath"]) + "\\");
    					
					    //----- If no log path, use default ----------------
					    if(dr["SMTP_LogPath"].ToString().Trim().Length == 0){
						    m_SMTP_LogPath = API_Utlis.PathFix(m_pOwnerServer.StartupPath + "Logs\\SMTP\\");
					    }
					    if(dr["POP3_LogPath"].ToString().Trim().Length == 0){
						    m_POP3_LogPath = API_Utlis.PathFix(m_pOwnerServer.StartupPath + "Logs\\POP3\\");
					    }
					    if(dr["IMAP_LogPath"].ToString().Trim().Length == 0){
						    m_IMAP_LogPath = API_Utlis.PathFix(m_pOwnerServer.StartupPath + "Logs\\IMAP\\");
					    }
					    if(dr["Server_LogPath"].ToString().Trim().Length == 0){
						    m_Server_LogPath = API_Utlis.PathFix(m_pOwnerServer.StartupPath + "Logs\\Server\\");
					    }					
					    if(dr["FetchPOP3_LogPath"].ToString().Trim().Length == 0){
						    m_Fetch_LogPath = API_Utlis.PathFix(m_pOwnerServer.StartupPath + "Logs\\FetchPOP3\\");
					    }
					    m_pFetchServer.LogPath = m_Fetch_LogPath;
				    }
                    catch(Exception x){
                        OnError(x);
                    }
					//------------------------------------------------------------//

                    #endregion

			//		SCore.WriteLog(m_Server_LogPath + "server.log","//---- Server settings loaded " + DateTime.Now);
				}
			}
			catch(Exception x){
				Error.DumpError(x,new System.Diagnostics.StackTrace());
			}
		}