Example usage for javax.mail MultipartDataSource getBodyPart

List of usage examples for javax.mail MultipartDataSource getBodyPart

Introduction

In this page you can find the example usage for javax.mail MultipartDataSource getBodyPart.

Prototype

public BodyPart getBodyPart(int index) throws MessagingException;

Source Link

Document

Get the specified Part.

Usage

From source file:edu.stanford.muse.email.EmailFetcherStats.java

private List<String> getAttachmentNames(MimeMessage m, Part p) throws MessagingException, IOException {
    List<String> result = new ArrayList<String>();
    try {// www  . jav  a2  s  .  c om
        if (p.isMimeType("multipart/*") || p.isMimeType("message/rfc822")) {
            if (p.isMimeType("multipart/alternative"))
                return result; // ignore alternative's because real attachments don't have alternatives
            DataHandler dh = p.getDataHandler();
            DataSource ds = dh.getDataSource();
            if (ds instanceof MultipartDataSource) {
                MultipartDataSource mpds = (MultipartDataSource) ds;
                for (int i = 0; i < mpds.getCount(); i++)
                    result.addAll(getAttachmentNames(m, mpds.getBodyPart(i)));
            } else {
                String name = ds.getName();
                if (!Util.nullOrEmpty(name))
                    result.add(name);
            }
        } else {
            String filename = p.getFileName();
            if (filename != null)
                result.add(filename);
        }
    } catch (Exception e) {
        // sometimes we see javax.mail.MessagingException: Unable to load BODYSTRUCTURE
        // in this case, just ignore, not much we can do i guess.
        Util.print_exception(e, log);
    }
    return result;
}