2012-05-15 69 views
8

Tôi đang sử dụng OpenPop.net để thử và phân tích các liên kết của chúng tôi từ tất cả các email trong một hộp thư đến. Tôi thấy phương pháp này để có được tất cả các thông điệp:OpenPop.net nhận được tin nhắn văn bản thực tế

public static List<OpenPop.Mime.Message> FetchAllMessages(string hostname, int port, bool useSsl, string username, string password) 
    { 
     // The client disconnects from the server when being disposed 
     using (Pop3Client client = new Pop3Client()) 
     { 
      // Connect to the server 
      client.Connect(hostname, port, useSsl); 

      // Authenticate ourselves towards the server 
      client.Authenticate(username, password); 

      // Get the number of messages in the inbox 
      int messageCount = client.GetMessageCount(); 

      // We want to download all messages 
      List<OpenPop.Mime.Message> allMessages = new List<OpenPop.Mime.Message>(messageCount); 

      // Messages are numbered in the interval: [1, messageCount] 
      // Ergo: message numbers are 1-based. 
      // Most servers give the latest message the highest number 
      for (int i = messageCount; i > 0; i--) 
      { 
       allMessages.Add(client.GetMessage(i));      
      } 

      client.Disconnect(); 

      // Now return the fetched messages 
      return allMessages; 
     } 
    } 

Bây giờ tôi đang cố gắng để lặp qua mỗi tin nhắn nhưng tôi dường như không thể tìm ra cách để làm điều đó, tôi có điều này cho đến nay cho nút của tôi:

private void button7_Click(object sender, EventArgs e) 
    { 

     List<OpenPop.Mime.Message> allaEmail = FetchAllMessages("pop3.live.com", 995, true, "[email protected]", "xxxxx"); 

     var message = string.Join(",", allaEmail); 
     MessageBox.Show(message); 
    } 

Làm cách nào tôi lặp qua từng mục trong allaEmail để tôi có thể hiển thị nó trong một MessageBox?

Trả lời

25

Tôi có thể thấy rằng bạn sử dụng fetchAllEmail example từ trang chủ OpenPop. Một ví dụ tương tự showing how to get body text cũng có trên trang chủ.

Bạn cũng có thể muốn xem cách email thực sự được cấu trúc. A email introduction chỉ tồn tại cho mục đích này.

Có nói rằng, tôi sẽ làm điều gì đó tương tự như mã bên dưới.

private void button7_Click(object sender, EventArgs e) 
{ 
    List<OpenPop.Mime.Message> allaEmail = FetchAllMessages(...); 

    StringBuilder builder = new StringBuilder(); 
    foreach(OpenPop.Mime.Message message in allaEmail) 
    { 
     OpenPop.Mime.MessagePart plainText = message.FindFirstPlainTextVersion(); 
     if(plainText != null) 
     { 
      // We found some plaintext! 
      builder.Append(plainText.GetBodyAsText()); 
     } else 
     { 
      // Might include a part holding html instead 
      OpenPop.Mime.MessagePart html = message.FindFirstHtmlVersion(); 
      if(html != null) 
      { 
       // We found some html! 
       builder.Append(html.GetBodyAsText()); 
      } 
     } 
    } 
    MessageBox.Show(builder.ToString()); 
} 

Tôi hy vọng điều này có thể giúp bạn trên đường đi. Lưu ý rằng cũng có online documentation cho OpenPop.

+1

WOW cảm ơn bạn! Đó là chính xác những gì tôi đã sau! :) Checkbox comming – user1213488

+0

html.GetBodyAsText() đưa ra một ngoại lệ cho biết đối tượng không được thiết lập cho một cá thể. nhưng tôi nhận được giá trị bằng cách sử dụng FindFirstPlainTextVersion() và sau đó plainText.GetBodyAsText() bất kỳ ý tưởng tại sao? – Antony

0

Đây là cách tôi đã làm nó:

string Body = msgList[0].MessagePart.MessageParts[0].GetBodyAsText(); 
      foreach(string d in Body.Split('\n')){ 
       Console.WriteLine(d);      
      } 

Hy vọng nó giúp.

Các vấn đề liên quan