我需要从Outlook邮箱中恢复所有邮件。我使用OpenPop开源,但是我不能恢复纯文本(值为空),我不明白为什么,因为当我检查我的邮件时,纯文本存在。当我尝试使用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;
}
}
}
}
}发布于 2013-05-20 15:30:17
我也有同样的问题。我通过选取正文的原始数据并使用一种方法将其字节转换为可读文本(string)来解决此问题。
string Body = msgList[0].MessagePart.MessageParts[0].GetBodyAsText();以下是您获得的正文文本。
msgList是调用FetchAllMe messages的结果,它为您提供了一组消息。每条消息都有包含正文文本的MessagePart。使用GetBodyAsText检索正文文本。OpenPop库中已经包含了GetBodyAsText,所以它不是我的方法。
希望这能消除你的疑虑。
发布于 2016-06-06 15:21:48
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());
}https://stackoverflow.com/questions/15975702
复制相似问题