I am trying to create an inline image attachment in a MapiMessage object and then store it in a PST file.
When I set the AttachContentId property on a MapiAttachment object, the attachment is not visible either inline or the attachments pane in outlook client (on mac).
I’ve verified that setting the AttachmentHidden property to true does not help.
Could you please share all the required properties to be set for an inline attachment added as a mapi attachment to a mapi message added to a PST file?
var msg = MapiMessage.FromMailMessage(
<mail message object>,
MapiConversionOptions.UnicodeFormat
);
msg.Attachments.Add("test.png", <A byte array from the image>);
var attach = msg.Attachments.Last<MapiAttachment>();
attach.SetProperty(
KnownPropertyList.AttachContentId,
"some-content-id"
);
attach.SetProperty(
KnownPropertyList.AttachMimeTag,
"image/png"
);
attach.SetProperty(
KnownPropertyList.LastModificationTime,
DateTime.Now()
);
attach.SetProperty(
KnownPropertyList.AttachmentHidden,
true // setting to either true or false doesn't work
);
// https://docs.aspose.com/email/java/differentiate-between-inline-and-regular-attachments/
attach.SetProperty(
KnownPropertyList.AttachFlags,
0x00000004
);
attach.SetProperty(
new MapiProperty(
0x3716001E // Even tried 0x3716001F
).Descriptor,
"inline"
);
The content id set in property AttachContentId is used in the message body with bodyContentType being BodyContentType.Html.
Setting the AttachContentId has two effects:
The image attachment is not shown on the preview pane of the message on the outlook client.
The image is not embedded inline in the message.
I’m not sure what else needs to be set to mark the attachment as inline and make it available as part of the message body.
Note:
Setting AttachContentId on any image attachment (even if it’s not inline) hides the attachment from the attachment preview pane in outlook client. Screen Shot 2023-03-21 at 8.39.10 PM.jpg (175.0 KB)
Removing the extension (.png in this case) makes the attachment appear on the attachment preview pane in outlook client but it appears as a text file even when the AttachMimeTag is set. Screenshot 2023-03-23 at 10.19.30 AM.jpg (244.3 KB)
Please try the following code to create a message with inline attachment:
// create message and set html body
MapiMessage msg = new MapiMessage("from@from.com","to@to.com", "Test message", "");
msg.SetBodyContent(@"<!DOCTYPE html>
<html>
<head>
</head>
<body>
<h1>Test with image</h1>
<img src=""cid:attach1234"">
<p>Test with image</p>
</body>
</html>", BodyContentType.Html);
// add some image from file as inline
attachment msg.Attachments.Add("image.jpg", File.ReadAllBytes(@"D:\image.jpg"));
msg.Attachments[0].SetProperty(KnownPropertyList.AttachContentId, "attach1234");
msg.Attachments[0].SetProperty(KnownPropertyList.AttachmentHidden, true);
// save message
msg.Save(@"D:\test1.msg");
Oh sorry, I forgot to upload the screenshot. PFA.
The issue is that the inline attachment, is not being displayed in the outlook client as an image, but just as [cid:attach1234] (Based on the example you posted).
In the attached file MsgToPst.zip (96.6 KB)
you can find the project of a sample application that creates a message containing an inline image in the body and adds such a message to pst.
All you have to do is:
unzip the file
open and run the project in visual studio
open the pst file from the output folder in the project root in Outlook and make sure that the message is displayed with the image.
When KnownPropertyList.AttachContentId is set, the attachment is neither displayed at the top in the attachments preview pane or inline. But there is some indication that the attachment exists (Notice the clip icon above the time in the email list on the left of the screenshot above).
When I don’t set the KnownPropertyList.AttachContentId property, the attachment is shown in the attachment preview but not as inline attachment (Screenshot 2023-04-18 at 9.34.04 AM.jpg (203.6 KB)
)
Setting the KnownPropertyList.AttachmentHidden property does not make a difference in either case.
Please let me know if we can have a call for this discussion.
It would be very helpful to get this resolved asap.
As you have been informed recently, we have created an investigation ticket on your issue. As soon as the problem is solved, we will provide you with detailed comments and a possible solution.
We are sorry to keep you waiting. Thanks for your understanding and patience.
Does this also address the issue of HTML itself not being rendered correctly?
Any of the styling was also not rendered properly on the Outlook for Mac app.
Please let me know what is the issue and the expected time for the fixes.
The investigation of your issue is still in progress. We’re facing the difficulty to reproduce it in environment similar to yours. It works with Windows just fine.
If you want to open the message properly on MAC you need to use the compress option with body content:
// create message and set html body
MapiMessage msg = new MapiMessage("from@from.com","to@to.com", "Test message", "");
msg.SetBodyContent(@"<!DOCTYPE html>
<html>
<head>
</head>
<body>
<h1>Test with image</h1>
<img src=""cid:attach1234"">
<p>Test with image</p>
</body>
</html>", BodyContentType.Html, true); //<<
// compression(true) - Specify that the content should be compressed.