Skip to content

Commit

Permalink
improved attachments extraction
Browse files Browse the repository at this point in the history
  • Loading branch information
car031 committed May 10, 2023
1 parent 6520962 commit a2d7cd7
Showing 1 changed file with 5 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.URLName;
import javax.mail.internet.ContentDisposition;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MailDateFormat;
import javax.mail.internet.MimeMessage;
Expand Down Expand Up @@ -328,7 +327,7 @@ public static EMail messageToMail(javax.mail.Message msg, boolean extractAttachm

setReplyTo(msg, email);

if (msg.isMimeType(MULTIPART_STAR) && (msg.getContent() instanceof Multipart)) {
if (msg.getContent() instanceof Multipart) {
Multipart mp = (Multipart) msg.getContent();
int count = mp.getCount();
for (int i = 1; i < count; i++) {
Expand Down Expand Up @@ -456,14 +455,15 @@ private static void setReceivedDate(javax.mail.Message msg, EMail email) throws

private static void addAttachments(BodyPart p, EMail email, boolean extractAttachmentContent)
throws UnsupportedEncodingException, MessagingException, IOException {
if (p.isMimeType(MULTIPART_STAR)) {
if (p.getContent() instanceof Multipart) {
Multipart mp = (Multipart) p.getContent();
int count = mp.getCount();
for (int i = 1; i < count; i++) {

for (int i = 0; i < count; i++) {
BodyPart bp = mp.getBodyPart(i);
if (bp.getFileName() != null && extractAttachmentContent) {
addAttachment(bp, email);
} else if (bp.isMimeType(MULTIPART_STAR)) {
} else if (p.getContent() instanceof Multipart) {
addAttachments(bp, email, extractAttachmentContent);
}
}
Expand All @@ -474,13 +474,11 @@ private static void addAttachments(BodyPart p, EMail email, boolean extractAttac

private static void addAttachment(Part part, EMail email) throws UnsupportedEncodingException, MessagingException {
String fileName = part.getFileName();
String disposition = "";
if (part.getContentType().equalsIgnoreCase("message/rfc822")) {
// The part is another email (may happen in case of forwards).
try (InputStream is = part.getInputStream()) {
EMail embeddedEmail = messageToMail(part.getInputStream(), false);
fileName = embeddedEmail.getSubject() + ".eml";
disposition = "attachment";
} catch (Throwable t) {
log.warn(t.getMessage(), t);
}
Expand All @@ -490,15 +488,6 @@ private static void addAttachment(Part part, EMail email) throws UnsupportedEnco
if (StringUtils.isEmpty(fileName))
return;

String[] contentDispositionValues = part.getHeader("Content-Disposition");
if (contentDispositionValues != null && contentDispositionValues.length > 0) {
// Skip part that specifies a Content-Disposition but it is not
// 'attachment'
disposition = new ContentDisposition(contentDispositionValues[0]).getDisposition();
if (!disposition.contains("attachment"))
return;
}

String[] contentIdValues = part.getHeader("Content-ID");
if (contentIdValues != null && contentIdValues.length > 0) {
// This is not an attachment but a content referenced by the body,
Expand Down

0 comments on commit a2d7cd7

Please sign in to comment.