How to read attachments from a MimeMessage using JavaMail?

Posted on 2004-11-28
Last Modified: 2013-11-24
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 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!

Question by:boyeric
    LVL 92

    Accepted Solution

    FileOutputSream out = new FileOutputStream();
    int n = 0;
    while (-1!=(
    LVL 92

    Expert Comment

    you could also use a buffer if you want to read/write chunks at a time

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Suggested Solutions

    Title # Comments Views Activity
    scoresAverage challenge 8 58
    Fibonacci challenge 11 61
    triangle challenge 4 35
    json format text only 4 46
    Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
    International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
    This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
    This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now