POP3协议。操作STAT和LIST

时间:2014-04-18 15:54:14

标签: c# tcpclient pop3

我尝试实现简单的邮件客户端。现在我可以检索消息:

            // create an instance of TcpClient
            TcpClient tcpclient = new TcpClient();
            // HOST NAME POP SERVER and gmail uses port number 995 for POP 
            tcpclient.Connect("pop.gmail.com", 995);
            // This is Secure Stream // opened the connection between client and POP Server
            System.Net.Security.SslStream sslstream = new SslStream(tcpclient.GetStream());
            // authenticate as client  
            sslstream.AuthenticateAsClient("pop.gmail.com");
            //bool flag = sslstream.IsAuthenticated;   // check flag
            // Asssigned the writer to stream 
            StreamWriter sw = new StreamWriter(sslstream);
            // Assigned reader to stream
            StreamReader reader = new StreamReader(sslstream);
            // refer POP rfc command, there very few around 6-9 command
            sw.WriteLine("USER my_mail@gmail.com");
            // sent to server
            sw.Flush();
            sw.WriteLine("PASS my_pass");
            sw.Flush();
            // this will retrive your first email
            sw.WriteLine("RETR 1");
            sw.Flush();

            string str = string.Empty;
            string strTemp = string.Empty;
            while ((strTemp = reader.ReadLine()) != null)
            {
                // find the . character in line
                if (strTemp == ".")
                {
                    break;
                }
                if (strTemp.IndexOf("-ERR") != -1)
                {
                    break;
                }
                str += strTemp;
            }

            //str = reader.ReadToEnd();
            // close the connection
            sw.WriteLine("QUIT");
            sw.Flush();

            richTextBox2.Text = str;

但是当我尝试实现操作STATLIST时,我的程序崩溃了。我认为阅读流的循环存在问题。对于操作STAT,我尝试阅读"\r\n"strTemp = "\r\n")和操作LIST - ".\r\n"

这是我STAT的代码:

            // create an instance of TcpClient
            TcpClient tcpclient = new TcpClient();
            // HOST NAME POP SERVER and gmail uses port number 995 for POP 
            tcpclient.Connect("pop.gmail.com", 995);
            // This is Secure Stream // opened the connection between client and POP Server
            System.Net.Security.SslStream sslstream = new SslStream(tcpclient.GetStream());
            // authenticate as client  
            sslstream.AuthenticateAsClient("pop.gmail.com");
            //bool flag = sslstream.IsAuthenticated;   // check flag
            // Asssigned the writer to stream 
            StreamWriter sw = new StreamWriter(sslstream);
            // Assigned reader to stream
            StreamReader reader = new StreamReader(sslstream);
            // refer POP rfc command, there very few around 6-9 command
            sw.WriteLine("USER my_mail@gmail.com");
            // sent to server
            sw.Flush();
            sw.WriteLine("PASS my_pass");
            sw.Flush();
            // this will retrive your first email
            sw.WriteLine("STAT");
            sw.Flush();

            string str = string.Empty;
            string strTemp = string.Empty;
            while ((strTemp = reader.ReadLine()) != null)
            {
                // find the . character in line
                if (strTemp == "\r\n")
                {
                    break;
                }
                if (strTemp.IndexOf("-ERR") != -1)
                {
                    break;
                }
                str += strTemp;
            }

            //str = reader.ReadToEnd();
            // close the connection
            sw.WriteLine("QUIT");
            sw.Flush();

            richTextBox2.Text = str;

按下按钮后,我的主窗口没有响应。我的错误在哪里?

谢谢!

1 个答案:

答案 0 :(得分:1)

您的应用很可能挂在ReadLine()上。请注意,StreamReader.ReadLine()不包含\r\n。所以你对\r\n的检查永远不会命中,因此break语句永远不会命中。

您可能只需将其更改为if (strTemp == "")即可。如果没有做到这一点,你就必须在调试器中单步执行。

另请注意,阻止这样的调用在UI线程中不是一个好主意。你真的应该把它卸载给后台工作者。