12

I am using OpenPop.net to try and parse our links from all the emails that are in a given inbox. I found this method to get all the message:

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

Now I am trying to loop through each message but I cannot seem to figure out how to do it, I have this so far for my button:

    private void button7_Click(object sender, EventArgs e)
    {

        List<OpenPop.Mime.Message> allaEmail = FetchAllMessages("pop3.live.com", 995, true, "xxxxx@hotmail.com", "xxxxx");

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

How would i loop through each entry in allaEmail so that i can display it in a MessageBox?

laylarenee
  • 3,276
  • 7
  • 32
  • 40
user1213488
  • 473
  • 2
  • 7
  • 19

3 Answers3

34

I can see that you use the fetchAllEmail example from the OpenPop homepage. A similar example showing how to get body text is also on the homepage.

You might also want to look at how emails are actually structured. A email introduction exists for just this purpose.

Having that said, I would do something similar to the code below.

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

I hope this can help you on the way. Notice that there is also online documentation for OpenPop.

foens
  • 8,642
  • 2
  • 36
  • 48
  • 1
    WOW thank you foens! That was exactly what i was after! :) Checkbox comming – user1213488 May 15 '12 at 14:01
  • html.GetBodyAsText() gives an exception says the objectreference not set to an instance. but i get the value using FindFirstPlainTextVersion() and then plainText.GetBodyAsText() any idea why? – Antony Mar 29 '16 at 18:15
0

This is how I did it:

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

Hope it helps.

Liquid Core
  • 1
  • 6
  • 27
  • 52
  • Since this was shorter, I tried your option, but it did not work. Gave me array conversion complaints. @foens solution worked. – Anthony Horne Feb 17 '21 at 17:59
0

Other answers in this question are incomplete and icorrect, mainly because they never use FindAllTextVersions method which is crucial.

Here is the complete method to get the actual body text content:

private static string GetMessageBodyAsText(Message message)
{
    try
    {
        List<MessagePart> list = message.FindAllTextVersions();

        // First let's try getting the plain text part:
        foreach (MessagePart part in list)
        {
            if (part != null)
            {
                return part.GetBodyAsText();
            }
        }

        // Now let's try getting the HTML part
        MessagePart html = message.FindFirstHtmlVersion();
        if (html != null)
        {
            return html.GetBodyAsText();
        }
        return null;
    }
    catch (Exception exc)
    {
        // Handle your exception here
        return null;
    }
}
Koby Douek
  • 16,156
  • 19
  • 74
  • 103
  • Does this mean that the entire HTML message is in one part and plain text is in many parts? – KeithL Aug 08 '19 at 14:22