Example usage for javax.mail Multipart removeBodyPart

List of usage examples for javax.mail Multipart removeBodyPart

Introduction

In this page you can find the example usage for javax.mail Multipart removeBodyPart.

Prototype

public synchronized void removeBodyPart(int index) throws MessagingException 

Source Link

Document

Remove the part at specified location (starting from 0).

Usage

From source file:org.apache.james.transport.mailets.StripAttachment.java

/**
 * Checks every part in this part (if it is a Multipart) for having a
 * filename that matches the pattern. If the name matches, the content of
 * the part is stored (using its name) in te given diretcory.
 * /*from   w  w  w  .ja  v a2 s.  c om*/
 * Note: this method is recursive.
 * 
 * @param part
 *            The part to analyse.
 * @param mail
 * @return
 * @throws Exception
 */
private boolean analyseMultipartPartMessage(Part part, Mail mail) throws Exception {
    if (part.isMimeType("multipart/*")) {
        try {
            Multipart multipart = (Multipart) part.getContent();
            boolean atLeastOneRemoved = false;
            int numParts = multipart.getCount();
            for (int i = 0; i < numParts; i++) {
                Part p = multipart.getBodyPart(i);
                if (p.isMimeType("multipart/*")) {
                    atLeastOneRemoved |= analyseMultipartPartMessage(p, mail);
                } else {
                    boolean removed = checkMessageRemoved(p, mail);
                    if (removed) {
                        multipart.removeBodyPart(i);
                        atLeastOneRemoved = true;
                        i--;
                        numParts--;
                    }
                }
            }
            if (atLeastOneRemoved) {
                part.setContent(multipart);
                if (part instanceof Message) {
                    ((Message) part).saveChanges();
                }
            }
            return atLeastOneRemoved;
        } catch (Exception e) {
            log("Could not analyse part.", e);
        }
    }
    return false;
}

From source file:org.mxhero.engine.plugin.attachmentlink.alcommand.internal.domain.Message.java

public void removeAll(Part part, BodyPart notDelete) throws MessagingException, IOException {
    if (isAttach(part)) {
        if (part != notDelete) {
            BodyPart multi = (BodyPart) part;
            Multipart parent = multi.getParent();
            parent.removeBodyPart(multi);
        }/*from   w  ww. j a v  a 2  s.c o  m*/
    } else if (part.isMimeType(MULTIPART_TYPE)) {
        Multipart mp = (Multipart) part.getContent();
        List<BodyPart> toRemove = new ArrayList<BodyPart>();
        for (int i = 0; i < mp.getCount(); i++) {
            BodyPart bodyPart = mp.getBodyPart(i);
            if (removePart(bodyPart, notDelete)) {
                toRemove.add(bodyPart);
            } else {
                removeAll(bodyPart, notDelete);
            }
        }
        for (BodyPart bp : toRemove)
            mp.removeBodyPart(bp);
    }
}