Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to save inline images and attachments separatley?

843834Jun 28 2010 — edited Jun 29 2010
Hi,

I want to save all inline images into a certain directory and all attachments into another directory.

I use the following code to do this:
  Multipart multipart = (Multipart) message.getContent();
        
        for (int x = 0; x < multipart.getCount(); x++) {
            BodyPart bodyPart = multipart.getBodyPart(x);
            String disposition = bodyPart.getDisposition();

            if(disposition != null){
            	
            	log.debug("Disposition: " + disposition);
            
	            if (disposition.equals(BodyPart.ATTACHMENT)) {
	            	log.debug("Mail has attachment: ");
	                DataHandler handler = bodyPart.getDataHandler();
	                log.debug("FILENAME: " + handler.getName());	                
	                saveAttachment(session, bodyPart, emailData);
	            }
	            
	            else if(disposition.equals(BodyPart.INLINE)){
	            	log.debug("Mail has inline attachment: ");
	                DataHandler handler = bodyPart.getDataHandler();
	                log.debug("Inline FILENAME: " + handler.getName());
	                saveInlineAttachment(session, bodyPart, emailData);
	            }
            }
            else {
            	log.debug(bodyPart.getContent());
            }
        }
This approach works when the email has inline images only or attachments only.

It does not work when the email has both inline images and attachments. (Only the attachments are saved).

Come someone please help me correct my solution so that it can process an email that has both inline images and attachments and save them in different directories.


Thank you
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 27 2010
Added on Jun 28 2010
4 comments
830 views