1
votes

I'm trying to parse MIME emails from Lotus notes with .NET Domino interop. When the emails are not in MIME format I've succeed to get Body content through simple NotesDocument.GetFirstItem("Body").Text; clause. However with MIME I'm getting null or empty string when I'm trying to parse body content.

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;                   
}

Does anyone have any experience with this problem? Or how to get body content as a HTML or RichfullText or any other way?

1
You're not testing whether the message contains MIME or rich text in the body. Are you sure that the message you're processing in this mailbox are all MIME? You should probably be using doc.hasItem("$NoteHasNativeMIME") to check. - Richard Schwartz
You're right! This is just a example of code. The final version contains switch between content types. So different branch take care of RichText and different one take care of MIME body. :) thanks for comment, it will save some time to someone. - Mastenka

1 Answers

4
votes

You most likely need to find the child MIME entities. The following Java logic should help you in the right direction:

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