Loading an email body containing embedded images in java

My problem is this:

I have set up my code to read emails from a specific account. This part works great.

The problem is parsing the email. Separation of attachments and email bodies (containing embedded images).

My code is as follows:

Void readMessages(Folder folder){ Message[] messages = folder.getMessages(); // loading of message objects. for (int messageNumber = 0; messageNumber < messages.length; messageNumber++) { final Message currentMessage = messages[messageNumber]; logger.info("Handling the mail with subject " + currentMessage.getSubject()); logger.info("Content type for the current message is " + currentMessage.getContentType()); final String messageFileName = currentMessage.getFileName(); logger.info("File name for the message " + messageFileName + ". File name is blank " + StringUtils.isBlank(messageFileName)); Object messageContentObject = currentMessage.getContent(); if (messageContentObject instanceof Multipart) { Multipart multipart = (Multipart) messageContentObject; // downloading all attachments.... int attachmentCount = multipart.getCount(); logger.info("Number of attachments "); for (int i = 0; i < attachmentCount; i++) { Part part = (Part) multipart.getBodyPart(i); downloadAttachment(part, folderPath.toString()); } } } } } private void downloadAttachment(Part part, String folderPath) throws Exception { String disPosition = part.getDisposition(); String fileName = part.getFileName(); String decodedText = null; logger.info("Disposition type :: " + disPosition); logger.info("Attached File Name :: " + fileName); if (disPosition != null && disPosition.equalsIgnoreCase(Part.ATTACHMENT)) { logger.info("DisPosition is ATTACHMENT type."); File file = new File(folderPath + File.separator + decodedText); file.getParentFile().mkdirs(); saveEmailAttachment(file, part); } else if (fileName != null && disPosition == null) { logger.info("DisPosition is Null type but file name is valid. Possibly inline attchment"); File file = new File(folderPath + File.separator + decodedText); file.getParentFile().mkdirs(); saveEmailAttachment(file, part); } else if (fileName == null && disPosition == null) { logger.info("DisPosition is Null type but file name is null. It is email body."); File file = new File(folderPath + File.separator + "mail.html"); file.getParentFile().mkdirs(); saveEmailAttachment(file, part); } } protected int saveEmailAttachment(File saveFile, Part part) throws Exception { BufferedOutputStream bos = null; InputStream is = null; int ret = 0, count = 0; try { bos = new BufferedOutputStream(new FileOutputStream(saveFile)); part.writeTo(new FileOutputStream(saveFile)); } finally { try { if (bos != null) { bos.close(); } if (is != null) { is.close(); } } catch (IOException ioe) { logger.error("Error while closing the stream.", ioe); } } return count; } 

The problem I get is when I run this code, I get an HTML file, but the embedded images are replaced by the sign for the error image, which indicates an image without a source.

Please help me. Let me know if further information is required.

I also tried saving the body as an .eml by changing:

  File file = new File(folderPath + File.separator + "mail.html"); 

to

  File file = new File(folderPath + File.separator + "mail.eml"); 

BUt I got the same results.

+7
source share
2 answers

I wrote below code to convert email text to pdf, including inline images. in the code, I replaced the image code (for example: cid: image001.jpg@01D17AAA.1EA2A6A0 ) with the image download track. I create a "hashmap" for the image keyword and loading path when loading the image.

  HTMLWorker htmlWorker = new HTMLWorker(document); if(bodyStr!=null) { //find inline images inlineImages=downloadInLineImage(mostRecentMatch, dynamicOutputDirectory); if(inlineImages!=null) { for (Map.Entry<String, String> entry : inlineImages.entrySet()) { //System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue()); bodyStr=bodyStr.replaceAll("cid:"+entry.getKey() , entry.getValue()); } } htmlWorker.parse(new StringReader(bodyStr)); } 

Download inline image with element transfer.

  private HashMap<String,String> downloadInLineImage(Item item, String dynamicOutputDirectory) throws Exception, ServiceLocalException { //create output directory if not present //bind the item to a new email message. if you do not bind, then the getHasAttachments() function will fail EmailMessage mostRecentMatch = (EmailMessage)item; String from = mostRecentMatch.getFrom().getAddress(); String user =StringUtils.substringBefore(from, "@"); AttachmentCollection collection=item.getAttachments(); HashMap<String,String> inlineFiles=new HashMap<String,String>(); if(collection.getCount()>0) { for (Attachment attachment : collection.getItems()) { if(attachment.getIsInline()) { FileAttachment currentFile = (FileAttachment) attachment; String filePath=dynamicOutputDirectory+"/"+user+currentFile.getName(); File file=new File(filePath); FileOutputStream fio=new FileOutputStream(file); currentFile.load(fio); inlineFiles.put(currentFile.getContentId(), filePath); fio.close(); } } } 
+1
source

Links to embedded images are replaced with cid: URN, such as <img src="cid:SOMEID"> , because the <img src="cid:SOMEID"> does not contain file names. SOMEID refers to the Content-ID of multi-page "objects".

To make it work, you must store multi-page file attachments (e.g. temporary names) and replace the URN cid with real file names.

+1
source

All Articles