Example usage for javax.mail.internet MimeBodyPart getAllHeaderLines

List of usage examples for javax.mail.internet MimeBodyPart getAllHeaderLines

Introduction

In this page you can find the example usage for javax.mail.internet MimeBodyPart getAllHeaderLines.

Prototype

@Override
public Enumeration<String> getAllHeaderLines() throws MessagingException 

Source Link

Document

Get all header lines as an Enumeration of Strings.

Usage

From source file:mitm.common.security.smime.SMIMEUtils.java

public static void writeBodyPart(BodyPart bodyPart, OutputStream output, String defaultContentTransferEncoding)
        throws IOException, MessagingException {
    if (bodyPart instanceof MimeBodyPart) {
        MimeBodyPart mimeBodyPart = (MimeBodyPart) bodyPart;

        String[] contentTransferEncodings = bodyPart.getHeader("Content-Transfer-Encoding");

        String contentTransferEncoding = defaultContentTransferEncoding;

        if (contentTransferEncodings != null && contentTransferEncodings.length > 0) {
            contentTransferEncoding = contentTransferEncodings[0];
        }/*from  w w w  . j a  v a2  s .  c  o  m*/

        /*
         * First try the raw input stream.
         * If message is created from a stream Javamail will return the raw stream. If
         * the message is created from 'scratch' getRawInputStream throws a
         * MessagingException. We will therefore first try the raw version and if
         * that fails we fallback on writeTo.
         */

        try {
            InputStream input = mimeBodyPart.getRawInputStream();

            Enumeration<?> lines = mimeBodyPart.getAllHeaderLines();

            /* step through all header lines */
            while (lines.hasMoreElements()) {
                String header = (String) lines.nextElement();

                output.write(MiscStringUtils.toAsciiBytes(header));
                output.write(MailUtils.CRLF_BYTES);
            }

            output.write(MailUtils.CRLF_BYTES);

            if (!contentTransferEncoding.equalsIgnoreCase("binary")) {
                output = new CRLFOutputStream(output);
            }

            IOUtils.copy(input, output);

            output.flush();
        } catch (MessagingException e) {
            /*
             * Fallback to writeTo
             */
            if (!contentTransferEncoding.equalsIgnoreCase("binary")) {
                output = new CRLFOutputStream(output);
            }

            bodyPart.writeTo(output);

            output.flush();
        }

    } else {
        if (!defaultContentTransferEncoding.equalsIgnoreCase("binary")) {
            output = new CRLFOutputStream(output);
        }

        bodyPart.writeTo(output);

        output.flush();
    }
}

From source file:org.xmlactions.email.EMailParser.java

private void handlePart(Part part) throws MessagingException, IOException, DocumentException {

    log.debug("\n\n\nhandlePart ==>>");
    log.debug("part.toString():" + part.toString());
    log.debug(//from  w w  w . j  ava2s. com
            "part.getContent():" + (part.getFileName() == null ? part.getContent().toString() : "Attachment"));
    log.debug("part.getContentType():" + part.getContentType());
    log.debug("part.getFilename():" + part.getFileName());
    log.debug("part.isAttachment:" + part.getFileName());
    log.debug("part.isMessage:" + (part.getContent() instanceof Message));
    Object obj = part.getContent();
    if (obj instanceof Multipart) {
        Multipart mmp = (Multipart) obj;
        for (int i = 0; i < mmp.getCount(); i++) {
            Part bodyPart = mmp.getBodyPart(i);
            if (bodyPart instanceof Message) {
                setFirstMessageProcessed(true);// need to mark this when we
                // get a forwarded message
                // so we don't look for case
                // numbers in forwarded
                // emails.
            }
            handlePart(bodyPart);
        }
    } else if (obj instanceof Part) {
        if (obj instanceof Message) {
            setFirstMessageProcessed(true);// need to mark this when we get
            // a forwarded message so we
            // don't look for case numbers
            // in forwarded emails.
        }
        handlePart((Part) obj);
    } else {
        if (part instanceof MimeBodyPart) {
            MimeBodyPart p = (MimeBodyPart) part;
            Enumeration enumeration = p.getAllHeaders();
            while (enumeration.hasMoreElements()) {
                Object e = enumeration.nextElement();
                if (e == null)
                    e = null;
            }
            Object content = p.getContent();
            enumeration = p.getAllHeaderLines();
            while (enumeration.hasMoreElements()) {
                Object e = enumeration.nextElement();
                if (e == null)
                    e = null;
            }
            DataHandler dh = p.getDataHandler();
            if (dh == null)
                dh = null;
        }
        addPart(part);
        log.debug("=== Add Part ===");
        log.debug((String) (part.getFileName() != null ? "isAttachment" : part.getContent()));
        // log.info("not recognised class:" + obj.getClass().getName() +
        // "\n" + obj);
    }
    log.debug("<<== handlePart");
}