Parameter list problem

advertisements

How do I check for null in a array list and remove them? It keeps giving me a error of "ArgumentOutOfRange was handled" or if I change the SMTPException to just Exception it says "Index out of range". Looking at the debugger and taking a look specifically at mails.Count it gives me Count = 4. I originally gave it 3 emails to check. The last Array is a "". I believe this is causing the problem.

private void button1_Click(object sender, EventArgs e)
    {
        try
        {
            if (textBox1.Text == "")
            {
                textBox3.Text += "[-] Listbox is Empty!!!!\r\n";
                return;
            }
            // textBox1.Text.Split(new string[] { "\n" }, StringSplitOptions.RemoveEmptyEntries);
            // Grabs Emails supplied in textbox1 amd then seperates array using '\n'
            ArrayList mails = new ArrayList(textBox1.Text.Split('\n')); 

            // Note: For thought
            // IEnumerable<string> myResults = mails.Split('\n').Where<string>(s => !string.IsNullOrEmpty(s));
            for (int i = 0; i < mails.Count; i++)
            {
                // Seperates user & pass to be passed for authentification.
                ArrayList mailInfo = new ArrayList(mails[i].ToString().Split(':'));
                textBox3.Text += "[+] Checking email format for" + "\r\n" + "[/] " +  mails[i] + "\r\n";
                // Attach domain name if not attached.
                if (!mailInfo[0].ToString().EndsWith("@gmail.com")) mailInfo[0] = mailInfo[0] + "@gmail.com"; 

                // Debug:  Check point
                //
                // Error message:
                // Index was out of range. Must be non-negative and less than the size of the collection. Parameter name index.
                // mails.Count = 4 when feeding only 3 emails.
                //
                // Function:  Check mail & Password
                // error: index out of range
                // MessageBox.Show(mailInfo[0].ToString() + "\n\n" + mailInfo[1].ToString());
                if (mails[i] == "") throw new ArgumentOutOfRangeException("No Mail", "No more mail to check.");
                if (checkAccount(mailInfo[0].ToString(), mailInfo[1].ToString()))
                {
                    textBox3.Text += "[+] Connection Successful! Checking mail.: mailInfo[0].ToString()\r\n";
                }
            }
        }

What am I doing wrong??? Is the null my problem and if so how do I remove it or am I missing something?


Try populating the array like this:

ArrayList mails = new ArrayList(textBox1.Text.Trim().Split('\n'));

That should remove any trailing white space and remove that last item from getting into the array.