Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# POP3协议。操作统计和列表_C#_Tcpclient_Pop3 - Fatal编程技术网

C# POP3协议。操作统计和列表

C# POP3协议。操作统计和列表,c#,tcpclient,pop3,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)

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

            // 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;
但是当我试图实现操作
STAT
LIST
时,我的程序崩溃了。我认为在阅读流的循环中有一个问题。对于操作
STAT
,我尝试读取,直到
“\r\n”
strTemp=“\r\n”
)为止,对于操作
列表
-
”\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;
按下按钮后,我的主窗口没有响应。我的错在哪里


谢谢大家!

您的应用程序很可能挂在
ReadLine()
上。请注意,其中不包括
\r\n
。因此,您对
\r\n
的检查将永远不会命中,因此break语句将永远不会命中

如果(strTemp==“”),您可能只需要将其更改为
。如果这不起作用,则必须在调试器中逐步执行


还要注意,在UI线程中阻止这样的调用不是一个好主意。你真的应该把它交给后台工作人员。

你忽略现有库的任何有效原因,例如?什么是“崩溃”异常(因为这是CLR,所以没有GPF,是吗)?你添加了什么代码导致它?你知道,这里的心灵感应者不多。@ivan_pozdeev,我编辑了我的问题。这是我实验室的任务,因此我无法在项目中导入库。我是按照规范做的,不知道为什么不起作用。有什么想法吗?Jon B,没有,没用。我也尝试了
if(strTemp==null)
。我也不知道为什么我不能清除stream
reader.DiscardBufferedData()发送我的登录名和密码后