How to replace embedded message content in the MapiMessage with RTF body?

Scenario: I open MSG file (sample.msg) with embedded message; save embedded message and read it with MapiMessage.FromFile method; do some work and save embedded message to the file. Then I need to replace original attachment on sample.msg without changing the order of attachments in RTF body.


Question:
How to replace attachment content (embedded message).

I tried to use SetProperty-method (see code sample below), but it does not work - cannot open attachment after re-saving main message (sample-resaved.msg).

Code sample:
var mapi0 = MapiMessage.FromFile(@“sample.msg”);
mapi0.Attachments[0].Save(@“att.msg”);

//work with embedded message
var emb = MapiMessage.FromFile(@“att.msg”);
//…
emb.Save(@“att-resaved.msg”);

// replace attachment content
mapi0.Attachments[0].SetProperty(new MapiProperty(MapiPropertyTag.PR_ATTACH_DATA_OBJ, File.ReadAllBytes(@“att-resaved.msg”)));

mapi0.Save(@“sample-resaved.msg”); // this file will contain bad attachment.
// See screenshot “after SetProperty.png”
-----------------------------------------------------------------------
I cannot use Add(string, MapiMessage) method, because attachment order will be changed in the message body (RTF). See screenshot “After Add method.png”.

Thanks,
Alex Shloma

Hi Alex,

You may please try replacing BinaryData byte array to replace the attachment contents and let us know the feedback. It does not change attachments order and also the newly replaced attachment can be opened successfully.

String path = @"\";
var mapi0 = MapiMessage.FromFile(path + @"sample.msg");
mapi0.Attachments[0].Save(path + @"att.msg");

//work with embedded message
var emb = MapiMessage.FromFile(path + @"att.msg");
//....
emb.Save(path + @"att-resaved.msg");

// replace attachment content
//mapi0.Attachments[0].SetProperty(new MapiProperty(MapiPropertyTag.PR_ATTACH_DATA_OBJ, File.ReadAllBytes(path + @"att-resaved.msg")));

MemoryStream memStr_att_resaved = new MemoryStream();
MapiMessage att_resaved = MapiMessage.FromFile(path + @"att-resaved.msg");
att_resaved.Save(memStr_att_resaved);

mapi0.Attachments[0].BinaryData = ReadAllBytes(memStr_att_resaved);
mapi0.Save(path + @"sample-resaved.msg"); // this file will contain ok attachment.

This code sample does not work.


0th attachment is embedded message and ObjectData must be used. If you make some changes in emb object (see code below); set BinaryDate as in your code sample and open resulting sample-resaved.msg message in Outlook, you will see no changes in attachment.

emb.SenderName = “Name”;
emb.SenderEmailAddress = “email@address”;
emb.SentRepresentingName = “SentRepresentingName”;
emb.Attachments.Add(“t.txt”, File.ReadAllBytes(@“text.txt”));

Thanks,
Alex Shloma

Hi Alex,


I have logged an enhancement ticket to provide a method for replacing embedded attachment message contents under Id: EMAILNET-35095 for further investigation by the product team. I shall write here as soon as some feedback is received in this regard.

The issues you have found earlier (filed as EMAILNET-35095) have been fixed in this update.


This message was posted using Notification2Forum from Downloads module by Aspose Notifier.
Hi,

Please provide code sample which demonstrates new API.

Thanks,
Alex Shloma

Hi Alex,

Article links are provided in the release notes on the download page. You may please visit the “Replace Embedded MSG Attachments Contents” section in this article and let us know the feedback.