仅使用 TcpClient 和 StreamWriter/StreamReader 读取 POP3 服务器

发布于 2024-08-25 09:48:50 字数 2543 浏览 2 评论 0原文

我正在尝试通过 POP3 协议从我的 live.com 帐户读取邮件。

我发现服务器是 pop3.live.com,端口是 995。

我不打算使用预制库,我正在使用 NetworkStream 和 StreamReader/StreamWriter 来完成这项工作。我需要弄清楚这一点。因此,此处给出的任何答案:Reading Email using Pop3 in C# 是没用。

它是一个更大程序的一部分,但我做了一个小测试来看看它是否有效。无论如何,我什么也得不到。这是我正在使用的代码,我认为应该是正确的。

编辑:此代码已旧,请参阅已解决的第二块问题。

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 调试器不断失败 tc.Connect("pop3.live.com",995); 这会引发“尝试对无法访问的网络 65.55.172.253:995 进行套接字操作”错误。

因此,我从计算机上的端口 8000 发送到端口 995(hotmail pop3 端口)。 我什么也没得到,我也没有想法。


第二块:问题显然是我没有编写退出命令。

代码:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

七禾 2024-09-01 09:48:50

如果使用 Wireshark 查看网络流量会发生什么?它到底有没有发送任何东西?

编辑:我也无法通过 telnet 连接到该端口的 pop3.live.com。您是否曾成功通过 pop3 电子邮件客户端进行连接?

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文