OpenPop.Mime找不到明文

时间:2013-04-12 15:49:17

标签: c# asp.net outlook plaintext openpop

我需要从Outlook框中恢复所有邮件。我使用OpenPop开源,但我无法恢复纯文本(值为null),我不明白为什么,因为当我检查我的邮件时,纯文本存在。当我尝试使用html版本时,它可以工作,但我不需要在我的项目中使用这个版本。感谢任何可以帮助我的人。

using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Globalization;
using System.IO;
using System.Net.Security;
using System.Net.Sockets;
using System.Text;
using System.Windows.Forms;
using OpenPop.Mime;
using OpenPop.Pop3;

namespace EmailGmail
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string hostname = ***;
            int port = **;
            bool useSsl = true;
            string username = ***;
            string password = ***;

            List<OpenPop.Mime.Message> allaEmail = FetchAllMessages(hostname, port, useSsl, username, password);

            foreach (OpenPop.Mime.Message message in allaEmail)
            {
                OpenPop.Mime.MessagePart plainText = message.FindFirstPlainTextVersion();

                OpenPop.Mime.MessagePart html = message.FindFirstHtmlVersion();

            }
        }

        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())
            {
                try
                {

                // 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));
                }

                // Now return the fetched messages
                return allMessages;
                }
                catch (Exception ex)
                {
                    return null;
                }

            }

        }
    }
}

2 个答案:

答案 0 :(得分:1)

我遇到了同样的问题。我通过选择正文的原始数据并使用一种方法将其字节转换为可读文本(字符串)来解决。

string Body = msgList[0].MessagePart.MessageParts[0].GetBodyAsText();

以下是您获取正文的内容。

msgList是调用FetchAllMe消息的结果,它为您提供了一组消息。每条消息都有其MessagePart,其中包含正文文本。使用GetBodyAsText检索正文。 GetBodyAsText已包含在OpenPop库中,因此它不是我的方法。

希望这能解决你的疑虑。

答案 1 :(得分:1)

/Za