OpenPop.net get actual message text

旧街凉风 提交于 2019-12-01 03:10:55

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.

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.

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;
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!