c# - OpenPop.net 获取实际消息文本

标签 c# asp.net email openpop

我正在使用 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, "xxxxx@hotmail.com", "xxxxx");

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

我将如何遍历 allaEmail 中的每个条目以便我可以将其显示在 MessageBox 中?

最佳答案

我可以看到你使用了 fetchAllEmail example来自 OpenPop 主页。类似的例子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());
}

希望这对您有所帮助。请注意,还有 online documentation用于 OpenPop。

关于c# - OpenPop.net 获取实际消息文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10601913/

相关文章:

asp.net - 如何更正部分回发时丢失的 ASP.NET Webform jquery 引用

c# - 使用 Visual Studio 开发服务器时如何让 Web 服务使用固定端口号?

python - 使用python和imaplib的gmail登录失败

django - 电子邮件,与发件人地址不同的 'reply to' 地址

python - 如何不使用 gmail 从 python 发送电子邮件?

c# - 为什么在ApiController中获取ActionName这么复杂?更简单的方法?

c# - 由于复制重复引用而导致构建时间长

mysql - 指定要在 dv.Table.Rows(x)(VALUE).ToString() 中使用的行

c# - 为什么.net 的 MailMessage 类的 To 属性是只读的?

c# - Windows 手机 8 : Play YouTube live event stream