Read an Outlook MSG file

When Outlook Express saves an email, it uses the EML format which is a good thing because the format is a standard.

But Outlook (not the Express but the one with Office) can only save an email with the MSG format which is Microsoft specific.

1. 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 an 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");
    }
  }
}

POI 3.7:

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");
        }
    }
}

2. Using msgparser library

http://auxilii.com/msgparser/

msgparser is a small open-source Java library that parses Outlook .msg files and provides their content using Java objects. msgparser uses the Apache POI – POIFS library to parse the message files which use the OLE 2 Compound Document format.

import java.util.List;
import com.auxilii.msgparser.*;
import com.auxilii.msgparser.attachment.*;

public class SimpleMsgParser {
  public static void main(String[] args) throws Exception{
    MsgParser msgp = new MsgParser();
    Message msg = msgp.parseMsg("c:/temp/test2.msg");

    String fromEmail = msg.getFromEmail();
    String fromName = msg.getFromName();
    String subject = msg.getSubject();
    String body = msg.getBodyText();

    System.out.println("From :" + fromName + " <" + fromEmail + ">");
    System.out.println("Subject :" + subject);
    System.out.println("");
    System.out.println(body);
    System.out.println("");

    List atts = msg.getAttachments();
    for (Attachment att : atts) {
      if (att instanceof FileAttachment) {
        FileAttachment file = (FileAttachment) att;
        System.out.println("Attachment : " + file.getFilename());
        // you get the actual attachment with
        // byte date[] = file.getData();
      }
    }
  }
}

3. Using jmbox

https://jmbox.dev.java.net/

The jmbox project (read jambox) is a Local Store Provider for JavaMail, enabling developers to use JavaMail api to manage the mail stored in local repositories like Outlook Express, Mozilla, Netscape etc.

At the moment are supported navigation and reading from Outlook Express 5/6 mail (dbx format).

Done! Happy Coding!

Related posts:

Spring @RequestMapping New Shortcut Annotations
Java Program to Implement Sieve Of Atkin
Java Program to Perform Arithmetic Operations on Numbers of Size
Java Program to Check Whether an Undirected Graph Contains a Eulerian Cycle
Java equals() and hashCode() Contracts
Send email with authentication
Dynamic Proxies in Java
Giới thiệu Aspect Oriented Programming (AOP)
Converting Between a List and a Set in Java
Java Program to Implement String Matching Using Vectors
How to Read HTTP Headers in Spring REST Controllers
Java Program to Generate Random Numbers Using Multiply with Carry Method
Java Program to Implement Depth-limited Search
Sorting Query Results with Spring Data
Java Program to Find k Numbers Closest to Median of S, Where S is a Set of n Numbers
Inheritance and Composition (Is-a vs Has-a relationship) in Java
Java Program to Implement JobStateReasons API
Java Program to Implement Skew Heap
Java Program to Implement D-ary-Heap
Java – Combine Multiple Collections
Immutable Objects in Java
Guide to ThreadLocalRandom in Java
Java Program to Check whether Directed Graph is Connected using BFS
Spring MVC Content Negotiation
Vấn đề Nhà sản xuất (Producer) – Người tiêu dùng (Consumer) và đồng bộ hóa các luồng trong Java
Hướng dẫn tạo và sử dụng ThreadPool trong Java
Java Program to Generate All Possible Subsets with Exactly k Elements in Each Subset
Java Program to do a Breadth First Search/Traversal on a graph non-recursively
Spring 5 Functional Bean Registration
Versioning a REST API
Custom Thread Pools In Java 8 Parallel Streams
Java Program to implement Associate Array