Link to home
Start Free TrialLog in
Avatar of boyeric
boyeric

asked on

How to read attachments from a MimeMessage using JavaMail?

Hi, dudes! i have a problem concerning JavaMail library, when i want to get attachment files after i acquire messages from the Email server. i've checked up the sun.com online tutorial, they showed me some sample codes like below:

Multipart mp = (Multipart)message.getContent();

for (int i=0, n=multipart.getCount(); i<n; i++) {
  Part part = multipart.getBodyPart(i));

  String disposition = part.getDisposition();

  if ((disposition != null) && 
      ((disposition.equals(Part.ATTACHMENT) ||
       (disposition.equals(Part.INLINE))) {
    saveFile(part.getFileName(), part.getInputStream());
  }
}

But i still have no idea how this "saveFile" method should be written? how can i put the InputStream into the new created file? and do i have to handle the different MimeTypes (.txt, .gif, .zip.....) by myself?

As you may see, i am still new to Java, so will be nice of you to write it in more detail. :) Thanx in advance!

Eric
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
you could also use a buffer if you want to read/write chunks at a time