I am using Rohit Joshi's IMAP library to access an IMAP email account.
So far, I have a program that connects to the mail server, retrieves all unread messages and saves them in an XML file. My problem is that some of the messages come back readable and some comeback encoded and impossible to read.
Readable:
<Part ID="1">
- <Part ID="1.1">
<DATA>Please respond if you receive this =20 Thanks =20 =20 *******=20 *************=20 =20 </DATA>
Unreadable:
<Part ID="1">
<DATA>SGVsbG8gRGVhbGVyIENhciBTZWFyY2gsCgogV2UgaGF2ZSBiZWVuIHJlcXVlc3RlZCBieSBMT1cg Q09VTlRSWSBNT1RPUlMgdG8gcmVjZWl2ZSB0aGVpciB2ZWhpY2xlCmludmVudG9yeSBkYXRhIGZy b20geW91IGZvciBFdmVyeUNhckxpc3RlZC5jb20uCiBEYXRhIHNob3VsZCBiZSBzZW50IHRvIHlv dXIgRE1pIERhdGEgJiBNZWRpYSBTdG9yZSAoYWthIC0gRGVhbGVyIFBvcnRhbCkKZnRwIGFjY291 bnQuCgogKioqKiogKioqKioKCiAqKiogKioqCgogSWYgcG9zc2libGUsIHBsZWFzZSBzZW5kIGFs bCBhdmFpbGFibGUgdmVoaWNsZSBpbnZlbnRvcnkgZGF0YSBhdCB0aGlzCnRpbWUuIFRoaXMgd2ls bCBhbGxvdyB1cyB0byBlbnN1cmUgYW55IGZ1dHVyZSBhZGp1c3RtZW50cyB0byB0aGUgZGF0YSB3 ZQpleHBvcnQgYXJlIGVhc2lseSBhY2NvbXBsaXNoZWQgd2l0aG91dCBoYXZpbmcgdG8gcmVxdWVz dCBhIGNoYW5nZSB0byB5b3VyCmV4cG9ydC4gUGxlYXNlIGRvIG5vdCBzZW5kIGltYWdlcyB1bmxl c3MgaW1hZ2VzIGFyZSByZXF1ZXN0ZWQgYmVsb3cuCgogRGVhbGVyIFBvcnRhbCBJRDogRFAzNzgy NQogTE9XIENPVU5UUlkgTU9UT1JTCiAxIEJsdWZmdG9uIFJkLgogQmx1ZmZ0b24sIFNDIDI5OTEw CgoKIENvbnRhY3QgSW5mb3JtYXRpb24gb24gUmVj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bmQgY29uZmlkZW50aWFsLiBJZiB0aGUgcmVhZGVy IG9mIHRoZSBtZXNzYWdlIGlzIG5vdCB0aGUgaW50ZW5kZWQgcmVjaXBpZW50IG9yIGFuIGF1dGhv cml6ZWQgcmVwcmVzZW50YXRpdmUgb2YgdGhlIGludGVuZGVkIHJlY2lwaWVudCwgeW91IGFyZSBo ZXJlYnkgbm90aWZpZWQgdGhhdCBhbnkgZGlzc2VtaW5hdGlvbiBvZiB0aGlzIGNvbW11bmljYXRp b24gaXMgc3RyaWN0bHkgcHJvaGliaXRlZC4gSWYgeW91IGhhdmUgcmVjZWl2ZWQgdGhpcyBjb21t dW5pY2F0aW9uIGluIGVycm9yLCBwbGVhc2Ugbm90aWZ5IHVzIGltbWVkaWF0ZWx5IGJ5IGUtbWFp bCBhbmQgZGVsZXRlIHRoZSBtZXNzYWdlIGFuZCBhbnkgYXR0YWNobWVudHMgZnJvbSB5b3VyIHN5 c3RlbS4K </DATA>
Here is my code. Any help as to what I need to do to get the email readable would be appreciated.
//Connect to the Mail Server
Imap oImap = new Imap();
oImap.Login(ConfigurationSettings.AppSettings["emailServer"], ConfigurationSettings.AppSettings["emailUser"], ConfigurationSettings.AppSettings["emailPass"]);
//Select the INBOX Folder
oImap.SelectFolder("INBOX");
//Get all Unread messages
ArrayList msgList = new ArrayList();
string[] searchData = new string[1];
searchData[0] = "unseen";
oImap.SearchMessage(searchData, false, msgList);
MemoryStream stm = new MemoryStream(); //using a memory stream buffer for XmlTextWrite to write
XmlTextWriter oXmlWriter = new XmlTextWriter(stm, System.Text.Encoding.UTF8);
oXmlWriter.Formatting = Formatting.Indented;
oXmlWriter.WriteStartDocument(true);
oXmlWriter.WriteStartElement("Emails");
for (int i = 1; i < msgList.Count; i++)
{
oXmlWriter.WriteStartElement("Message");
oXmlWriter.WriteAttributeString("UID", msgList[i].ToString());
try
{
oImap.FetchMessage(msgList[i].ToString(), oXmlWriter, true);
}
catch (Exception e)
{
}
oXmlWriter.WriteEndElement();
}
oXmlWriter.WriteEndElement();
oXmlWriter.WriteEndDocument();
oXmlWriter.Flush();
stm.Seek(0, SeekOrigin.Begin); //have to move the offset to zero to read from the beginning of the memory stream
XmlTextReader xr = new XmlTextReader(stm);
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(xr);
stm.Flush();
xr.Close();
stm.Close();
oXmlWriter.Close();
oXmlWriter = null;
xmldoc.Save("email.xml");
Console.WriteLine(xmldoc.ToString());
oImap.LogOut();
The text is encoded in Base64. You could use the following function to decode it:
Also note that the e-mail contains newlines which are not in a Windows format, but you could solve that like this: