Importing EML files into Notes (lots of them)
However on a closer look you might guess where the author of an outstanding movie got his inspiration from. RFC 2045 nicely states:" NOTE: The previous four definitions are clearly circular. This is unavoidable, since the overall structure of a MIME message is indeed recursive." So trying to parse that messages on my own was clearly out of the question. Luckily there is ready code available. On one side there is JavaEE and the the MimeMessage and on the other Apache James. An implementation of the
javax.mail.internet
classes is hidden somewhere in the Notes client, so that would favour MimeMessage. However I found it easier to work with the mime4j classes. Here is what I came up with:
import java.io.IOException ;
import java.io.InputStream ;
import lotus.domino.Document ;
import lotus.domino.NotesException ;
import lotus.domino.Session ;
import org.apache.james.mime4j.MimeException ;
import org.apache.james.mime4j.parser.ContentHandler ;
import org.apache.james.mime4j.parser.MimeStreamParser ;
public class Mime2Doc {
public void importMail (Session s, InputStream in, Document doc ) throws NotesException, MimeException, IOException {
doc. replaceItemValue ( "Form", "Memo" ) ;
MimeStreamParser parser = new MimeStreamParser ( ) ;
ContentHandler h = new DocContentHandler (s, doc ) ;
parser. setContentHandler (h ) ;
parser. parse (in ) ;
}
}
The final piece in the puzzle is the class that helps to track the MIME Parts that are put onto the Stack and returns MIME-Type and encoding derived from the header fields. You are ready to test.I used a directory full of eml files to test:
import java.io.File ;
import java.io.FileInputStream ;
import java.io.FileNotFoundException ;
import java.io.IOException ;
import org.apache.james.mime4j.MimeException ;
import lotus.domino.Document ;
import lotus.domino.NotesException ;
import lotus.domino.NotesFactory ;
import lotus.domino.NotesThread ;
import lotus.domino.Session ;
import lotus.domino.Database ;
public class Tester {
public static void main ( String [ ] args ) {
// Change these for your own test
final String WORKDIR = "/home/user/testingmime/" ;
final String EXTENSION = ".eml" ;
final String DBNAME = "testmail.nsf" ;
final String SERVER = "" ;
NotesThread. sinitThread ( ) ;
try {
Session s = NotesFactory. createSession ( ) ;
Database db = s. getDatabase (SERVER, DBNAME ) ;
File folder = new File (WORKDIR ) ;
File [ ] tobeImported = folder. listFiles ( ) ;
Mime2Doc md = new Mime2Doc ( ) ;
for ( int i = 0 ; i < tobeImported. length ; i ++ ) {
File f = tobeImported [i ] ;
if (f. isFile ( ) && f. getName ( ). endsWith (EXTENSION ) ) {
System. out. println ( "File: " +f. getName ( ) ) ;
Document doc = db. createDocument ( ) ;
FileInputStream in = new FileInputStream (f ) ;
md. importMail (s,in, doc ) ;
doc. recycle ( ) ;
}
}
System. out. println ( "--- DONE ---" ) ;
// Cleanup
db. recycle ( ) ;
} catch (NotesException e ) {
e. printStackTrace ( ) ;
} catch ( FileNotFoundException e ) {
e. printStackTrace ( ) ;
} catch (MimeException e ) {
e. printStackTrace ( ) ;
} catch ( IOException e ) {
e. printStackTrace ( ) ;
} finally {
NotesThread. stermThread ( ) ;
}
}
}
Posted by Stephan H Wissel on 23 April 2012 | Comments (18) | categories: Show-N-Tell Thursday