与POP3一起使用时,StreamReader.ReadLine()始终失败

时间:2011-04-29 14:38:57

标签: c# sockets ssl

我总是无法在下面的代码块中读取流。

Data = "STAT"+CRLF;             
            szData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray());
            NetStrm.Write(szData,0,szData.Length);
            Status.Items.Add(RdStrm.ReadLine());

示例代码如下。

// create server POP3 with port 995
        Server = new TcpClient(POPServ.Text,995);                               
        Status.Items.Clear();

        try
        {
            // initialization
            NetStrm = Server.GetStream();
            RdStrm= new StreamReader(Server.GetStream());
            Status.Items.Add(RdStrm.ReadLine());

            // Login Process
            Data = "USER "+ User.Text+CRLF;             
            szData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray());
            NetStrm.Write(szData,0,szData.Length);
            Status.Items.Add(RdStrm.ReadLine());

            Data = "PASS "+ Passw.Text+CRLF;                
            szData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray());
            NetStrm.Write(szData,0,szData.Length);
            Status.Items.Add(RdStrm.ReadLine());

            // Send STAT command to get information ie: number of mail and size
            Data = "STAT"+CRLF;             
            szData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray());
            NetStrm.Write(szData,0,szData.Length);
            Status.Items.Add(RdStrm.ReadLine());

我也想启用SSL。请帮我!!!

1 个答案:

答案 0 :(得分:1)

        var client = new TcpClient(serverName, port);

        var sslStream = new System.Net.Security.SslStream(m_client .GetStream());
        sslStream .AuthenticateAsClient(serverName);

一切看起来都不错。读取是否超时?您是否在登录期间收到+ OK消息?我会尝试做一个缓冲区读取,看看你是否得到任何返回。也尝试不同的命令。

我从未将ReadLine()用于网络流。我一直使用BeginRead。

Stream.BeginRead,然后检查回车换行符。

http://msdn.microsoft.com/en-us/library/system.io.stream.beginread.aspx