我正在尝试通过 POP3 协议(protocol)从我的 live.com 帐户读取邮件。
我发现服务器是 pop3.live.com,端口是 995。
我不打算使用预制库,我正在使用 NetworkStream 和 StreamReader/StreamWriter 来完成这项工作。我需要弄清楚这一点。因此,此处给出的任何答案:Reading Email using Pop3 in C#没有用。
它是一个更大程序的一部分,但我做了一个小测试以查看它是否有效。不管怎样,我什么也得不到。这是我正在使用的代码,我认为它应该是正确的。
编辑:这段代码是旧的,请引用已解决的第二 block 问题。
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);
抛出一个“A socket operation was attempted to an unreachable network 65.55.172.253:995”错误。
因此,我从我机器上的端口 8000 发送到端口 995,即 hotmail pop3 端口。 我什么也没得到,而且我没有想法。
第二 block :问题显然是我没有写退出命令。
代码:
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);
}
最佳答案
如果您使用 Wireshark 查看网络流量会怎样?它发送任何东西吗?
编辑:我也无法通过该端口的 telnet 连接到 pop3.live.com。您曾经设法通过 pop3 电子邮件客户端成功连接吗?
关于c# - 仅使用 TcpClient 和 StreamWriter/StreamReader 读取 POP3 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2440151/