使用 Gmail 阅读邮件
我正在阅读 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用
SslStream
而不是NetworkStream
,因为 gmail 需要 sslUse
SslStream
instead ofNetworkStream
as gmail requires sslGmail 要求您使用 SSL。
995 端口是基于 SSL 的 POP3,请考虑使用 SslStream。
Gmail requires you to use SSL.
995 port is POP3 over SSL, consider using SslStream.