首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >OpenPop.Mime找不到明文

OpenPop.Mime找不到明文
EN

Stack Overflow用户
提问于 2013-04-12 23:49:17
回答 2查看 2.5K关注 0票数 0

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

代码语言:javascript
复制
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;
                }

            }

        }
    }
}
EN

回答 2

Stack Overflow用户

发布于 2013-05-20 15:30:17

我也有同样的问题。我通过选取正文的原始数据并使用一种方法将其字节转换为可读文本(string)来解决此问题。

代码语言:javascript
复制
string Body = msgList[0].MessagePart.MessageParts[0].GetBodyAsText();

以下是您获得的正文文本。

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

希望这能消除你的疑虑。

票数 1
EN

Stack Overflow用户

发布于 2016-06-06 15:21:48

代码语言:javascript
复制
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());
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15975702

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档