Search code examples
c#emaillotus-noteslotus-dominomime

Parse MIME email from Lotus Notes


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?


Solution

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