C# OpenPop.net 获取实际的消息文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10601913/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
OpenPop.net get actual message text
提问by user1213488
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, "[email protected]", "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 中的每个条目,以便我可以在 MessageBox 中显示它?
采纳答案by foens
I can see that you use the fetchAllEmail examplefrom the OpenPop homepage. A similar example showing how to get body textis also on the homepage.
我可以看到您使用了 OpenPop 主页中的fetchAllEmail 示例。主页上还有一个类似的示例,展示了如何获取正文文本。
You might also want to look at how emails are actually structured. A email introductionexists 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 documentationfor OpenPop.
我希望这可以帮助你。请注意,还有OpenPop 的在线文档。
回答by Liquid Core
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.
希望能帮助到你。
回答by Koby Douek
Other answers in this question are incomplete and icorrect, mainly because they never use FindAllTextVersionsmethod which is crucial.
这个问题的其他答案是不完整和不正确的,主要是因为他们从不使用FindAllTextVersions至关重要的方法。
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;
}
}

