2012-07-11 71 views
0

我正在致力於一個電子郵件客戶端,它將連接到Gmail郵箱並檢索特定的電子郵件。IMAP響應限制的大小

現在我可以連接到我的郵箱,並且可以檢索部分電子郵件,而不是所有電子郵件,無論我的緩衝區有多大,我仍然只從我的電子郵件中獲得1400個字符,然後爲郵件正文的其餘部分填充空字符。

你可以找到一個截屏的電子郵件正文在這個環節提前

http://www.elzouhery.com/Mail%20Snapshot.png

感謝

編輯

看到完整的代碼

static void Main(string[] args) 
     { 
      TcpIMAP imap = ConnectToEmail(); 
      Console.WriteLine("Total Messages " + imap.MailCount()); 
      Console.WriteLine("Total Unread Messages " + imap.MailUnreadCount()); 
      Console.WriteLine("******************************************************"); 
      imap.SelectInbox(); 

      StreamWriter writer = null; 
      int mailCount = imap.MailCount(); 
      var mailSize = string.Empty; 
      var content = string.Empty; 
      var subject = string.Empty; 


      for (int i = 1; i < mailCount; i++) 
      { 
       try 
       { 
        writer = new StreamWriter(@"c:\Mails\" + i + ".txt", true); 
        content = imap.GetMessage(i).ToString(); 
        writer.Write(content); 
        writer.Close(); 
       } 
       catch(Exception ex) 
       { 
        writer.Write(content); 
        Console.Write(ex.Message); 
        writer.Close(); 
       } 
      } 
     } 

     private static TcpIMAP ConnectToEmail() 
     { 
      string host = "imap.gmail.com"; 
      string username = "************"; 
      string password = "************"; 

      TcpIMAP imap = new TcpIMAP(); 
      imap.Connect(host, 993); 
      imap.AuthenticateUser(username, password); 
      return imap; 
     } 

     public static string GetMailSubject(string Header) 
     { 
      var headerLines = Header.Split(Environment.NewLine.ToCharArray()); 
      foreach (var line in headerLines) 
      { 
       if (line.IndexOf("Subject") > -1) 
       { 
        return line.Replace("Subject: ", ""); 
       } 
      } 
      return ""; 
     } 
/***************************************************/ 
class TcpIMAP 
{ 
    private TcpClient _imapClient; 
    private Stream _imapNs; 
    private StreamWriter _imapSw; 
    private StreamReader _imapSr; 

    public TcpIMAP() 
    { 

    } 

    public TcpIMAP(string hostname, int port) 
    { 
     InitializeConnection(hostname, port); 
    } 

    public void Connect(string hostname, int port) 
    { 
     InitializeConnection(hostname, port); 
    } 

    private void InitializeConnection(string hostname, int port) 
    { 
     try 
     { 
      _imapClient = new TcpClient(hostname, port); 
      System.Net.Security.SslStream sslstream = new System.Net.Security.SslStream(_imapClient.GetStream()); 
      sslstream.AuthenticateAsClient("imap.gmail.com"); 
      _imapNs = sslstream; 
      _imapSw = new StreamWriter(_imapNs); 
      _imapSr = new StreamReader(_imapNs); 

      Console.WriteLine("*** Connected ***"); 
      Response(); 
     } 
     catch (SocketException ex) 
     { 
      Console.WriteLine(ex.Message); 
     } 
    } 


    public void AuthenticateUser(string username, string password) 
    { 
     _imapSw.WriteLine("$ LOGIN " + username + " " + password); 
     _imapSw.Flush(); 
     Response(); 
    } 


    public int MailCount() 
    { 
     _imapSw.WriteLine("$ STATUS INBOX (messages)"); 
     _imapSw.Flush(); 

     string res = Response(); 
     Match m = Regex.Match(res, "[0-9]*[0-9]"); 
     return Convert.ToInt32(m.ToString()); 
    } 

    public int MailUnreadCount() 
    { 
     _imapSw.WriteLine("$ STATUS INBOX (unseen)"); 
     _imapSw.Flush(); 

     string res = Response(); 
     Match m = Regex.Match(res, "[0-9]*[0-9]"); 
     return Convert.ToInt32(m.ToString()); 
    } 


    public string SelectInbox() 
    { 
     _imapSw.WriteLine("$ SELECT INBOX"); 
     _imapSw.Flush(); 
     return Response(); 
    } 


    public object GetMessageHeaders(int index) 
    { 
     _imapSw.WriteLine("$ FETCH " + index + " (body[header.fields (from subject date)])"); 
     _imapSw.Flush(); 

     return Response(); 
    } 

    public object GetMessage(int index) 
    { 
     _imapSw.WriteLine("$ FETCH " + index + " BODY.PEEK[]"); 
     _imapSw.Flush(); 

     return Response(); 
    } 
    private string Response() 
    { 
     byte[] data = new byte[_imapClient.ReceiveBufferSize]; 
     int ret = _imapNs.Read(data, 0, data.Length); 
     string output = Encoding.ASCII.GetString(data).TrimEnd().Replace("\0", ""); 
     return output; 
    } 



    public void Disconnect() 
    { 
     _imapSw.WriteLine("$ LOGOUT"); 
     _imapSw.Flush(); 
     _imapClient.Close(); 
    } 

    public string SendCommand(string command) 
    { 
     _imapSw.WriteLine("$ " + command); 
     _imapSw.Flush(); 
     return Response(); 
    } 
下方
+0

郵政編碼請。聽起來就像你只收到郵件的第一個數據包。 – Joe 2012-07-11 16:23:39

+0

以上代碼 – 2012-07-12 09:30:35

+0

什麼是_imapClient,什麼是設置ReceiveBufferSize?我猜你必須根據其他事件/觸發反覆調用。 – Joe 2012-07-12 11:08:24

回答

0

看起來你正在使用的代碼從這裏,或類似:

http://www.codeproject.com/Articles/29594/How-to-Access-Emails-Using-the-IMAP-Protocol

書面該代碼是錯誤的,並不會爲較大的郵件工作。 Response()調用需要循環調用.Read(),追加結果直到方法返回0(表示沒有更多可用數據。)查看NetworkStream.Read的文檔。

此外,使用IMAP庫會更好(請參閱Accessing Imap in C#)。

+0

是的,我用你上面提到的代碼,我試圖循環讀取加載整個消息體,但能夠讀取我必須發送一個命令到服務器,例如我不得不在一個循環中發送STATE命令,直到我看到OK成功,這個消息已經完成,但它是越野車,所以我認爲有可能在1次調用中獲得整個消息體。 – 2012-07-12 14:06:42

+0

此外,您發送的鏈接不工作替代鏈接http://hellowebapps.com/products/imapx/ – 2012-07-12 14:09:42

0

你只需改變你的接收緩衝區大小