2012-05-15 12 views
8

Sto usando OpenPop.net per provare e analizzare i nostri collegamenti da tutte le email che si trovano in una determinata casella di posta. Ho trovato questo metodo per ottenere tutto il messaggio:OpenPop.net visualizza il testo del messaggio attuale

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; 
     } 
    } 

Ora sto cercando di collegare attraverso ogni messaggio, ma io non riesco a capire come farlo, ho questa finora per il mio tasto:

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); 
    } 

Come potrei scorrere in loop ogni voce in allaEmail in modo che possa visualizzarla in un MessageBox?

risposta

25

Vedo che si utilizza fetchAllEmail example dalla home page di OpenPop. Un esempio simile showing how to get body text è anche nella home page.

Si potrebbe anche voler vedere come le email sono effettivamente strutturate. Un email introduction esiste proprio per questo scopo.

Detto questo, vorrei fare qualcosa di simile al codice qui sotto.

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()); 
} 

Spero che questo possa aiutarti in questo modo. Si noti che esiste anche online documentation per OpenPop.

+1

WOW grazie foens! Era esattamente quello che stavo cercando! :) Checkbox in arrivo – user1213488

+0

html.GetBodyAsText() dà un'eccezione dice che il riferimento all'oggetto non è impostato su un'istanza. ma ottengo il valore utilizzando FindFirstPlainTextVersion() e quindi plainText.GetBodyAsText() qualche idea del perché? – Antony

0

Ecco come ho fatto:

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

Speranza che aiuta.

Problemi correlati