// Get the mime4j configuration, or use a default one
MimeConfig config = new MimeConfig();
config.setMaxLineLen(10000); // max length of any individual header
config = context.get(MimeConfig.class, config);
MimeStreamParser parser = new MimeStreamParser(config);
XHTMLContentHandler xhtml = new XHTMLContentHandler(handler, metadata);
MailContentHandler mch = new MailContentHandler(
xhtml, metadata, config.isStrictParsing());
parser.setContentHandler(mch);
parser.setContentDecoding(true);
TaggedInputStream tagged = TaggedInputStream.get(stream);
try {
parser.parse(tagged);
} catch (IOException e) {
tagged.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