Question

I try to create simple mail client. Now I can receive list of messages from mail box:

        // 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.Write("USER my_login@gmail.com\r\n");
        // sent to server
        sw.Flush();
        sw.Write("PASS my_pass\r\n");
        sw.Flush();

        // this will retrive your first email
        sw.Write("LIST\r\n");
        sw.Flush();

        // close the connection
        sw.WriteLine("QUIT\r\n");
        sw.Flush();

        richTextBox2.Text = reader.ReadToEnd();

        sw.Close();
        reader.Close();
        tcpclient.Close();

I don't uderstand why it is possible to read from stream only after sending command QUIT? If I try to read stream to end or all lines from stream before sending message QUIT my program crashes. Can anyone help me?

Thank you very much!

Was it helpful?

Solution

Your program doesn't crash, it hangs. It hangs because ReadToEnd() waits for an EOF which doesn't get sent by the server until the connection is closed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top