Example usage for javax.mail MultipartDataSource getCount

List of usage examples for javax.mail MultipartDataSource getCount

Introduction

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

Prototype

public int getCount();

Source Link

Document

Return the number of enclosed BodyPart objects.

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 {//from w ww .  j  a va 2  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;
}