2016-08-11 196 views
1

我想從.NET Domino互操作中解析Lotus Notes的MIME電子郵件。當電子郵件不是MIME格式時,我已成功通過簡單的NotesDocument.GetFirstItem("Body").Text;子句獲取正文內容。但是在MIME中,當我試圖解析正文內容時,我得到null或空字符串。解析來自Lotus Notes的MIME電子郵件

var session = new NotesSession(); 
session.Initialize("RadioLotus028"); 
session.ConvertMime = false; 
var db = session.GetDatabase("PRGLNApps01/CZ/RFERL", "mail-in\\SEEurope\\MIA.nsf", false); 
if (db == null) throw new ArgumentNullException("cannot load database"); 

var legnth = db.AllDocuments.Count; 
for (int i = 1; i < legnth; i++) 
{ 
    NotesDocument doc = db.AllDocuments.GetNthDocument(i); 
    NotesMIMEEntity bodyMIME = doc.GetMIMEEntity(); 

    NotesStream stream = session.CreateStream(); 
    //bodyMIME.GetContentAsBytes(stream); 
    //bodyMIME.GetEntityAsText(stream); 
    bodyMIME.GetContentAsText(stream); 

    string bodyString = stream.ReadText(); 
    var bodyString2 = stream.Read(); 
    string bodyString3 = bodyMIME.ContentAsText; 

    var from = doc.GetFirstItem("From").Text; 
    var subject = doc.GetFirstItem("Subject").Text;     
} 

有沒有人有這個問題的經驗?或者如何以HTML或RichfullText或其他方式獲取正文內容?

+0

您不測試消息是否包含MIME或正文中的富文本。您確定您在此郵箱中處理的郵件全部是MIME嗎?你應該使用doc.hasItem(「$ NoteHasNativeMIME」)來檢查。 –

+0

你是對的!這只是代碼的一個例子。最終版本包含內容類型之間的切換。所以不同的分支照顧RichText和不同的分支照顧MIME主體。 :)感謝評論,這將節省一些時間給某人。 – Mastenka

回答

3

你很可能需要找到子MIME實體。以下Java邏輯應該可以幫助您朝着正確的方向發展:

MIMEEntity mime = sourceDoc.getMIMEEntity(bodyField); 
if (mime != null) { 
    // If multipart MIME entity 
    if (mime.getContentType().equals("multipart")) { 
     // Find text/html content of each child entity 
     MIMEEntity child1 = mime.getFirstChildEntity(); 
     while (child1 != null) { 
      if (child1.getContentType().contains("text")) { 
       html = child1.getContentAsText(); 
      } 
      MIMEEntity child2 = child1.getFirstChildEntity(); 
      if (child2 == null) { 
       child2 = child1.getNextSibling(); 
       if (child2 == null) { 
        child2 = child1.getParentEntity(); 
        if (child2 != null) { 
         child2 = child2.getNextSibling(); 
        } 
       } 
      } 
      child1 = child2; 
     } 
    } else { 
     // Not multipart 
     html = mime.getContentAsText(); 
    } 
} 
+1

感謝您的解決方案是正確的......我一直都在想念它! :( – Mastenka

相關問題