@@ -440,6 +440,14 @@ public MimeMessage toMimeMessage(Session session) throws IOException, MessagingE
440
440
441
441
if (plainText ==null && html ==null ) { throw new MessagingException ("missing body" ); }
442
442
443
+ final List <OutlookMessageAttachment > regularAttachments = getAttachments ().stream ()
444
+ .filter (att -> att .getContentId () == null )
445
+ .collect (Collectors .toList ());
446
+
447
+ final List <OutlookMessageAttachment > inlineAttachments = getAttachments ().stream ()
448
+ .filter (att -> att .getContentId () != null )
449
+ .collect (Collectors .toList ());
450
+
443
451
final MimeMultipart multipart = new MimeMultipart ();
444
452
445
453
final List <MimeBodyPart > bodies = new ArrayList <>();
@@ -467,15 +475,44 @@ public MimeMessage toMimeMessage(Session session) throws IOException, MessagingE
467
475
combinedBodiesPart = bodies .get (0 );
468
476
}
469
477
470
- multipart .addBodyPart (combinedBodiesPart );
478
+ if (inlineAttachments .isEmpty ()) {
479
+ multipart .addBodyPart (combinedBodiesPart );
480
+ }
481
+ else {
482
+ final MimeMultipart relatedPart = new MimeMultipart ("related" );
483
+ relatedPart .addBodyPart (combinedBodiesPart );
484
+
485
+ for (OutlookMessageAttachment attachment : inlineAttachments ) {
486
+ String name = attachment .getName ();
487
+ String mimeType = attachment .getMimeType ();
488
+ byte [] data = readAttachement (attachment );
489
+ String contentId = attachment .getContentId ();
490
+
491
+ MimeBodyPart part = new MimeBodyPart ();
492
+ part .setDataHandler (new DataHandler (new ByteArrayDataSource (data , mimeType )));
493
+ part .setContentID (contentId );
494
+ part .setHeader ("Content-Disposition" , "inline" );
495
+ part .setFileName (name );
496
+ relatedPart .addBodyPart (part );
497
+ }
471
498
472
- for (OutlookMessageAttachment attachment : getAttachments ()) {
499
+ final MimeBodyPart relatedBodyPart = new MimeBodyPart ();
500
+ relatedBodyPart .setContent (relatedPart );
501
+ multipart .addBodyPart (relatedBodyPart );
502
+ }
503
+
504
+ for (OutlookMessageAttachment attachment : regularAttachments ) {
473
505
String name = attachment .getName ();
474
506
String mimeType = attachment .getMimeType ();
475
507
byte [] data = readAttachement (attachment );
508
+ String contentId = attachment .getContentId ();
476
509
477
510
MimeBodyPart part = new MimeBodyPart ();
478
511
part .setDataHandler (new DataHandler (new ByteArrayDataSource (data , mimeType )));
512
+ if (contentId != null ) {
513
+ part .setContentID (contentId );
514
+ part .setHeader ("Content-Disposition" , "inline" );
515
+ }
479
516
part .setFileName (name );
480
517
multipart .addBodyPart (part );
481
518
}
@@ -660,6 +697,7 @@ public void writeTo(OutputStream outputStream) throws IOException {
660
697
661
698
String name = attachment .getName ();
662
699
String mimeType = attachment .getMimeType ();
700
+ String contentId = attachment .getContentId ();
663
701
byte [] data = readAttachement (attachment );
664
702
665
703
StoragePropertiesChunk attachStorage = new StoragePropertiesChunk ();
@@ -669,6 +707,10 @@ public void writeTo(OutputStream outputStream) throws IOException {
669
707
attachStorage .setProperty (new PropertyValue (MAPIProperty .ATTACH_LONG_FILENAME , FLAG_READABLE | FLAG_WRITEABLE , StringUtil .getToUnicodeLE (name )));
670
708
}
671
709
if (mimeType !=null ) { attachStorage .setProperty (new PropertyValue (MAPIProperty .ATTACH_MIME_TAG , FLAG_READABLE | FLAG_WRITEABLE , StringUtil .getToUnicodeLE (mimeType ))); }
710
+ if (contentId !=null ) {
711
+ attachStorage .setProperty (new PropertyValue (MAPIProperty .ATTACH_CONTENT_ID , FLAG_READABLE | FLAG_WRITEABLE , StringUtil .getToUnicodeLE (contentId ), Types .UNICODE_STRING ));
712
+ attachStorage .setProperty (createLongPropertyValue (MAPIProperty .ATTACH_FLAGS , 4 ));
713
+ }
672
714
attachStorage .setProperty (createLongPropertyValue (MAPIProperty .ATTACH_NUM , attachmentCounter ));
673
715
attachStorage .setProperty (createLongPropertyValue (MAPIProperty .ATTACH_METHOD , 1 )); //ATTACH_BY_VALUE
674
716
attachStorage .setProperty (new PropertyValue (MAPIProperty .ATTACH_DATA , FLAG_READABLE | FLAG_WRITEABLE , data ));
@@ -888,13 +930,15 @@ protected void parseAttachments(MAPIMessage mapiMessage) throws ChunkNotFoundExc
888
930
StringChunk fileName = attachmentChunk .getAttachFileName ();
889
931
ByteChunk data = attachmentChunk .getAttachData ();
890
932
StringChunk mimeType = attachmentChunk .getAttachMimeTag ();
933
+ StringChunk contentId = attachmentChunk .getAttachContentId ();
891
934
892
935
String name = longFileName !=null ? longFileName .getValue () :
893
936
fileName !=null ? fileName .getValue () :
894
937
attachmentChunk .getPOIFSName ();
895
938
InputStream dataIS = data !=null ? new ByteArrayInputStream (data .getValue ()) : null ;
896
939
String mimeTypeVal = mimeType !=null ? mimeType .getValue () : null ;
897
- addAttachment (name , mimeTypeVal , dataIS );
940
+ OutlookMessageAttachment attachment = addAttachment (name , mimeTypeVal , dataIS );
941
+ attachment .setContentId (contentId !=null ? contentId .getValue () : null );
898
942
}
899
943
}
900
944
0 commit comments