Skip to content


echo " "; Profile Profile

How to parse mime message using mime4j library

There is example how to use mime4j lib. See comments below.

  1. package com.mozgoweb.mail.test;  
  2.   
  3. import java.io.ByteArrayOutputStream;  
  4. import java.io.FileInputStream;  
  5. import java.io.FileOutputStream;  
  6. import java.io.IOException;  
  7. import java.util.ArrayList;  
  8. import org.apache.james.mime4j.message.BinaryBody;  
  9. import org.apache.james.mime4j.message.BodyPart;  
  10. import org.apache.james.mime4j.message.Entity;  
  11. import org.apache.james.mime4j.message.Message;  
  12. import org.apache.james.mime4j.message.Multipart;  
  13. import org.apache.james.mime4j.message.TextBody;  
  14. import org.apache.james.mime4j.parser.Field;  
  15.   
  16. /** 
  17.  * 
  18.  * @author Denis Lunev <den@mozgoweb.com> 
  19.  */  
  20.   
  21. public class TestParser {  
  22.   
  23.     private StringBuffer txtBody;  
  24.     private StringBuffer htmlBody;  
  25.     private ArrayList<BodyPart> attachments;  
  26.   
  27.     /** 
  28.      * 
  29.      * @param fileName 
  30.      */  
  31.     public void parseMessage(String fileName) {  
  32.         FileInputStream fis = null;  
  33.   
  34.         txtBody = new StringBuffer();  
  35.         htmlBody = new StringBuffer();  
  36.         attachments<BodyPart> = new ArrayList();  
  37.   
  38.         try {  
  39.             //Get stream from file  
  40.             fis = new FileInputStream(fileName);  
  41.             //Create message with stream from file  
  42.             //If you want to parse String, you can use:  
  43.             //Message mimeMsg = new Message(new ByteArrayInputStream(mimeSource.getBytes()));  
  44.             Message mimeMsg = new Message(fis);  
  45.   
  46.             //Get some standard headers  
  47.             System.out.println("To: " + mimeMsg.getTo().toString());  
  48.             System.out.println("From: " + mimeMsg.getFrom().toString());  
  49.             System.out.println("Subject: " + mimeMsg.getSubject());  
  50.   
  51.             //Get custom header by name  
  52.             Field priorityFld = mimeMsg.getHeader().getField("X-Priority");  
  53.             //If header doesn't found it returns null  
  54.             if (priorityFld != null) {  
  55.                 //Print header value  
  56.                 System.out.println("Priority: " + priorityFld.getBody());  
  57.             }  
  58.   
  59.             //If message contains many parts - parse all parts  
  60.             if (mimeMsg.isMultipart()) {  
  61.                 Multipart multipart = (Multipart) mimeMsg.getBody();  
  62.                 parseBodyParts(multipart);  
  63.             } else {  
  64.                 //If it's single part message, just get text body  
  65.                 String text = getTxtPart(mimeMsg);  
  66.                 txtBody.append(text);  
  67.             }  
  68.   
  69.             //Print text and HTML bodies  
  70.             System.out.println("Text body: " + txtBody.toString());  
  71.             System.out.println("Html body: " + htmlBody.toString());  
  72.   
  73.             for (BodyPart attach : attachments) {  
  74.                 String attName = attach.getFilename();  
  75.                 //Create file with specified name  
  76.                 FileOutputStream fos = new FileOutputStream(attName);  
  77.                 try {  
  78.                     //Get attach stream, write it to file  
  79.                     BinaryBody bb = (BinaryBody) attach.getBody();  
  80.                     bb.writeTo(fos);  
  81.                 } finally {  
  82.                     fos.close();  
  83.                 }  
  84.             }  
  85.   
  86.         } catch (IOException ex) {  
  87.             ex.fillInStackTrace();  
  88.         } finally {  
  89.             if (fis != null) {  
  90.                 try {  
  91.                     fis.close();  
  92.                 } catch (IOException ex) {  
  93.                     ex.printStackTrace();  
  94.                 }  
  95.             }  
  96.         }  
  97.     }  
  98.   
  99.     /** 
  100.      * This method classifies bodyPart as text, html or attached file 
  101.      * 
  102.      * @param multipart 
  103.      * @throws IOException 
  104.      */  
  105.     private void parseBodyParts(Multipart multipart) throws IOException {  
  106.         for (BodyPart part : multipart.getBodyParts()) {  
  107.             if (part.isMimeType("text/plain")) {  
  108.                 String txt = getTxtPart(part);  
  109.                 txtBody.append(txt);  
  110.             } else if (part.isMimeType("text/html")) {  
  111.                 String html = getTxtPart(part);  
  112.                 htmlBody.append(html);  
  113.             } else if (part.getDispositionType() != null && !part.getDispositionType().equals("")) {  
  114.                 //If DispositionType is null or empty, it means that it's multipart, not attached file  
  115.                 attachments.add(part);  
  116.             }  
  117.   
  118.             //If current part contains other, parse it again by recursion  
  119.             if (part.isMultipart()) {  
  120.                 parseBodyParts((Multipart) part.getBody());  
  121.             }  
  122.         }  
  123.     }  
  124.   
  125.     /** 
  126.      * 
  127.      * @param part 
  128.      * @return 
  129.      * @throws IOException 
  130.      */  
  131.     private String getTxtPart(Entity part) throws IOException {  
  132.         //Get content from body  
  133.         TextBody tb = (TextBody) part.getBody();  
  134.         ByteArrayOutputStream baos = new ByteArrayOutputStream();  
  135.         tb.writeTo(baos);  
  136.         return new String(baos.toByteArray());  
  137.     }  
  138.   
  139.     /** 
  140.      * 
  141.      * @param args 
  142.      */  
  143.     public static void main(String[] args) {  
  144.   
  145.         String eml = "message.eml";  
  146.   
  147.         TestParser parser = new TestParser();  
  148.         parser.parseMessage(eml);  
  149.     }  
  150. }  

Posted in Uncategorized. Tagged with , .

0 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.