我正在使用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;
}
}
现在,我试图遍历每条消息,但似乎无法弄清楚该如何做,到目前为止,我的按钮已经有了:
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);
}
我如何遍历allaEmail中的每个条目,以便可以在MessageBox中显示它?
最佳答案
我可以在OpenPop主页上看到fetchAllEmail example。主页上也有一个类似的示例showing how to get body text。
您可能还想看看电子邮件的实际结构。为此存在一个email introduction。
话虽如此,我会做类似下面的代码。
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());
}
希望这对您有所帮助。注意,OpenPop也有online documentation。