@@ -450,6 +450,14 @@ public MimeMessage toMimeMessage(Session session) throws IOException, MessagingE
450
450
451
451
if (plainText ==null && html ==null ) { throw new MessagingException ("missing body" ); }
452
452
453
+ final List <OutlookMessageAttachment > regularAttachments = getAttachments ().stream ()
454
+ .filter (att -> att .getContentId () == null )
455
+ .collect (Collectors .toList ());
456
+
457
+ final List <OutlookMessageAttachment > inlineAttachments = getAttachments ().stream ()
458
+ .filter (att -> att .getContentId () != null )
459
+ .collect (Collectors .toList ());
460
+
453
461
final MimeMultipart multipart = new MimeMultipart ();
454
462
455
463
final List <MimeBodyPart > bodies = new ArrayList <>();
@@ -477,15 +485,44 @@ public MimeMessage toMimeMessage(Session session) throws IOException, MessagingE
477
485
combinedBodiesPart = bodies .get (0 );
478
486
}
479
487
480
- multipart .addBodyPart (combinedBodiesPart );
488
+ if (inlineAttachments .isEmpty ()) {
489
+ multipart .addBodyPart (combinedBodiesPart );
490
+ }
491
+ else {
492
+ final MimeMultipart relatedPart = new MimeMultipart ("related" );
493
+ relatedPart .addBodyPart (combinedBodiesPart );
494
+
495
+ for (OutlookMessageAttachment attachment : inlineAttachments ) {
496
+ String name = attachment .getName ();
497
+ String mimeType = attachment .getMimeType ();
498
+ byte [] data = readAttachement (attachment );
499
+ String contentId = attachment .getContentId ();
500
+
501
+ MimeBodyPart part = new MimeBodyPart ();
502
+ part .setDataHandler (new DataHandler (new ByteArrayDataSource (data , mimeType )));
503
+ part .setContentID (contentId );
504
+ part .setHeader ("Content-Disposition" , "inline" );
505
+ part .setFileName (name );
506
+ relatedPart .addBodyPart (part );
507
+ }
481
508
482
- for (OutlookMessageAttachment attachment : getAttachments ()) {
509
+ final MimeBodyPart relatedBodyPart = new MimeBodyPart ();
510
+ relatedBodyPart .setContent (relatedPart );
511
+ multipart .addBodyPart (relatedBodyPart );
512
+ }
513
+
514
+ for (OutlookMessageAttachment attachment : regularAttachments ) {
483
515
String name = attachment .getName ();
484
516
String mimeType = attachment .getMimeType ();
485
517
byte [] data = readAttachement (attachment );
518
+ String contentId = attachment .getContentId ();
486
519
487
520
MimeBodyPart part = new MimeBodyPart ();
488
521
part .setDataHandler (new DataHandler (new ByteArrayDataSource (data , mimeType )));
522
+ if (contentId != null ) {
523
+ part .setContentID (contentId );
524
+ part .setHeader ("Content-Disposition" , "inline" );
525
+ }
489
526
part .setFileName (name );
490
527
multipart .addBodyPart (part );
491
528
}
@@ -668,6 +705,7 @@ public void writeTo(OutputStream outputStream) throws IOException {
668
705
669
706
String name = attachment .getName ();
670
707
String mimeType = attachment .getMimeType ();
708
+ String contentId = attachment .getContentId ();
671
709
byte [] data = readAttachement (attachment );
672
710
673
711
StoragePropertiesChunk attachStorage = new StoragePropertiesChunk ();
@@ -677,6 +715,10 @@ public void writeTo(OutputStream outputStream) throws IOException {
677
715
attachStorage .setProperty (new PropertyValue (MAPIProperty .ATTACH_LONG_FILENAME , FLAG_READABLE | FLAG_WRITEABLE , StringUtil .getToUnicodeLE (name )));
678
716
}
679
717
if (mimeType !=null ) { attachStorage .setProperty (new PropertyValue (MAPIProperty .ATTACH_MIME_TAG , FLAG_READABLE | FLAG_WRITEABLE , StringUtil .getToUnicodeLE (mimeType ))); }
718
+ if (contentId !=null ) {
719
+ attachStorage .setProperty (new PropertyValue (MAPIProperty .ATTACH_CONTENT_ID , FLAG_READABLE | FLAG_WRITEABLE , StringUtil .getToUnicodeLE (contentId ), Types .UNICODE_STRING ));
720
+ attachStorage .setProperty (createLongPropertyValue (MAPIProperty .ATTACH_FLAGS , 4 ));
721
+ }
680
722
attachStorage .setProperty (createLongPropertyValue (MAPIProperty .ATTACH_NUM , attachmentCounter ));
681
723
attachStorage .setProperty (createLongPropertyValue (MAPIProperty .ATTACH_METHOD , 1 )); //ATTACH_BY_VALUE
682
724
attachStorage .setProperty (new PropertyValue (MAPIProperty .ATTACH_DATA , FLAG_READABLE | FLAG_WRITEABLE , data ));
@@ -910,13 +952,15 @@ protected void parseAttachments(MAPIMessage mapiMessage) throws ChunkNotFoundExc
910
952
StringChunk fileName = attachmentChunk .getAttachFileName ();
911
953
ByteChunk data = attachmentChunk .getAttachData ();
912
954
StringChunk mimeType = attachmentChunk .getAttachMimeTag ();
955
+ StringChunk contentId = attachmentChunk .getAttachContentId ();
913
956
914
957
String name = longFileName !=null ? longFileName .getValue () :
915
958
fileName !=null ? fileName .getValue () :
916
959
attachmentChunk .getPOIFSName ();
917
960
InputStream dataIS = data !=null ? new ByteArrayInputStream (data .getValue ()) : null ;
918
961
String mimeTypeVal = mimeType !=null ? mimeType .getValue () : null ;
919
- addAttachment (name , mimeTypeVal , dataIS );
962
+ OutlookMessageAttachment attachment = addAttachment (name , mimeTypeVal , dataIS );
963
+ attachment .setContentId (contentId !=null ? contentId .getValue () : null );
920
964
}
921
965
}
922
966
0 commit comments