Anu
Anu

Reputation: 127

StreamReader.ReadLine() always fails when used with POP3

I always fail to read from the stream in the following block of my code.

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

Sample code is given below.

// 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());

I want to enable SSL also. Please help me!!!

Upvotes: 0

Views: 625

Answers (1)

Will
Will

Reputation: 928

        var client = new TcpClient(serverName, port);

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

Everything looks good. Does the read just timeout? Are you receiving +OK messages during log in? I would try doing a buffer read to see if you are getting anything returned. Also try a different command.

I've never used ReadLine() for network streams. I've always used BeginRead.

Stream.BeginRead, then checked for the carriage return line feed.

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

Upvotes: 1

Related Questions