2013-08-05 42 views
0

我試圖在Windows窗體中閱讀我的電子郵件。這是它應該在電子郵件收件箱中顯示我的第一條消息。首先我嘗試使用我的雅虎帳戶。代碼工作正常,但沒有顯示我的消息。剛剛得到這個結果:「+ OK你好從popgate-0.8.0.450444 pop113.plus.mail.bf1.yahoo.com」。用Windows窗體閱讀郵件使用C#

這裏是我的代碼:

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 
using System.Configuration; 
using System.Web; 
using System.Web.Security; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Web.UI.WebControls.WebParts; 
using System.Web.UI.HtmlControls; 
using System.IO; 
using System.Net.NetworkInformation; 
using System.Net.Security; 
using System.Net.Sockets; 

namespace emailcheck 
{ 
public partial class Form1 : Form 
{ 
    public Form1() 
    { 
     InitializeComponent(); 
    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     try 
     { 
      // create an instance of TcpClient 

      TcpClient tcpclient = new TcpClient(); 

      // HOST NAME POP SERVER and gmail uses port number 995 for POP 

      tcpclient.Connect("pop.mail.yahoo.com", 995); 

      // This is Secure Stream // opened the connection between client and POP Server 

      System.Net.Security.SslStream sslstream = new SslStream(tcpclient.GetStream()); 

      // authenticate as client 

      sslstream.AuthenticateAsClient("pop.mail.yahoo.com"); 

      //bool flag = sslstream.IsAuthenticated; // check flag 

      // Asssigned the writer to stream 

      System.IO.StreamWriter sw = new StreamWriter(sslstream); 

      // Assigned reader to stream 

      System.IO.StreamReader reader = new StreamReader(sslstream); 

      // refer POP rfc command, there very few around 6-9 command 

      sw.WriteLine("[email protected]"); 

      // sent to server 
      sw.Flush(); sw.WriteLine("password"); 

      sw.Flush(); 

      // RETR 1 will retrive your first email. it will read content of your first email 

      sw.WriteLine("RETR 1"); 

      sw.Flush(); 
      // close the connection 
      sw.WriteLine("Quit "); 
      sw.Flush(); string str = string.Empty; 
      string strTemp = string.Empty; 
      while ((strTemp = reader.ReadLine()) != null) 
      { 
       // find the . character in line 
       if (strTemp == ".") 
       { 
        break; 
       } 
       if (strTemp.IndexOf("-ERR") != -1) 
       { 
        break; 
       } 
       str += strTemp; 
      } 


      richTextBox1.Text = str; 
      // textBox1.Text ="Congratulation.. ....!!! You read your first gmail email "; 
     } 

     catch (Exception ex) 
     { 
      richTextBox1.Text = ex.ToString(); 
     } 
    } 
}} 

誰能告訴我如何顯示的信息?還有如何檢查其他域名郵件,如Gmail,Hotmail等,

回答

1

爲什麼你在從流中獲取電子郵件之前退出? 服務器在RETR命令後立即返回電子郵件的內容。

我跑你的代碼,它會在日誌的經典方案:

sw.WriteLine("USER [email protected]"); // Refer to RFC 1939 

// sent to server 
sw.Flush(); 
strTemp = reader.ReadLine(); 

sw.WriteLine("PASS password"); 

sw.Flush(); 
strTemp = reader.ReadLine(); 

你應該得到確定,現在的IE。 + OK。歡迎。 現在你應該可以彈出消息。

要獲得郵件內容(從您的代碼):

sw.WriteLine("RETR 1"); 

sw.Flush(); 
strTemp = reader.ReadLine(); 
while ((strTemp = reader.ReadLine()) != null) 
{ 
// find the . character in line 
    if (strTemp == ".") 
    { 
     break; 
    } 
    if (strTemp.IndexOf("-ERR") != -1) 
    { 
     break; 
    } 
    str += strTemp; 
} 

完整代碼

try 
     { 
      // create an instance of TcpClient 
      string str = string.Empty; 
      string strTemp = string.Empty; 
      TcpClient tcpclient = new TcpClient(); 

      // HOST NAME POP SERVER and gmail uses port number 995 for POP 

      tcpclient.Connect("pop.gmail.com", 995); 

      // This is Secure Stream // opened the connection between client and POP Server 

      System.Net.Security.SslStream sslstream = new SslStream(tcpclient.GetStream()); 

      // authenticate as client 

      sslstream.AuthenticateAsClient("pop.gmail.com"); 

      //bool flag = sslstream.IsAuthenticated; // check flag 

      // Asssigned the writer to stream 

      System.IO.StreamWriter sw = new StreamWriter(sslstream); 

      // Assigned reader to stream 

      System.IO.StreamReader reader = new StreamReader(sslstream); 
      strTemp = reader.ReadLine(); 
      // refer POP rfc command, there very few around 6-9 command 

      sw.WriteLine("USER [email protected]"); 

      // sent to server 
      sw.Flush(); 
      strTemp = reader.ReadLine(); 

      sw.WriteLine("PASS password"); 

      sw.Flush(); 
      strTemp = reader.ReadLine(); 

      // RETR 1 will retrive your first email. it will read content of your first email 

      sw.WriteLine("RETR 1"); 

      sw.Flush(); 
      strTemp = reader.ReadLine(); 
      while ((strTemp = reader.ReadLine()) != null) 
      { 
       // find the . character in line 
       if (strTemp == ".") 
       { 
        break; 
       } 
       if (strTemp.IndexOf("-ERR") != -1) 
       { 
        break; 
       } 
       str += strTemp; 
      } 
      // close the connection 
      sw.WriteLine("Quit "); 
      sw.Flush(); 
      while ((strTemp = reader.ReadLine()) != null) 
      { 
       // find the . character in line 
       if (strTemp == ".") 
       { 
        break; 
       } 
       if (strTemp.IndexOf("-ERR") != -1) 
       { 
        break; 
       } 
       str += strTemp; 
      } 

      // textBox1.Text ="Congratulation.. ....!!! You read your first gmail email "; 
     } 

     catch (Exception ex) 
     { 

     } 
+0

我已刪除了。但結果是空的。我沒有得到消息。你能幫我嗎 – yasmuru

+0

你確定你的認證成功了嗎? – tray2002

+0

ya。 + OK表示它讀取服務器 – yasmuru