Skip to main content
 首页 » 编程设计

c#之从 Lotus Notes 解析 MIME 电子邮件

2024年04月18日36cyq1162

我正在尝试使用 .NET Domino 互操作来解析来自 Lotus Notes 的 MIME 电子邮件。当电子邮件不是 MIME 格式时,我已经通过简单的 NotesDocument.GetFirstItem("Body").Text; 子句成功获取正文内容。但是,当我尝试解析正文内容时,对于 MIME,我得到的是 null 或空字符串。

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 或任何其他方式获取正文内容?

请您参考如下方法:

您很可能需要找到子 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(); 
    } 
}