2010-09-24 30 views
5

海蘭,C#,如何通過POP3

檢索來自Gmail服務器的郵件列表中我有一個使用SSL與Gmail SMTP服務器發送郵件(smtp.gmail.com)的應用程序。

現在我想閱讀該帳戶的電子郵件,有沒有人有任何想法如何在C#和ASP.NET編程方式?

在這一點上,我使用此代碼:

TcpClient tcpClient = new TcpClient(); 
tcpClient.Connect("pop.gmail.com", 587); 

NetworkStream netStream = tcpClient.GetStream(); 
System.IO.StreamReader strReader = new System.IO.StreamReader(netStream); 

Label7.Text = strReader.ReadLine() + "<br />"; 
//Label7.Text = "Server connected!"; 

byte[] WriteBuffer = new byte[1024]; 
ASCIIEncoding enc = new System.Text.ASCIIEncoding(); 

WriteBuffer = enc.GetBytes("USER " + TextBox4.Text + "\r\n"); 
netStream.Write(WriteBuffer, 0, WriteBuffer.Length); 
Label7.Text += strReader.ReadLine() + "<br />"; 

WriteBuffer = enc.GetBytes("PASS " + TextBox5.Text + "\r\n"); 
netStream.Write(WriteBuffer, 0, WriteBuffer.Length); 
Label7.Text += strReader.ReadLine() + "<br />"; 

WriteBuffer = enc.GetBytes("LIST\r\n"); 
netStream.Write(WriteBuffer, 0, WriteBuffer.Length); 

String ListMessage; 
while (true) 
{ 
    ListMessage = strReader.ReadLine(); 
    if (ListMessage == ".") 
    { 
     break; 
    } 
    else 
    { 
     Label7.Text += ListMessage + "<br />"; 
     continue; 
    } 
} 

WriteBuffer = enc.GetBytes("QUIT\r\n"); 
netStream.Write(WriteBuffer, 0, WriteBuffer.Length); 
Label7.Text += strReader.ReadLine() + "<br />"; 

當我調試它,它的顯示,它的連接,但在檢索電子郵件沒有反應。

+0

在TcpClient.Connect地址中不應該是pop.gmail.com而不是smtp.gmail.com? – 2010-09-24 09:18:30

+0

啊,是的,我的錯誤,我已編輯到pop.gmail.com,但我仍然有問題... – 2010-09-24 09:24:40

回答

1

open source project(我參與)的POP3功能包含您需要的一切。包括安全通信支持&高級認證。

如果你真的想自己做一個,瀏覽源代碼可能會節省你幾天的開發。

+0

,這是與任何類型的帳戶?就像一個Gmail賬戶? – 2010-09-24 11:58:20

+0

任何POP3帳戶,包括Gmail。 – 2010-09-24 14:02:25

+0

這個項目已經死在Codeplex上了。這些樣本甚至沒有構建。 – Seany84 2011-08-14 12:05:25

0

我使用這個示例庫,出現在代碼項目link text 有一個很好和乾淨的api與pop3一起工作。