public void parse(InputStream stream, ContentHandler handler, Metadata metadata,
                      ParseContext context) throws IOException, SAXException, TikaException {
        // Get the mime4j configuration, or use a default one
        MimeConfig config =
                new MimeConfig.Builder().setMaxLineLen(100000).setMaxHeaderLen(100000).build();

        config = context.get(MimeConfig.class, config);
        Detector localDetector = context.get(Detector.class);
        if (localDetector == null) {
            //lazily load this if necessary
            if (detector == null) {
                EmbeddedDocumentUtil embeddedDocumentUtil = new EmbeddedDocumentUtil(context);
                detector = embeddedDocumentUtil.getDetector();
            }
            localDetector = detector;
        }
        MimeStreamParser parser =
                new MimeStreamParser(config, null, new DefaultBodyDescriptorBuilder());
        XHTMLContentHandler xhtml = new XHTMLContentHandler(handler, metadata);

        MailContentHandler mch = new MailContentHandler(xhtml, localDetector, metadata, context,
                config.isStrictParsing(), extractAllAlternatives);
        parser.setContentHandler(mch);
        parser.setContentDecoding(true);
        parser.setNoRecurse();
        xhtml.startDocument();
        TikaInputStream tstream = TikaInputStream.get(stream);
        try {
            parser.parse(tstream);
        } catch (IOException e) {
            tstream.throwIfCauseOf(e);
            throw new TikaException("Failed to parse an email message", e);
        } catch (MimeException e) {
            // Unwrap the exception in case it was not thrown by mime4j
            Throwable cause = e.getCause();
            if (cause instanceof TikaException) {
                throw (TikaException) cause;
            } else if (cause instanceof SAXException) {
                throw (SAXException) cause;
            } else {
                throw new TikaException("Failed to parse an email message", e);
            }
        }
        xhtml.endDocument();
    }
@Test
    public void testSimple() throws Exception {
        Metadata metadata = new Metadata();
        InputStream stream = getStream("test-documents/testRFC822");
        ContentHandler handler = mock(DefaultHandler.class);

        try {
            EXTRACT_ALL_ALTERNATIVES_PARSER.parse(stream, handler, metadata, new ParseContext());
            verify(handler).startDocument();
            //just one body
            verify(handler).startElement(eq(XHTMLContentHandler.XHTML), eq("p"), eq("p"),
                    any(Attributes.class));
            verify(handler).endElement(XHTMLContentHandler.XHTML, "p", "p");
            //no multi-part body parts
            verify(handler, never())
                    .startElement(eq(XHTMLContentHandler.XHTML), eq("div"), eq("div"),
                            any(Attributes.class));
            verify(handler, never()).endElement(XHTMLContentHandler.XHTML, "div", "div");
            verify(handler).endDocument();
            //note no leading spaces, and no quotes
            assertEquals("Julien Nioche (JIRA) <jira@apache.org>",
                    metadata.get(TikaCoreProperties.CREATOR));
            assertEquals("[jira] Commented: (TIKA-461) RFC822 messages not parsed",
                    metadata.get(TikaCoreProperties.TITLE));
            assertEquals("[jira] Commented: (TIKA-461) RFC822 messages not parsed",
                    metadata.get(TikaCoreProperties.SUBJECT));
        } catch (Exception e) {
            fail("Exception thrown: " + e.getMessage());
        }
    }