Setting and Removing a Property from Attachment

Hi,

I hav questino. What is the use of setting Property on an Attachment and how can I do this?

Hi Aaron,

Thank you for contacting Aspose support team.

Message attachments can have variety of properties for adding different characteristics or additional information. This message can be saved (e.g. in PST) and then can be retrieved as well along with the message attachment. You may please consider the following example where properties are attached with the attachment and then message is saved and retrieved from the PST. You can see that the newly attached property is retrieved from the message attachment in PST.

static void Email_420()
{
    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.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.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]);
                    
                    //Remove properties here
                    attachedMessage.RemoveProperty(MapiPropertyTag.PR_ATTACH_METHOD);
                    attachedMessage.RemoveProperty(MapiPropertyTag.PR_RECIPIENT_TYPE);
                }
            }
        }
    }
}