title: Reading a POP3 server with only TcpClient and StreamWriter/StreamReader
body: <p>I'm trying to read mails from my live.com account, via the POP3 protocol.</p>
  
  <p>I've found the the server is pop3.live.com and the port if 995.</p>
  
  <p>I'm not planning on using a pre-made library, I'm using NetworkStream and StreamReader/StreamWriter for the job. I need to figure this out. So, any of the answers given here: <a href="https://stackoverflow.com/questions/44383/reading-email-using-pop3-in-c">Reading Email using Pop3 in C#</a> are not usefull.</p>
  
  <p>It's part of a larger program, but I made a small test to see if it works. Eitherway, i'm not getting anything. Here's the code I'm using, which I think should be correct.</p>
  
  <p>EDIT: this code is old, please refer to the second block problem solved.</p>
  
  <pre><code>public Program() {
      string temp = "";
      using(TcpClient tc = new TcpClient(new IPEndPoint(IPAddress.Parse("127.0.0.1"),8000))) {
          tc.Connect("pop3.live.com",995);
          using(NetworkStream nws = tc.GetStream()) {
              using(StreamReader sr = new StreamReader(nws)) {
                  using(StreamWriter sw = new StreamWriter(nws)) {
                      sw.WriteLine("USER " + user);
                      sw.Flush();
                      sw.WriteLine("PASS " + pass);
                      sw.Flush();
                      sw.WriteLine("LIST");
                      sw.Flush();
                      while(temp != ".") {
                          temp += sr.ReadLine();
                      }
                  }
              }
          }
      }
      Console.WriteLine(temp);
  }
  </code></pre>
  
  <p>Visual Studio debugger constantly falls over <code>tc.Connect("pop3.live.com",995);</code> Which throws an "A socket operation was attempted to an unreachable network 65.55.172.253:995" error.</p>
  
  <p>So, I'm sending from port 8000 on my machine to port 995, the hotmail pop3 port.
  And I'm getting nothing, and I'm out of ideas.</p>
  
  <hr>
  
  <p>Second block: Problem was apparently that I didn't write the quit command.</p>
  
  <p>The Code:</p>
  
  <pre><code>public Program() {
      string str = string.Empty;
      string strTemp = string.Empty;
      using(TcpClient tc = new TcpClient()) {
          tc.Connect("pop3.live.com",995);
          using(SslStream sl = new SslStream(tc.GetStream())) {
              sl.AuthenticateAsClient("pop3.live.com"); 
              using(StreamReader sr = new StreamReader(sl)) {
                  using(StreamWriter sw = new StreamWriter(sl)) {
                      sw.WriteLine("USER " + user);
                      sw.Flush();
                      sw.WriteLine("PASS " + pass);
                      sw.Flush();
                      sw.WriteLine("LIST");
                      sw.Flush();
                      sw.WriteLine("QUIT ");
                      sw.Flush();
  
                      while((strTemp = sr.ReadLine()) != null) {
                          if(strTemp == "." || strTemp.IndexOf("-ERR") != -1) {
                              break;
                          }
                          str += strTemp;
                      }
                  }
              }
          }
      }
      Console.WriteLine(str);
  }
  </code></pre>