Reputation: 1
i need to recover all my message from my Outlook box. I use the OpenPop open source, but i can't recover the plain text (the value is null) and i don't understand why because when i check up my mail the plain text exist. When i try with the html version it works but i don't need this one in my project. Thanks to anyone who can help me.
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;
}
}
}
}
}
Upvotes: 0
Views: 2891
Reputation: 199
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());
}
Upvotes: 1
Reputation: 1
I had the same problem. I solved by going to pick the raw data of the body and using a method to convert its bytes to readable text (string).
string Body = msgList[0].MessagePart.MessageParts[0].GetBodyAsText();
Here is what you gets the body text.
msgList is the result of calling FetchAllMe messages, which gives you an array of messages. Every message has its MessagePart which contain the body text. Use GetBodyAsText to retrieve the body text. GetBodyAsText is already included in OpenPop library, so it's not a method of mine.
Hope this clears your doubts.
Upvotes: 1