final Session mailSession = Session.getDefaultInstance(props, null); final MimeMessage message = new MimeMessage(mailSession, in); final String content = getBodyText(message); final ExtractData data = new ExtractData(content != null ? content : StringUtil.EMPTY); final Enumeration<Header> headers = message.getAllHeaders(); putValue(data, "Content-ID", message.getContentID()); putValue(data, "Content-Language", message.getContentLanguage()); putValue(data, "Content-MD5", message.getContentMD5()); putValue(data, "Description", message.getDescription()); putValue(data, "Disposition", message.getDisposition()); putValue(data, "Encoding", message.getEncoding()); putValue(data, "File-Name", message.getFileName()); putValue(data, "From", message.getFrom()); putValue(data, "Line-Count", message.getLineCount()); putValue(data, "Message-ID", message.getMessageID()); putValue(data, "Message-Number", message.getMessageNumber()); putValue(data, "Received-Date", getReceivedDate(message)); putValue(data, "Reply-To", message.getReplyTo()); putValue(data, "Sender", message.getSender()); putValue(data, "Sent-Date", message.getSentDate()); putValue(data, "Size", message.getSize()); putValue(data, "Subject", message.getSubject()); putValue(data, "Receipients", message.getAllRecipients()); putValue(data, "To", message.getRecipients(Message.RecipientType.TO)); putValue(data, "Cc", message.getRecipients(Message.RecipientType.CC)); putValue(data, "Bcc", message.getRecipients(Message.RecipientType.BCC)); return data; } catch (final MessagingException e) {
protected static Date getReceivedDate(final javax.mail.Message message) throws MessagingException { final Date today = new Date(); final String[] received = message.getHeader("received"); if (received != null) { for (final String v : received) { String dateStr = null; try { dateStr = getDateString(v); final Date receivedDate = new MailDateFormat().parse(dateStr); if (!receivedDate.after(today)) { return receivedDate; } } catch (final ParseException e) { // ignore } } } return null; }
protected void appendAttachment(final StringBuilder buf, final BodyPart bodyPart) { final MimeTypeHelper mimeTypeHelper = getMimeTypeHelper(); final ExtractorFactory extractorFactory = getExtractorFactory(); try { final String filename = getDecodeText(bodyPart.getFileName()); final String mimeType = mimeTypeHelper.getContentType(null, filename); if (mimeType != null) { final Extractor extractor = extractorFactory.getExtractor(mimeType); if (extractor != null) { try (final InputStream in = bodyPart.getInputStream()) { final Map<String, String> map = new HashMap<>(); map.put(TikaMetadataKeys.RESOURCE_NAME_KEY, filename); final String content = extractor.getText(in, map).getContent(); buf.append(content).append(' '); } catch (final Exception e) { if (logger.isDebugEnabled()) { logger.debug("Exception in an internal extractor.", e); } } } } } catch (MessagingException e) { if (logger.isDebugEnabled()) { logger.debug("Exception in parsing BodyPart.", e); } } }
if (value instanceof String) { if ("Subject".equals(key)) { data.putValue(key, getDecodeText(value.toString())); } else { data.putValue(key, value.toString()); for (int i = 0; i < size; i++) { final Address address = ((Address[]) value)[i]; values[i] = getDecodeText(address.toString());
final BodyPart bodyPart = multipart.getBodyPart(i); if (Part.ATTACHMENT.equalsIgnoreCase(bodyPart.getDisposition())) { appendAttachment(buf, bodyPart); } else if (bodyPart.isMimeType("text/plain")) { buf.append(bodyPart.getContent().toString()).append(' ');