获取实际的消息文本

时间:2022-09-03 11:20:10

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:

我正在使用OpenPop.net尝试从给定收件箱中的所有电子邮件中解析我们的链接。我找到了这个方法来获取所有信息:

    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?

如何循环遍历allaEmail中的每个条目,以便在消息框中显示它?

2 个解决方案

#1


26  

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.

我可以看到您使用了来自OpenPop主页的fetchAllEmail示例。主页上也有一个类似的例子显示如何获取正文文本。

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.

我希望这能在路上对你有所帮助。注意,OpenPop还有在线文档。

#2


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.

希望它可以帮助。

#1


26  

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.

我可以看到您使用了来自OpenPop主页的fetchAllEmail示例。主页上也有一个类似的例子显示如何获取正文文本。

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.

我希望这能在路上对你有所帮助。注意,OpenPop还有在线文档。

#2


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.

希望它可以帮助。