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:
importjava.io.IOException;
importjava.io.InputStream;
importlotus.domino.Document;
importlotus.domino.NotesException;
importlotus.domino.Session;
importorg.apache.james.mime4j.MimeException;
importorg.apache.james.mime4j.parser.ContentHandler;
importorg.apache.james.mime4j.parser.MimeStreamParser;
publicclass Mime2Doc {
publicvoid 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:
importjava.io.File;
importjava.io.FileInputStream;
importjava.io.FileNotFoundException;
importjava.io.IOException;
importorg.apache.james.mime4j.MimeException;
importlotus.domino.Document;
importlotus.domino.NotesException;
importlotus.domino.NotesFactory;
importlotus.domino.NotesThread;
importlotus.domino.Session;
importlotus.domino.Database;
publicclass Tester {
publicstaticvoid main (String[] args ){
// Change these for your own test
finalString WORKDIR ="/home/user/testingmime/";
finalString EXTENSION =".eml";
finalString DBNAME ="testmail.nsf";
finalString SERVER ="";
NotesThread. sinitThread();
try{
Session s = NotesFactory. createSession();
Database db = s. getDatabase(SERVER, DBNAME );
File folder =newFile(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 =newFileInputStream(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