Пример #1
0
        /*
         * Called to verify the email account against the internal user database.
         * This requires the userName which is in format [email protected] format.
         * If the function returns true, the account is authenticated
         * and valid for the transaction to proceed.
         * Otherwise, don't proceed with it... It is invalid.
         */
        private bool VerifyEMail(string mailAddress)
        {
            try
            {
                mailAddress = mailAddress.ToLower().Trim();
                mailAddress = mailAddress.Trim(new char[] { '<', '>' });
                CServerCfg cfg     = new CServerCfg();
                XElement   elUsers = cfg.Root.Element("Users");
                foreach (XElement elDomain in elUsers.Nodes())
                {
                    foreach (XElement elUser in elDomain.Nodes())
                    {
                        string emailID = elUser.Name.LocalName + "@" + elDomain.Name.LocalName;

                        if (emailID.ToLower() == mailAddress)
                        {
                            return(true);
                        }
                    }
                }
            }
            catch
            {
            }
            return(false);
        }
Пример #2
0
        /*
         * Called to retreive the email account inbox path on server from the internal user database.
         * This requires the mailAddress in format [email protected] format.
         * If the function returns true, then the mailFolder variable holds the
         * EMAIL inbox path on the server, and also that the account is authenticated
         * and valid for the transaction to proceed.
         * Otherwise, don't proceed with it... It is invalid.
         */
        private bool getInbox(string mailAddress, ref string mailFolder)
        {
            try
            {
                mailAddress = mailAddress.ToLower().Trim();
                mailAddress = mailAddress.Trim(new char[] { '<', '>' });

                CServerCfg cfg     = new CServerCfg();
                XElement   elUsers = cfg.Root.Element("Users");
                foreach (XElement elDomain in elUsers.Nodes())
                {
                    foreach (XElement elUser in elDomain.Nodes())
                    {
                        string emailID = elUser.Name.LocalName + "@" + elDomain.Name.LocalName;
                        string folder  = elUser.Element("folder").Value;

                        if (emailID.ToLower() == mailAddress)
                        {
                            mailFolder = folder;
                            return(true);
                        }
                    }
                }
            }
            catch
            {
            }
            return(false);
        }
Пример #3
0
        /*
         * Called to verify the email account against the internal user database.
         * This requires the userName which is in format [email protected] format,
         * along with the valid password for the acoount.
         * If the function returns true, then the mailFolder variable holds the
         * EMAIL inbox path on the server, and also that the account is authenticated
         * and valid for the transaction to proceed.
         * Otherwise, don't proceed with it... It is invalid.
         */
        private bool VerifyAccount(string userName, string password, ref string mailFolder)
        {
            try
            {
                CServerCfg cfg     = new CServerCfg();
                XElement   elUsers = cfg.Root.Element("Users");
                foreach (XElement elDomain in elUsers.Nodes())
                {
                    foreach (XElement elUser in elDomain.Nodes())
                    {
                        string emailID = elUser.Name.LocalName + "@" + elDomain.Name.LocalName;
                        string pass    = elUser.Element("pass").Value;
                        string folder  = elUser.Element("folder").Value;

                        if (emailID.ToLower() == userName.ToLower() &&
                            pass == password)
                        {
                            mailFolder = folder;
                            return(true);
                        }
                    }
                }
            }
            catch
            {
            }
            return(false);
        }
Пример #4
0
        /*
         * This function is invoked when "Done" button is clicked.
         * Then save the new value of the POP3 and SMTP port numbers, if modified.
         */
        private void btnUpdate_Click(object sender, EventArgs e)
        {
            CServerCfg cfg = null;

            try
            {
                cfg = new CServerCfg();
                cfg.Root.Element("Pop3Port").SetValue(popPort.ToString());
                cfg.Root.Element("SmtpPort").SetValue(smtpPort.ToString());

                MessageBox.Show("Mail Server Configuration updated and saved.",
                                "Mail Server Configuration Updated",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Information);
            }
            catch
            {
                MessageBox.Show("The Savings could not be updated.",
                                "Mail Server Update failed",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Warning);
            }
            finally
            {
                if (cfg != null)
                {
                    cfg.Close();
                }

                this.Dispose();
            }
        }
Пример #5
0
        /*
         * This function is called when the user clicks the "Remove" button
         * which will remove the E-Mail account settings from the Mail configuration
         * file and also removes the user's mail inbox and all the mail content in it.
         */
        private void btnRemove_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("You are about to delete the E-Mail account. This will delete the E-Mail Inbox and the user will loose all the mail content in it.\nAre you sure with the delete option ?",
                                "Confirm Delete ?",
                                MessageBoxButtons.OKCancel,
                                MessageBoxIcon.Question) == DialogResult.Cancel)
            {
                return;
            }

            CServerCfg cfg = null;

            try
            {
                cfg = new CServerCfg();

                XElement      elDomain = cfg.Root.Element("Users").Element(cbDomains.Items[cbDomains.SelectedIndex].ToString());
                XElement      elUser   = elDomain.Element(tbUser.Text);
                DirectoryInfo dinfo    = new DirectoryInfo(Application.StartupPath + @"\" + elUser.Element("folder").Value);
                if (dinfo.Exists)
                {
                    dinfo.Delete(true);
                }

                elUser.Remove();
                cbDomains.Items.RemoveAt(cbDomains.SelectedIndex);
                if (cbDomains.Items.Count > 0)
                {
                    cbDomains.SelectedIndex = 0;
                }

                MessageBox.Show("The E-Mail account was successfully removed.",
                                "E-Mail Account deleted",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Information);
            }
            catch
            {
                MessageBox.Show("The E-Mail account cannot be removed at this time. Please try again later.",
                                "E-Mail cannot be deleted",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Stop);
            }
            finally
            {
                if (cfg != null)
                {
                    cfg.Close();
                }
            }
        }
Пример #6
0
        /*
         * This function is invoked when the user clicks the "Update"
         * button and thereby, updates the E-Mail account settings in the Mail
         * Configuration file.
         */
        private void btnUpdate_Click(object sender, EventArgs e)
        {
            // Update only updates the password
            tbPass.Text = tbPass.Text.Trim();
            if (tbPass.Text == "")
            {
                MessageBox.Show("Empty password string is not allowed. Please enter a non-empty valid password and retry.",
                                "Invalid password",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Stop);
                return;
            }
            if (tbPass.Text != tbPassConfirm.Text)
            {
                MessageBox.Show("The entered password does not match. Please enter the correct password before continuing.",
                                "Wrong password",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Stop);
                return;
            }

            CServerCfg cfg = null;

            try
            {
                cfg = new CServerCfg();
                XElement elDomain = cfg.Root.Element("Users").Element(cbDomains.Items[cbDomains.SelectedIndex].ToString());
                XElement elUser   = elDomain.Element(tbUser.Text);
                elUser.Element("pass").SetValue(tbPass.Text);

                MessageBox.Show("The E-Mail account was successfully updated.",
                                "E-Mail Account update success",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Information);
            }
            catch
            {
                MessageBox.Show("The E-Mail account could not be updated at this time. Please try again later.",
                                "E-Mail Account Update Failed",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Stop);
            }
            finally
            {
                if (cfg != null)
                {
                    cfg.Close();
                }
            }
        }
Пример #7
0
        /*
         * This function is called when the Domain configuration form is loading.
         * Here we will load the XML configuration file, and enumerate through
         * all the domain entries and display it in a list box on the form for
         * the user to choose from.
         */
        private void CfgDomain_Load(object sender, EventArgs e)
        {
            CServerCfg cfg = null;

            try
            {
                cfg = new CServerCfg();
                try
                {
                    XElement el = cfg.Root.Element("Domains");
                    if (el == null)
                    {
                        throw new Exception();
                    }

                    foreach (XElement dmname in el.Nodes())
                    {
                        lbDomains.Items.Add(dmname.Name.LocalName);
                    }

                    if (lbDomains.Items.Count > 0)
                    {
                        lbDomains.SelectedIndex = 0;
                    }
                }
                catch
                {
                    cfg.Root.Add(new XElement("Domains"));
                }
            }
            catch
            {
            }
            finally
            {
                if (cfg != null)
                {
                    cfg.Close();
                }
            }
        }
Пример #8
0
        /*
         * This function is invoked when the user selects a different user account
         * to update or remove.
         */
        private void lbUsers_SelectedIndexChanged(object sender, EventArgs e)
        {
            EnableReadOnly();

            string usermail   = lbUsers.Items[lbUsers.SelectedIndex].ToString();
            string username   = usermail.Substring(0, usermail.IndexOf("@"));
            string userdomain = usermail.Substring(usermail.IndexOf("@") + 1);

            CServerCfg cfg = null;

            try
            {
                cfg = new CServerCfg();
                XElement elDomain = cfg.Root.Element("Users").Element(userdomain);
                if (elDomain == null)
                {
                    throw new Exception();
                }

                XElement elUser = elDomain.Element(username);
                if (elUser == null)
                {
                    throw new Exception();
                }

                tbUser.Text             = elUser.Name.LocalName;
                cbDomains.SelectedIndex = cbDomains.Items.IndexOf(elDomain.Name.LocalName);
                tbPass.Text             = elUser.Element("pass").Value;
                tbPassConfirm.Text      = tbPass.Text;
            }
            catch
            {
            }
            finally
            {
                if (cfg != null)
                {
                    cfg.Close();
                }
            }
        }
Пример #9
0
        /*
         * Add the new domain to the list of list box on the form and also
         * to the mail configuration file.
         */
        private void btnAdd_Click(object sender, EventArgs e)
        {
            tbDomain.Text = tbDomain.Text.Trim();
            if (tbDomain.Text == "")
            {
                return;
            }

            if (lbDomains.Items.IndexOf(tbDomain.Text.ToLower()) >= 0)
            {
                return;
            }

            CServerCfg cfg = null;

            try
            {
                cfg = new CServerCfg();
                cfg.Root.Element("Domains").Add(new XElement(tbDomain.Text.ToLower()));
                lbDomains.SelectedIndex = lbDomains.Items.Add(tbDomain.Text.ToLower());
                MessageBox.Show("The Domain Name has been added to the Domain List.",
                                "Domain Name Added",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Information);
            }
            catch
            {
                MessageBox.Show("The Domain Name cannot be added to the Domain List.",
                                "Domain Name Added Failed",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Error);
            }
            finally
            {
                if (cfg != null)
                {
                    cfg.Close();
                }
            }
        }
Пример #10
0
        /*
         * This function is called when the form is loaded.
         * In this procedure, we would load the initial POP3 and SMTP
         * values from the Mail Configuration file, and display it in
         * their respective edit boxes.
         */
        private void CfgMailServer_Load(object sender, EventArgs e)
        {
            CServerCfg cfg = null;

            try
            {
                cfg = new CServerCfg();

                try
                {
                    popPort = int.Parse(cfg.Root.Element("Pop3Port").Value);
                }
                catch
                {
                    cfg.Root.Add(new XElement("Pop3Port", popPort.ToString()));
                }

                try
                {
                    smtpPort = int.Parse(cfg.Root.Element("SmtpPort").Value);
                }
                catch
                {
                    cfg.Root.Add(new XElement("SmtpPort", smtpPort.ToString()));
                }
            }
            catch
            {
            }
            finally
            {
                if (cfg != null)
                {
                    cfg.Close();
                }
            }

            tbPop.Text  = popPort.ToString();
            tbSmtp.Text = smtpPort.ToString();
        }
Пример #11
0
        /*
         * Remove the selected domain from the domain name list and
         * delete the domain name from Mail server configuration file. It also
         * removes the users and inboxes configured under it.
         */
        private void btnRemove_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("You are about to delete the E-Mail Domain. This will delete all the E-Mail Inbox configured with it and the dependent user will loose all the mail content in their respective Inbox.\nAre you sure with the delete option ?",
                                "Confirm Delete of Domain ?",
                                MessageBoxButtons.OKCancel,
                                MessageBoxIcon.Question) == DialogResult.Cancel)
            {
                return;
            }

            CServerCfg cfg = null;

            try
            {
                cfg = new CServerCfg();
                cfg.Root.Element("Domains").Element(lbDomains.Items[lbDomains.SelectedIndex].ToString()).Remove();
                XElement udomain = cfg.Root.Element("Users").Element(lbDomains.Items[lbDomains.SelectedIndex].ToString());
                if (udomain != null)
                {
                    foreach (XElement users in udomain.Nodes())
                    {
                        try
                        {
                            XElement      ufolder = users.Element("folder");
                            DirectoryInfo dinfo   = new DirectoryInfo(Application.StartupPath + @"\" + ufolder.Value);
                            dinfo.Delete(true);
                        }
                        catch
                        {
                        }
                    }
                    udomain.Remove();
                }

                try
                {
                    lbDomains.Items.RemoveAt(lbDomains.SelectedIndex);
                }
                catch
                {
                }
                finally
                {
                    if (lbDomains.Items.Count > 0)
                    {
                        lbDomains.SelectedIndex = 0;
                    }
                }

                MessageBox.Show("The Domain name was removed successfully from the Domain List.",
                                "Domain Name Removed",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Information);
            }
            catch
            {
                MessageBox.Show("The Domain Name cannot be removed from the Domain Name list.",
                                "Domain Name Removal Failed",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Error);
            }
            finally
            {
                cfg.Close();
            }
        }
Пример #12
0
        /*
         * This is the function solely reponsible for starting the SMTP server.
         * This function creates the SMTP TCP listener and bind to all available
         * network adapters and listens for SMTP clients to connect.
         * For eache client request it accepts, it spawns a new thread to cater to
         * the service of the client.
         */
        private void SmtpThreadFunc()
        {
            TcpListener lis = null;

            try
            {
                CServerLog.LogEntry("INFORMATION", "Smtp server starting...");
                try
                {
                    CServerCfg cfg = new CServerCfg();

                    // Read the SMTP port configuration from the XML based Mail comfiguration file
                    int smtpPort = int.Parse(cfg.Root.Element("SmtpPort").Value);

                    // Start the SMTP TCP listener interface and bind it to
                    // all network interfaces available on the assigned SMTP port
                    lis = new TcpListener(IPAddress.Parse("0.0.0.0"), smtpPort);

                    // Start the listening job
                    lis.Start();
                    CServerLog.LogEntry("SUCCESS", "SMTP server started and waiting for client to connect.");
                }
                catch (Exception ex)
                {
                    // Something wrong just happened
                    // Display it to user and log it
                    MessageBox.Show("Exception starting SMTP server: " + ex.ToString(),
                                    "SMTP Server cannot be started",
                                    MessageBoxButtons.OK,
                                    MessageBoxIcon.Stop);
                    CServerLog.LogEntry("FAILED", "Exception starting SMTP Server: " + ex.ToString());

                    // Rethrow the exception, so we can get out of this mess
                    throw ex;
                }

                // start to listen till eternity or till this thread is aborted
                while (true)
                {
                    // Poll this socket for readable data which will return true
                    // when there is client to be accepted.
                    if (lis.Server.Poll(1000, SelectMode.SelectRead) == true)
                    {
                        CServerLog.LogEntry("INFORMATION", "A SMTP client is waiting for connection.");

                        // Accept the new SMTP client and spawn a new child thread
                        // and let it cater the new client.
                        TcpClient client = lis.AcceptTcpClient();
                        Thread    th     = new Thread(SmtpClient);
                        th.Start(client);
                    }
                    else
                    {
                        // No client is available... So just wait for 100ms and repoll again
                        Thread.Sleep(100);
                    }
                }
            }
            catch
            {
                // Something wrong happened... We don't want to know
            }
            finally
            {
                // So we are done with the listener....
                // Lets stop it and free the network adapters.
                if (lis != null)
                {
                    lis.Stop();
                }
            }
        }
Пример #13
0
        /*
         * This is the function invoked on an independent Thread.
         * This function initializes the POP3 listener TCP Socket and waits for the
         * incoming connection to be made by Mail clients.
         * On accepting a client, the server spawns child threads, each of which is responsible
         * for handling individual client's request.
         */
        private void Pop3ThreadFunc()
        {
            TcpListener lis = null;

            try
            {
                CServerLog.LogEntry("INFORMATION", "Pop3 server starting...");

                try
                {
                    CServerCfg cfg = new CServerCfg();

                    // Read the POP3 listener port from the Mail Sever configuration file.
                    int pop3Port = int.Parse(cfg.Root.Element("Pop3Port").Value);

                    // Create a Tcp Listener to listen to the POP3 port on all available
                    // Network interface.
                    lis = new TcpListener(IPAddress.Parse("0.0.0.0"), pop3Port);

                    // Start the Listener Socket and bind it to the Network interfaces.
                    lis.Start();
                    CServerLog.LogEntry("SUCCESS", "POP3 server started and waiting for client to connect.");
                }
                catch (Exception ex)
                {
                    // Oops!!! I think something went wrong.... What to do now????

                    // Show the Exception to user and Log it.
                    MessageBox.Show("Exception starting POP3 server: " + ex.ToString(),
                                    "POP3 Server cannot be started",
                                    MessageBoxButtons.OK,
                                    MessageBoxIcon.Stop);
                    CServerLog.LogEntry("FAILED", "Exception starting POP3 Server: " + ex.ToString());

                    // Rethrow it... So that we get out of this mess.
                    throw ex;
                }

                // Wait for listeners till eternity or until the Thread is aborted.
                while (true)
                {
                    // Poll the POP3 TCP listener  for any client
                    // If we get true result, then we have a client waiting to be accepted.
                    if (lis.Server.Poll(1000, SelectMode.SelectRead) == true)
                    {
                        // Log the client
                        CServerLog.LogEntry("INFORMATION", "A POP3 client is waiting for connection.");

                        // get the client TCP Client socket interface and spawn a new thread
                        // to service the POP3 client
                        TcpClient client = lis.AcceptTcpClient();
                        Thread    th     = new Thread(Pop3Client);
                        th.Start(client);
                    }
                    else
                    {
                        // Wait for 100ms before we re-poll
                        Thread.Sleep(100);
                    }
                }
            }
            catch
            {
                // Some exception occurred.... lets do nothing here
            }
            finally
            {
                // so now its done
                // let's stop the listener if there is one and that's it.
                if (lis != null)
                {
                    lis.Stop();
                }
            }
        }
Пример #14
0
        /*
         * This function is invoked when the User Account Configuration dialog is invoked.
         * Enumerate the Domain and its Users, and list the registered E-Mail addresses in
         * the list box.
         */
        private void CfgUser_Load(object sender, EventArgs e)
        {
            CServerCfg cfg = null;

            try
            {
                cfg = new CServerCfg();
                XElement elDomain = cfg.Root.Element("Domains");
                if (elDomain != null)
                {
                    foreach (XElement el in elDomain.Nodes())
                    {
                        cbDomains.Items.Add(el.Name.LocalName);
                    }
                    if (cbDomains.Items.Count > 0)
                    {
                        cbDomains.SelectedIndex = 0;
                    }
                    else
                    {
                        // No E-Mail Domains specified
                        MessageBox.Show("The E-Mail server has no E-Mail domains configured. Please configure domain names before adding an User Account.",
                                        "E-Mail Domain not Configured",
                                        MessageBoxButtons.OK,
                                        MessageBoxIcon.Stop);
                        this.Dispose();
                        return;
                    }
                }

                XElement elUsers = cfg.Root.Element("Users");
                if (elUsers != null)
                {
                    foreach (XElement elDomains in elUsers.Nodes())
                    {
                        foreach (XElement elUser in elDomains.Nodes())
                        {
                            lbUsers.Items.Add(elUser.Name.LocalName + "@" + elDomains.Name.LocalName);
                        }
                    }
                    if (lbUsers.Items.Count > 0)
                    {
                        lbUsers.SelectedIndex = 0;
                        EnableReadOnly();
                    }
                    else
                    {
                        EnableWrite();
                    }
                }
                else
                {
                    // add the provision node for Users
                    cfg.Root.Add(new XElement("Users"));
                }
            }
            catch
            {
            }
            finally
            {
                if (cfg != null)
                {
                    cfg.Close();
                }
            }
        }
Пример #15
0
        /*
         * This function is invoked when we want to add the newly created E-Mail
         * user account to the Mail Configuration file and also to the E-Mail
         * list box.
         */
        private void btnAdd_Click(object sender, EventArgs e)
        {
            tbUser.Text = tbUser.Text.Trim();
            tbPass.Text = tbPass.Text.Trim();

            if (tbUser.Text == "" || tbPass.Text == "")
            {
                MessageBox.Show("User Name or Password cannot be blank.\nPlease enter a valid User Name and Password and try again.",
                                "Error in User Name or Password.",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Stop);
                return;
            }

            if (tbPass.Text != tbPassConfirm.Text)
            {
                MessageBox.Show("The entered passwords do not match. Please enter the correct password and try again.",
                                "Wrong Password",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Stop);
                return;
            }


            /*
             * <Users>
             * ... ... ...
             *  <domain.xyz>
             *      <email>
             *          <pass>mypassword</pass>
             *          <folder>myemailfolder</folder>
             *      </email>
             *  <domain.xyz>
             * ... ... ...
             * </Users>
             */

            CServerCfg cfg = null;

            try
            {
                cfg = new CServerCfg();
                XElement el = cfg.Root.Element("Users");
                if (el == null)
                {
                    throw new Exception();
                }

                // Create the new folder Guid for email account
                string userdir = Guid.NewGuid().ToString().ToUpper();


                DirectoryInfo   dirinfo     = new DirectoryInfo(Application.StartupPath);
                DirectoryInfo[] dirinfolist = dirinfo.GetDirectories();
                bool            bFound;
                do
                {
                    bFound = true;
                    foreach (DirectoryInfo dinfo in dirinfolist)
                    {
                        if (dinfo.Name == userdir)
                        {
                            userdir = Guid.NewGuid().ToString().ToUpper();
                            bFound  = false;
                            break;
                        }
                    }
                }while (bFound != true);

                // now create the unique user directory
                dirinfo = new DirectoryInfo(Application.StartupPath + @"\" + userdir);
                dirinfo.Create();

                // now add this settings to configuration
                XElement elUser = new XElement(tbUser.Text);
                elUser.Add(new XElement("pass", tbPass.Text));
                elUser.Add(new XElement("folder", userdir));

                XElement elDomain = el.Element(cbDomains.Text);
                if (elDomain == null)
                {
                    elDomain = new XElement(cbDomains.Text);
                    elDomain.Add(elUser);
                    el.Add(elDomain);
                }
                else
                {
                    elDomain.Add(elUser);
                }

                lbUsers.SelectedIndex = lbUsers.Items.Add(tbUser.Text + "@" + cbDomains.Text);

                MessageBox.Show("E-Mail Account was successfully created and added to the list.",
                                "E-Mail Account created",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Information);
            }
            catch
            {
                MessageBox.Show("E-Mail Account cannot be created at this time. Please try again later.",
                                "E-Mail Account cannot be created",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Stop);
            }
            finally
            {
                if (cfg != null)
                {
                    cfg.Close();
                }
            }
        }