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