我試圖通過POP3協議從我的live.com帳戶讀取郵件。僅使用TcpClient和StreamWriter/StreamReader讀取POP3服務器
我已經找到了服務器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);
會拋出一個「A套接字操作試圖無法訪問網絡65.55.172.253:995」的錯誤。
所以,我從我機器上的端口8000發送到端口995,hotmail pop3端口。 而我什麼也沒有,而且我沒有想法。
第二塊:問題顯然是我沒有寫入quit命令。
驗證碼:
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);
}
這不就是一個無限循環嗎?我懷疑服務器會回答你用一個點發送的所有命令。它可能會說多一點。 – svinto
該點是POP3標準方式來結束對話。我在問我的郵件的完整列表。它會在完成後發送一個點來表明這一事實。這就是爲什麼我一直聽,直到收到。 – KdgDev