Read an Outlook MSG fileTag(s): Networking Open Source
When Outlook Express saves an email, it uses the EML format which is a good thing because the format is a standard.
You read them with Javamail easily, see this HowTo.
But Outlook (not the Express but the one with Office) can only save an email with the MSG format which is Microsoft specific.
Apache POI HSMF
http://poi.apache.org/hsmf/HSMF is the POI Project's pure Java implementation of the Outlook MSG format.
This example takes a MSG file and extracts the attachment(s).
POI 3.6
import java.io.ByteArrayInputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.io.PrintWriter; import java.util.Iterator; import java.util.Map; import org.apache.poi.hsmf.MAPIMessage; import org.apache.poi.hsmf.datatypes.AttachmentChunks; import org.apache.poi.hsmf.exceptions.ChunkNotFoundException; // You need poi-scratchpad-3.6 and poi-3.6 ( http://poi.apache.org/ ) public class DetectMSGAttachment { public static void main (String ... args) throws IOException { String msgfile = "c:/temp/messagewithattachment.msg"; MAPIMessage msg = new MAPIMessage(msgfile); Map attachmentMap = msg.getAttachmentFiles(); if(attachmentMap.size() > 0) { for (Iterator ii = attachmentMap.entrySet().iterator(); ii.hasNext();) { Map.Entry entry = (Map.Entry)ii.next(); String attachmentfilename = entry.getKey().toString(); System.out.println(attachmentfilename); // extract attachment ByteArrayInputStream fileIn = (ByteArrayInputStream)entry.getValue(); File f = new File("c:/temp", attachmentfilename); // output OutputStream fileOut = null; try { fileOut = new FileOutputStream(f); byte[] buffer = new byte[2048]; int bNum = fileIn.read(buffer); while(bNum > 0) { fileOut.write(buffer); bNum = fileIn.read(buffer); } } finally { try { if(fileIn != null) { fileIn.close(); } } finally { if(fileOut != null) { fileOut.close(); } } } } } else { System.out.println("No attachment"); } } }
import java.io.ByteArrayInputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.io.PrintWriter; import java.util.Iterator; import java.util.Map; import org.apache.poi.hdgf.chunks.Chunk; import org.apache.poi.hsmf.MAPIMessage; import org.apache.poi.hsmf.datatypes.AttachmentChunks; import org.apache.poi.hsmf.datatypes.Chunks; import org.apache.poi.hsmf.exceptions.ChunkNotFoundException; // You need poi-scratchpad-3.7 and poi-3.7 ( http://poi.apache.org/ ) public class DetectMSGAttachment { public static void main (String ... args) throws IOException { String msgfile = "c:/temp/messagewithattachment.msg"; MAPIMessage msg = new MAPIMessage(msgfile); AttachmentChunks attachments[] = msg.getAttachmentFiles(); if(attachments.length > 0) { for (AttachmentChunks a : attachments) { System.out.println(a.attachLongFileName); // extract attachment ByteArrayInputStream fileIn = new ByteArrayInputStream(a.attachData.getValue()); File f = new File("c:/temp", a.attachLongFileName.toString()); // output OutputStream fileOut = null; try { fileOut = new FileOutputStream(f); byte[] buffer = new byte[2048]; int bNum = fileIn.read(buffer); while(bNum > 0) { fileOut.write(buffer); bNum = fileIn.read(buffer); } } finally { try { if(fileIn != null) { fileIn.close(); } } finally { if(fileOut != null) { fileOut.close(); } } } } } else { System.out.println("No attachment"); } } }
mail_outline
Send comment, question or suggestion to howto@rgagnon.com
Send comment, question or suggestion to howto@rgagnon.com