How parse email content with javax.mail?
===

I am trying to parse e-mails using javax.mail. I want to fetch both text content AND all attachments (preferrably INLINE pictures/attachments too).

I have the following code but it seems to break with more complex mails with several nested multiparts.

I have read the FAQ and googled the entire day without finding a solution.

Please help.

    public static String fetchEmailcontent(Part message, String messageid) throws IOException, MessagingException {
    		
    		StringWriter sw = new StringWriter(1024);
    
    		if (message != null && message.getContent() != null) {
    			if (message.getContent() instanceof Multipart) {
    				Multipart parts = (Multipart) message.getContent();
    				BodyPart p;
    				boolean alternative = parts.getContentType().trim().toLowerCase().startsWith("multipart/alternative") ? true : false;
    
    				InputStreamReader isr;
    				int retrieved;
    				char[] buffer = new char[512];
    				for (int i = 0; i < parts.getCount(); i++) {
    					p = parts.getBodyPart(i);
    
    					if (p.getContentType().toLowerCase().startsWith("multipart")) {
    						fetchEmailcontent(p, messageid);
    						break;
    					} else if ((Part.INLINE.equalsIgnoreCase(p.getDisposition()) || p.getDisposition() == null) && p.getContentType().toLowerCase().startsWith("text") && p.getFileName() == null) {
    
    						if (InputStream.class.isInstance(p.getContent())) {
    							InputStream ip = p.getInputStream();
    
    							StringWriter subwriter = new StringWriter(ip.available());
    							isr = new InputStreamReader(ip);
    							while (isr.ready()) {
    								retrieved = isr.read(buffer, 0, 512);
    								subwriter.write(buffer, 0, retrieved);
    							}
    							sw.write(subwriter.toString());
    						} else {
    							Object content = p.getContent();
    							if (java.io.ByteArrayInputStream.class.isInstance(content)) {
    								int bcount = ((java.io.ByteArrayInputStream) content).available();
    								byte[] c = new byte[bcount];
    								((java.io.ByteArrayInputStream) content).read(c, 0, bcount);
    								sw.write(new String(c));
    							} else {
    								sw.write(content.toString());
    							}
    						}
    						if (alternative && !"".equals(sw.toString().trim())) {
    							break;
    						}
    						sw.write("\r\n");
    					} else if (p.getDisposition() != null && (p.getDisposition().equalsIgnoreCase(Part.ATTACHMENT) || p.getDisposition().equalsIgnoreCase(Part.INLINE))) {
    						saveFile(MimeUtility.decodeText(p.getFileName()), p.getInputStream(), messageid);
    					}
    				}
    			} else if (message.getContentType().toLowerCase().startsWith("text")) {
    				sw.write(message.getContent().toString());
    			}
    		}
    		return sw.toString();
    	}

Here's an example of a mail it failed to fetch the attachments from:
(I have removed headers and the BASE64-code for the attachments to save space.. they are perfectly fine otherwise)

<BUNCH OF HEADERS>

    Content-Type: multipart/mixed; boundary=f46d04016b4779522904c58fb5b4
    
    --f46d04016b4779522904c58fb5b4
    Content-Type: multipart/alternative; boundary=f46d04016b4779522104c58fb5b2
    
    --f46d04016b4779522104c58fb5b2
    Content-Type: text/plain; charset=ISO-8859-1
    Content-Transfer-Encoding: quoted-printable
    
    test
    
    
    sdljpjdpjsd
    
    
    =E5=E4=F6
    
    --f46d04016b4779522104c58fb5b2
    Content-Type: text/html; charset=ISO-8859-1
    Content-Transfer-Encoding: quoted-printable
    
    <div>test </div><div>=A0</div><div>=A0</div><div>sdljpjdpjsd</div><div>=A0<=
    /div><div>=A0</div><div>=E5=E4=F6</div>
    
    --f46d04016b4779522104c58fb5b2--
    --f46d04016b4779522904c58fb5b4
    Content-Type: image/jpeg; name="blah.jpg"
    Content-Disposition: attachment; filename="blah.jpg"
    Content-Transfer-Encoding: base64
    X-Attachment-Id: f_h50rhk180
    
    BUNCH OF BASE64
    --f46d04016b4779522904c58fb5b4
    Content-Type: application/pdf; name="blah2.pdf"
    Content-Disposition: attachment; filename="blah2.pdf"
    Content-Transfer-Encoding: base64
    X-Attachment-Id: f_h50ria042
    
    BUNCH OF BASE64
    --f46d04016b4779522904c58fb5b4--


Thanks in advance.