GSG GSG - 1 year ago 70
Java Question

downloading email body containing inline images in java

My issue goes as follows:

I have My code setup to read emails from a particular account. That part works perfectly.

the issue is with parsing the Email message. Separating attachments and email body(containing inline images).

My code goes like this:

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];"Handling the mail with subject " + currentMessage.getSubject());"Content type for the current message is " + currentMessage.getContentType());
final String messageFileName = currentMessage.getFileName();"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();"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;"Disposition type :: " + disPosition);"Attached File Name :: " + fileName);

if (disPosition != null && disPosition.equalsIgnoreCase(Part.ATTACHMENT)) {"DisPosition is ATTACHMENT type.");
File file = new File(folderPath + File.separator + decodedText);
saveEmailAttachment(file, part);
} else if (fileName != null && disPosition == null) {"DisPosition is Null type but file name is valid. Possibly inline attchment");
File file = new File(folderPath + File.separator + decodedText);
saveEmailAttachment(file, part);
} else if (fileName == null && disPosition == null) {"DisPosition is Null type but file name is null. It is email body.");
File file = new File(folderPath + File.separator + "mail.html");
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) {
if (is != null) {
} 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 inline images is replaced by a sign for error image which indicates the image with no source.

Please help me out with. Let me know if any more info is required.

I also tried saving the body as an
file by changing:

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


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

BUt i got the same results.

Answer Source

I wrote below code to convert email body text to pdf including inline images. in code i replaced the image code(ex: cid:image001.jpg@01D17AAA.1EA2A6A0) with download image path. I am constructing the "hashmap" for image key and download path while downloading the image.

 HTMLWorker htmlWorker = new HTMLWorker(document);

                //find inline images
                inlineImages=downloadInLineImage(mostRecentMatch, dynamicOutputDirectory);

                    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 passing Item.

 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>();

        for (Attachment attachment : collection.getItems()) {


                FileAttachment currentFile = (FileAttachment) attachment;
                String filePath=dynamicOutputDirectory+"/"+user+currentFile.getName();
                File file=new File(filePath);
                FileOutputStream fio=new FileOutputStream(file);
                inlineFiles.put(currentFile.getContentId(), filePath);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download