IMAP编码电子邮件

时间:2011-08-11 12:47:13

标签: c# email imap

我正在使用Rohit Joshi's IMAP library访问IMAP电子邮件帐户。

到目前为止,我有一个连接到邮件服务器的程序,检索所有未读邮件并将它们保存在XML文件中。我的问题是,有些消息可以回读,有些回复编码,无法读取。

可读:

<Part ID="1">
- <Part ID="1.1">
  <DATA>Please respond if you receive this =20 Thanks =20 =20 *******=20 *************=20 =20 </DATA> 

不可读:

<Part ID="1">
  <DATA>SGVsbG8gRGVhbGVyIENhciBTZWFyY2gsCgogV2UgaGF2ZSBiZWVuIHJlcXVlc3RlZCBieSBMT1cg Q09VTlRSWSBNT1RPUlMgdG8gcmVjZWl2ZSB0aGVpciB2ZWhpY2xlCmludmVudG9yeSBkYXRhIGZy b20geW91IGZvciBFdmVyeUNhckxpc3RlZC5jb20uCiBEYXRhIHNob3VsZCBiZSBzZW50IHRvIHlv dXIgRE1pIERhdGEgJiBNZWRpYSBTdG9yZSAoYWthIC0gRGVhbGVyIFBvcnRhbCkKZnRwIGFjY291 bnQuCgogKioqKiogKioqKioKCiAqKiogKioqCgogSWYgcG9zc2libGUsIHBsZWFzZSBzZW5kIGFs bCBhdmFpbGFibGUgdmVoaWNsZSBpbnZlbnRvcnkgZGF0YSBhdCB0aGlzCnRpbWUuIFRoaXMgd2ls bCBhbGxvdyB1cyB0byBlbnN1cmUgYW55IGZ1dHVyZSBhZGp1c3RtZW50cyB0byB0aGUgZGF0YSB3 ZQpleHBvcnQgYXJlIGVhc2lseSBhY2NvbXBsaXNoZWQgd2l0aG91dCBoYXZpbmcgdG8gcmVxdWVz dCBhIGNoYW5nZSB0byB5b3VyCmV4cG9ydC4gUGxlYXNlIGRvIG5vdCBzZW5kIGltYWdlcyB1bmxl c3MgaW1hZ2VzIGFyZSByZXF1ZXN0ZWQgYmVsb3cuCgogRGVhbGVyIFBvcnRhbCBJRDogRFAzNzgy NQogTE9XIENPVU5UUlkgTU9UT1JTCiAxIEJsdWZmdG9uIFJkLgogQmx1ZmZ0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heSBjb250YWluIGluZm9y bWF0aW9uIHRoYXQgaXMgcHJpdmlsZWdlZCBhbmQgY29uZmlkZW50aWFsLiBJZiB0aGUgcmVhZGVy IG9mIHRoZSBtZXNzYWdlIGlzIG5vdCB0aGUgaW50ZW5kZWQgcmVjaXBpZW50IG9yIGFuIGF1dGhv cml6ZWQgcmVwcmVzZW50YXRpdmUgb2YgdGhlIGludGVuZGVkIHJlY2lwaWVudCwgeW91IGFyZSBo ZXJlYnkgbm90aWZpZWQgdGhhdCBhbnkgZGlzc2VtaW5hdGlvbiBvZiB0aGlzIGNvbW11bmljYXRp b24gaXMgc3RyaWN0bHkgcHJvaGliaXRlZC4gSWYgeW91IGhhdmUgcmVjZWl2ZWQgdGhpcyBjb21t dW5pY2F0aW9uIGluIGVycm9yLCBwbGVhc2Ugbm90aWZ5IHVzIGltbWVkaWF0ZWx5IGJ5IGUtbWFp bCBhbmQgZGVsZXRlIHRoZSBtZXNzYWdlIGFuZCBhbnkgYXR0YWNobWVudHMgZnJvbSB5b3VyIHN5 c3RlbS4K </DATA> 

这是我的代码。对于我需要做些什么以获得电子邮件可读性的任何帮助将不胜感激。

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

1 个答案:

答案 0 :(得分:1)

文本在Base64中编码。您可以使用以下函数对其进行解码:

public string DecodeBase64(string s)
{
    byte[] buf = Convert.FromBase64String(s);
    return System.Text.Encoding.UTF8.GetString(buf);
}

另请注意,电子邮件包含不是Windows格式的换行符,但您可以这样解决:

resultStr = DecodeBase64(emailStr).Replace("\n", System.Environment.NewLine);