Problem with attachment- recipient mapi properties in message attached to another message

I have problem with mapi properties of attachments/recipients which are in message attached to another message. The following code illustrates this situation:


using (PersonalStorage pst = PersonalStorage.Create(@“c:\test.pst”, FileFormatVersion.Unicode))
{
using (MapiMessage mapiMessage = new MapiMessage())
{
using (MapiMessage attachedMessage = new MapiMessage())
{
attachedMessage.Attachments.Add(“attachment”, new byte[0]);
MapiAttachment attachment = attachedMessage.Attachments[0];
attachment.Properties[MapiPropertyTag.PR_ATTACH_METHOD] = new MapiProperty(MapiPropertyTag.PR_ATTACH_METHOD, new byte[] { 5, 0, 0, 0 });

attachedMessage.Recipients.Add(“recipient”, string.Empty, MapiRecipientType.Unknown);
MapiRecipient mapiRecipient = attachedMessage.Recipients[0];
mapiRecipient.Properties[MapiPropertyTag.PR_RECIPIENT_TYPE] = new MapiProperty(MapiPropertyTag.PR_RECIPIENT_TYPE, new byte[] { 1, 0, 0, 0 });

Console.WriteLine(“Before save:”);
Console.WriteLine("PR_ATTACH_METHOD: " + attachedMessage.Attachments[0].Properties[MapiPropertyTag.PR_ATTACH_METHOD]);
Console.WriteLine("PR_RECIPIENT_TYPE: " + attachedMessage.Recipients[0].Properties[MapiPropertyTag.PR_RECIPIENT_TYPE]);

mapiMessage.Attachments.Add(“attachedMessage”, attachedMessage);
}

pst.RootFolder.AddMessage(mapiMessage);
}
}

using (PersonalStorage pst = PersonalStorage.FromFile(@“c:\test.pst”))
{
foreach (MapiMessage mapiMessage in pst.RootFolder.EnumerateMapiMessages())
{
using (MemoryStream memoryStream = new MemoryStream())
{
mapiMessage.Attachments[0].Save(memoryStream);

using (MapiMessage attachedMessage = MapiMessage.FromStream(memoryStream))
{
Console.WriteLine(“After load:”);
Console.WriteLine("PR_ATTACH_METHOD: " + attachedMessage.Attachments[0].Properties[MapiPropertyTag.PR_ATTACH_METHOD]);
Console.WriteLine("PR_RECIPIENT_TYPE: " + attachedMessage.Recipients[0].Properties[MapiPropertyTag.PR_RECIPIENT_TYPE]);
}
}
}
}

Before saved mapi properties have correct values:
PR_ATTACH_METHOD: 5
PR_RECIPIENT_TYPE: 1

and after load they are incorrect:
PR_ATTACH_METHOD: 1
PR_RECIPIENT_TYPE: 4294967295

Hi Maciej,

Thank you for posting your inquiry.

This behavior is due to the wrong usage of property setting. Please use the SetProperty method to set these properties as shown in the following code sample:

Code:

File.Delete("test.pst");

using (PersonalStorage pst = PersonalStorage.Create(@"test.pst", FileFormatVersion.Unicode))
{
    using (MapiMessage mapiMessage = new MapiMessage())
    {
        using (MapiMessage attachedMessage = new MapiMessage())
        {
            attachedMessage.Attachments.Add("attachment", new byte[0]);
            MapiAttachment attachment = attachedMessage.Attachments[0];

            // attachment.Properties[MapiPropertyTag.PR_ATTACH_METHOD] = new MapiProperty(MapiPropertyTag.PR_ATTACH_METHOD, new byte[] { 5, 0, 0, 0 });
            attachment.SetProperty(new MapiProperty(MapiPropertyTag.PR_ATTACH_METHOD, BitConverter.GetBytes((long)5)));

            attachedMessage.Recipients.Add("recipient", string.Empty, MapiRecipientType.Unknown);
            MapiRecipient mapiRecipient = attachedMessage.Recipients[0];

            // mapiRecipient.Properties[MapiPropertyTag.PR_RECIPIENT_TYPE] = new MapiProperty(MapiPropertyTag.PR_RECIPIENT_TYPE, new byte[] { 1, 0, 0, 0 });
            mapiRecipient.SetProperty(new MapiProperty(MapiPropertyTag.PR_RECIPIENT_TYPE, BitConverter.GetBytes((long)1)));

            Console.WriteLine("Before save:");
            Console.WriteLine("PR_ATTACH_METHOD: " + attachedMessage.Attachments[0].Properties[MapiPropertyTag.PR_ATTACH_METHOD]);
            Console.WriteLine("PR_RECIPIENT_TYPE: " + attachedMessage.Recipients[0].Properties[MapiPropertyTag.PR_RECIPIENT_TYPE]);

            mapiMessage.Attachments.Add("attachedMessage", attachedMessage);
        }

        pst.RootFolder.AddMessage(mapiMessage);
    }
}

using (PersonalStorage pst = PersonalStorage.FromFile(@"test.pst"))
{
    foreach (MapiMessage mapiMessage in pst.RootFolder.EnumerateMapiMessages())
    {
        using (MemoryStream memoryStream = new MemoryStream())
        {
            mapiMessage.Attachments[0].Save(memoryStream);

            using (MapiMessage attachedMessage = MapiMessage.FromStream(memoryStream))
            {
                Console.WriteLine("After load:");
                Console.WriteLine("PR_ATTACH_METHOD: " + attachedMessage.Attachments[0].Properties[MapiPropertyTag.PR_ATTACH_METHOD]);
                Console.WriteLine("PR_RECIPIENT_TYPE: " + attachedMessage.Recipients[0].Properties[MapiPropertyTag.PR_RECIPIENT_TYPE]);
            }
        }
    }
}

Thank you for your answer Muhammad, it helped me a lot. However, I have one important question to your changes in my code. Why you casting both property values to long? PR_ATTACH_METHOD (https://msdn.microsoft.com/pl-pl/library/bb415598.aspx ) and PR_RECIPIENT_TYPE (PR_RECIPIENT_TYPE | Microsoft Learn) are PT_LONG/PT_I4 and takes 4 bytes ([http://www.aspose.com/docs/display/emailnet/Aspose.Email.Formats.Outlook.Msg.MapiType+Enumeration ](http://www.aspose.com/docs/display/emailnet/Aspose.Email.Formats.Outlook.Msg.MapiType+Enumeration)). Long takes 8 bytes:

BitConverter.GetBytes((long)5) = new byte[] { 5, 0, 0, 0, 0, 0, 0, 0 }

Hi Maciej,


Thank you for sharing your feedback.

It seems that is related to the API implementation as if I check the PR_ATTACH_METHOD and PR_RECIPIENT_TYPE properties and both these are of type long in the API. You can check this in the API’s CHM documentation and use these as per the specifications. Please share with us if you have further inquiry.