Beispiel #1
0
        private void ReadMailInfo()
        {
            //读取服务器回送的连接信息
            ResponseData data = new ResponseData();

            GetResponse(data);
            if (CheckResponse(data.responseString) == false)
            {
                return;
            }
            //向服务器发送用户名,请求确认
            SendToServer("USER " + textBoxUser.Text + "\r\n");
            GetResponse(data);
            if (CheckResponse(data.responseString) == false)
            {
                return;
            }
            //向服务器发送密码,请求确认
            SendToServer("PASS " + textBoxPassword.Text + "\r\n");
            GetResponse(data);
            if (CheckResponse(data.responseString) == false)
            {
                return;
            }
            //向服务器发送LIST命令,请求获取邮件总数和总字节数
            SendToServer("LIST \r\n");
            GetResponse(data);
            if (CheckResponse(data.responseString) == false)
            {
                return;
            }
            string[] splitString = data.responseString.Split(new string[] { "\r\n" }, StringSplitOptions.None);
            //从字符串中取子串获取邮件总数
            int count;

            string [] strCount = splitString[0].Split(' ');
            if (strCount.Length == 3) //返回形如 +ok 1 1521 这种格式
            {
                count = int.Parse(strCount[1]);
            }
            else //返回形如 +OK
            {
                count = splitString.Length - 2;
            }
            if (splitString.Length < count)
            {
                return;
            }
            //判断邮箱中是否有邮件
            if (count > 0)
            {
                ClearListBoxItem(listBoxOperation);
                SetGroupBoxText(groupBoxOperation, "信箱中共有 " + count + " 封邮件");
                string str;
                //向邮件列表框中添加邮件
                for (int i = 1; i <= count; i++)
                {
                    if (splitString[i] == "")
                    {
                        continue;
                    }
                    string[] s = splitString[i].Split(' ');
                    if (s.Length == 2)
                    {
                        str = string.Format("第{0}封:{1}字节", s[0], s[1]);
                    }
                    else
                    {
                        str = string.Format("第{0}封", s[0]);
                    }
                    AddListBoxItem(listBoxOperation, str);
                }
            }
            else
            {
                SetGroupBoxText(groupBoxOperation, "信箱中没有邮件");
            }
            SetButtonState(buttonConnect, false);
            SetButtonState(buttonDisconnect, true);
        }
Beispiel #2
0
 private void GetResponse(ResponseData data)
 {
     networkStream.BeginRead(data.bytes, 0, data.bytes.Length, new AsyncCallback(ReadCallBack), data);
     waitHandle.WaitOne();
 }