Example usage for javax.mail Part setContent

List of usage examples for javax.mail Part setContent

Introduction

In this page you can find the example usage for javax.mail Part setContent.

Prototype

public void setContent(Multipart mp) throws MessagingException;

Source Link

Document

This method sets the given Multipart object as this message's content.

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.
 * //w  ww .  j a va2 s  .  c  o m
 * 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.orbeon.oxf.processor.EmailProcessor.java

private void handleBody(PipelineContext pipelineContext, String dataInputSystemId, Part parentPart,
        Element bodyElement) throws Exception {

    // Find out if there are embedded parts
    final Iterator parts = bodyElement.elementIterator("part");
    String multipart;//from ww w . j av a2 s . c o m
    if (bodyElement.getName().equals("body")) {
        multipart = bodyElement.attributeValue("mime-multipart");
        if (multipart != null && !parts.hasNext())
            throw new OXFException("mime-multipart attribute on body element requires part children elements");
        // TODO: Check following lines, which were doing nothing!
        //            final String contentTypeFromAttribute = NetUtils.getContentTypeMediaType(bodyElement.attributeValue("content-type"));
        //            if (contentTypeFromAttribute != null && contentTypeFromAttribute.startsWith("multipart/"))
        //                contentTypeFromAttribute.substring("multipart/".length());
        if (parts.hasNext() && multipart == null)
            multipart = DEFAULT_MULTIPART;
    } else {
        final String contentTypeAttribute = NetUtils
                .getContentTypeMediaType(bodyElement.attributeValue("content-type"));
        multipart = (contentTypeAttribute != null && contentTypeAttribute.startsWith("multipart/"))
                ? contentTypeAttribute.substring("multipart/".length())
                : null;
    }

    if (multipart != null) {
        // Multipart content is requested
        final MimeMultipart mimeMultipart = new MimeMultipart(multipart);

        // Iterate through parts
        while (parts.hasNext()) {
            final Element partElement = (Element) parts.next();

            final MimeBodyPart mimeBodyPart = new MimeBodyPart();
            handleBody(pipelineContext, dataInputSystemId, mimeBodyPart, partElement);
            mimeMultipart.addBodyPart(mimeBodyPart);
        }

        // Set content on parent part
        parentPart.setContent(mimeMultipart);
    } else {
        // No multipart, just use the content of the element and add to the current part (which can be the main message)
        handlePart(pipelineContext, dataInputSystemId, parentPart, bodyElement);
    }
}