Reading a POP3 server with only TcpClient and Stre

2019-07-24 15:36发布

问题:

I'm trying to read mails from my live.com account, via the POP3 protocol.

I've found the the server is pop3.live.com and the port if 995.

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: Reading Email using Pop3 in C# are not usefull.

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.

EDIT: this code is old, please refer to the second block problem solved.

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);
}

Visual Studio debugger constantly falls over tc.Connect("pop3.live.com",995); Which throws an "A socket operation was attempted to an unreachable network 65.55.172.253:995" error.

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.


Second block: Problem was apparently that I didn't write the quit command.

The 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);
}

回答1:

What happens if you view the Network Traffic using Wireshark? Is it sending anything at all?

Edit: I can't connect via telnet to pop3.live.com at that port either. Have you managed to successfully connect via a pop3 email client ever?



标签: c# pop3