[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 5463
  • Last Modified:

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
0
boyeric
Asked:
boyeric
  • 2
1 Solution
 
objectsCommented:
FileOutputSream out = new FileOutputStream();
int n = 0;
while (-1!=(n=in.read()))
{
   out.write(n);
}
0
 
objectsCommented:
you could also use a buffer if you want to read/write chunks at a time
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now