使用 Gmail 阅读邮件

发布于 2024-11-29 17:37:57 字数 1750 浏览 1 评论 0原文

我正在阅读 Joseph 和 Ben Albahari 撰写的《C# 4.0 in a Nutshell》,并在网络章节中发现了这段使用 POP3 读取邮件的代码。众所周知,POP3 具有明确的通信方式。当我使用本章中的代码时,很明显它应该可以工作,但事实并非如此。这是代码:

private static string ReadLine(Stream stream)
        {
            List<byte> list = new List<byte>();
            while (true)
            {
                int b = stream.ReadByte();
                if (b == 10 || b < 0) break;

                if (b != 13) list.Add( (byte)b);
            }
            return Encoding.UTF8.GetString(list.ToArray());
        }

        private static void SendCommand(Stream stream, string line)
        {
            byte[] byteArr = Encoding.UTF8.GetBytes(line + "\r\n");
            stream.Write(byteArr, 0, byteArr.Length);
            string response = ReadLine(stream);
            if (!response.StartsWith("+OK"))
                throw new Exception("Pop exception: " + response);
        }

        static void Main(string[] args)
        {

            using (TcpClient client = new TcpClient("pop.gmail.com", 995))
            {

                using (NetworkStream stream = client.GetStream())
                {
                    ReadLine(stream);
                }
            }

该代码不完整,因为它不下载邮件。我只是想看看我们从 Gmail 收到的第一个回复是什么。但不幸的是,程序只是卡在了 ReadLine 方法中的 ReadByte 处。我想当我第一次连接到 gmail 时应该得到这一行:

+OK Hello there.

但我的程序只是挂起。根据此页面:

http://mail.google.com/support/ bin/answer.py?answer=13287

您必须连接到 pop.gmail.com,这正是我所做的。有人能告诉我缺少什么吗?

注意:不要向我发送任何第三方项目来执行此类操作。我知道使用它们非常容易。但我只是想看看幕后发生了什么。如果您发现我的程序本身存在错误,那对我来说会更好。

谢谢。

I was reading C# 4.0 in a Nutshell by Joseph and Ben Albahari and came across this code in Networking chapter which reads the mails using POP3. POP3 has a defined communication as we all know. When I use the code in the chapter it looks obvious that it should work but it doesn't. This is the code:

private static string ReadLine(Stream stream)
        {
            List<byte> list = new List<byte>();
            while (true)
            {
                int b = stream.ReadByte();
                if (b == 10 || b < 0) break;

                if (b != 13) list.Add( (byte)b);
            }
            return Encoding.UTF8.GetString(list.ToArray());
        }

        private static void SendCommand(Stream stream, string line)
        {
            byte[] byteArr = Encoding.UTF8.GetBytes(line + "\r\n");
            stream.Write(byteArr, 0, byteArr.Length);
            string response = ReadLine(stream);
            if (!response.StartsWith("+OK"))
                throw new Exception("Pop exception: " + response);
        }

        static void Main(string[] args)
        {

            using (TcpClient client = new TcpClient("pop.gmail.com", 995))
            {

                using (NetworkStream stream = client.GetStream())
                {
                    ReadLine(stream);
                }
            }

The code is incomplete in the sense that it doesn't download mails. I was just trying to see what is the 1st reponse that we get from Gmail. But unfortunately, the program just stucks at ReadByte in ReadLine method. I think I should get this line when I first connect to the gmail:

+OK Hello there.

But my program just hangs. As per this page:

http://mail.google.com/support/bin/answer.py?answer=13287

you have to connect on pop.gmail.com which is exactly what I have done. Can anybody tell me what is missing?

Note: Do not send me any 3rd party projects to do this sort of this. I know it is very easy using them. But I am just trying to see what happens under the hoods. It would be better for me if you find the bug present in my program itself.

Thanks.

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

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

发布评论

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

评论(2

獨角戲 2024-12-06 17:37:57

使用 SslStream 而不是 NetworkStream,因为 gmail 需要 ssl

TcpClient objTcpClient = new TcpClient();
//Connecting to the pop3 server at the specified port 
objTcpClient.Connect(pop3Server, pop3PortNumber);

//ValidateCertificate is a delegate
SslStream netStream = new SslStream(objTcpClient.GetStream(), false, ValidateCertificate); //Authenticates the client on the server
netStream.AuthenticateAsClient(pop3Server);
//Stream Reader to read response stream 
StreamReader netStreamReader = new StreamReader(netStream, Encoding.UTF8, true);

Use SslStream instead of NetworkStream as gmail requires ssl

TcpClient objTcpClient = new TcpClient();
//Connecting to the pop3 server at the specified port 
objTcpClient.Connect(pop3Server, pop3PortNumber);

//ValidateCertificate is a delegate
SslStream netStream = new SslStream(objTcpClient.GetStream(), false, ValidateCertificate); //Authenticates the client on the server
netStream.AuthenticateAsClient(pop3Server);
//Stream Reader to read response stream 
StreamReader netStreamReader = new StreamReader(netStream, Encoding.UTF8, true);
旧时模样 2024-12-06 17:37:57

Gmail 要求您使用 SSL。

995 端口是基于 SSL 的 POP3,请考虑使用 SslStream

Gmail requires you to use SSL.

995 port is POP3 over SSL, consider using SslStream.

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