Example usage for javax.mail.internet MimePart getAllHeaderLines

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

Introduction

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

Prototype

public Enumeration<String> getAllHeaderLines() throws MessagingException;

Source Link

Document

Get all header lines as an Enumeration of Strings.

Usage

From source file:mitm.common.mail.MailUtils.java

@SuppressWarnings("rawtypes")
public static void writeMessageRaw(MimePart part, OutputStream output) throws IOException, MessagingException {
    Enumeration lines = part.getAllHeaderLines();

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

        output.write(MiscStringUtils.toAsciiBytes(header));
        output.write(CRLF_BYTES);/*from w  w  w .jav a2  s .c  o  m*/
    }

    output.write(CRLF_BYTES);

    InputStream input;

    /*
     * 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 will try getInputStream.
     *
     * Only MimeMessage has getRawInputStream so we check whether the part 
     * is a MimeMessage
     */
    if (part instanceof MimeMessage) {
        try {
            input = ((MimeMessage) part).getRawInputStream();
        } catch (MessagingException e) {
            /*
             * Could be that we are using Javamail 1.3 which does not support 
             * getRawInputStream. Use the inputStream instead
             */
            input = part.getInputStream();
        }
    } else {
        input = part.getInputStream();
    }

    IOUtils.copy(input, output);
}