javaemaillotus-notesmime

How to Programmatically Convert Lotus Notes email Document to MIME Format


I began developing a complex solution then found that the DxlExporter will do all the work for you. I wanted to share this simple solution.


Solution

  • After converting the doc to MIME via convertToMIME() use the DxlExporter to do the rest of the work. It creates XML output that contains a <mime> tag in which the output of the fully converted MIME format document resides. This code does not do a full XML parse. It simply grabs everything between the <mime> </mime> tags. I've successfully converted 10s of thousands of email documents with this code with only a handful of failures - with all of those coming from badly formatted external email documents. I've had 100% success on Notes-originated email documents.

    import lotus.domino.Document;
    import lotus.domino.DxlExporter;
    import lotus.domino.NotesException;
    import lotus.domino.Session;
    
    public class DocToMimeConverter
    {
        private static final int MIMEOPTION_DXL = 0;
        private static final String tagStart = "<mime><![CDATA[";
        private static final String tagEnd = "]]></mime>";
    
        private DxlExporter exporter = null;
        
        public DocToMimeConverter(Session session) throws NotesException
        {
            super();
            exporter = session.createDxlExporter();
        }
    
        public String convert(Document doc) throws NotesException
        {
            String mimeDoc = null;
            
            exporter.setMIMEOption(MIMEOPTION_DXL);
            
            doc.removeItem("$KeepPrivate");
            doc.convertToMIME(Document.CVT_RT_TO_PLAINTEXT_AND_HTML);
            String dxl = exporter.exportDxl(doc);
            
            int idxStart = dxl.indexOf(tagStart);
            int idxEnd = dxl.indexOf(tagEnd);
            
            if (idxStart != -1 && idxEnd != -1 && idxEnd > idxStart)
            {
                mimeDoc = dxl.substring(idxStart + tagStart.length(), idxEnd);
            }
            
            return mimeDoc;
        }
    }
    

    The $KeepPrivate will prevent any document containing it to fail. So include doc.removeItem("$KeepPrivate") if you want to convert those documents also.

    Also in the calling program:

    Session s = NotesFactory.createSession((String)null, (String)null, NotesAuth.getPassword());
    s.setConvertMIME(false);
    

    The setConvertMIME(false) says to not convert any native MIME formatted documents to Notes format. Useful if your objective is to do a MIME conversion. Saves a little time and any round trip inaccuracies.

    I used the following code to select email messages in the calling program:

    if ("Memo".equals(doc.getItemValueString("Form")) ||
            "Reply".equals(doc.getItemValueString("Form")))
    

    For my use case I used the UUID of the Notes document along with the '*.EML' to create individual files for each email message. These were then successfully imported into another email system.