Cannot set DisplayName for attached msg (attached to an msg)

We must replace some of the attachments of an msg. When we remove and insert a new msg attachment to an msg, we find no way to set the DisplayName of the attachment, the final msg has an attachment with the DisplayName equal to the FileName. Can you help us change the DisplayName of a replaced attachment?

Snippet: to read the original DisplayName from the container msg

            string longFileName;
            string displayName;
            using var msgFileStream = File.OpenRead(path);
            using (MapiMessage message = MapiMessage.Load(msgFileStream))
            {
                MapiAttachment mapiAttachment = message.Attachments[0];
                longFileName = mapiAttachment.LongFileName;
                displayName = mapiAttachment.DisplayName;
            }

Snippet: to replace the contained msg attachment

using (MailMessage message = MailMessage.Load(msgFileStream))
{
var replacementContent = Stream.Null;//Stream with content to replace
using var replacementAttachment = new Aspose.Email.Attachment(replacementContent, Path.GetFileName(longFileName));
**[//replacementAttachment.DisplayName](https://replacementattachment.displayname/) = displayName;//ā€˜Attachmentā€™ does not contain a definition for 'DisplayNameā€™**
message.Attachments.RemoveAt(0);
message.Attachments.Insert(0, replacementAttachment);
}

If we add it as a MapiMessage, the DisplayName is readonly.

Thanks for your help!

@Macarea_Dabbah

Could you please ZIP and attach your input and expected output message files here for our reference? We will then provide you more information on it.

@tahir.manzoor thanks for the prompt response!

To reproduce this issue you can follow the steps below:

  1. Create with Outlook a new email file. Subject ā€œAttached email subjectā€. Save the message as "Attached_email_subject.msg"

  2. Create with Outlook a new email. Attach the file ā€œAttached_email_subject.msgā€. Save the message as "container_email.msg"

  3. Run this code snippet

            // Original File                        
             using var originalMsgFileStream = File.OpenRead(Path.Join(path, "container_email.msg"));
             using (MapiMessage originalMessage = MapiMessage.Load(originalMsgFileStream))
             {
                 Console.WriteLine($"Original DisplayName: {originalMessage.Attachments[0].DisplayName}");
                 Console.WriteLine($"Original LongFileName: {originalMessage.Attachments[0].LongFileName}");
             }
    
             // Replace attachment contents
             var afterReplacementFileName = Path.Join(path, "container_email_after_replacement.msg");
             using (MailMessage message = MailMessage.Load(originalMsgFileStream))
             {
                 var replacementContent = Path.Join(path, "replacement_email.msg");
                 using var replacementAttachment = new Aspose.Email.Attachment(replacementContent, Path.GetFileName(replacementContent));
                 //replacementAttachment.DisplayName = displayName;//'Attachment' does not contain a definition for 'DisplayName'
                 message.Attachments.RemoveAt(0);
                 message.Attachments.Insert(0, replacementAttachment);
    
                 Console.WriteLine($"While replacing Name {message.Attachments[0].Name}");                                
    
                 using (var outs = new MemoryStream())
                 {
                     message.Save(outs, SaveOptions.CreateSaveOptions(MailMessageSaveType.OutlookMessageFormat));                    
                     File.WriteAllBytes(afterReplacementFileName, outs.ToArray());
                 }
             }
    
             // Parse the replaced file
             using var afterReplacementFileStream = File.OpenRead(afterReplacementFileName);
             using (MapiMessage message = MapiMessage.Load(afterReplacementFileStream))
             {
                 Console.WriteLine($"After replacement DisplayName: {message.Attachments[0].DisplayName}");
                 Console.WriteLine($"After replacement LongFileName: {message.Attachments[0].LongFileName}");
             }
    

You will get the following output:

Original DisplayName: Attached email subject
Original LongFileName: Attached_email_subject.msg
While replacing Name replacement_email.msg
After replacement DisplayName: **replacement_email.msg**
After replacement LongFileName: replacement_email.msg

The desired output is:
Original DisplayName: Attached email subject
Original LongFileName: Attached_email_subject.msg
While replacing Name replacement_email.msg
After replacement DisplayName: replacement email
After replacement LongFileName: replacement_email.msg

  1. Open the replacement_email.msg. You will see the attachment display name to be ā€œreplacement_email.msgā€ instead of ā€œreplacement emailā€.

Here are some screenshots:

@Macarea_Dabbah

You can use Attachment.Name property as shown below to get the desired output.

replacementAttachment.Name = "New display name of attachment.msg";

@tahir.manzoor the proposed solution has side effects: the filename of the attachment is modified, it is the exact same as the ā€œNameā€ we set; the final email has an attachment with a filename that has no extension. We need to only modify the DisplayName (and keep the original the FileName). The proposal may also have more side effects: if the attachment is an email, the email subject may contain invalid path characters and when saving, it will have issues. Please find below the evidences.

Code snippet:
// Original File
using var originalMsgFileStream = File.OpenRead(Path.Join(path, ā€œcontainer_email.msgā€));
using (MapiMessage originalMessage = MapiMessage.Load(originalMsgFileStream))
{
Console.WriteLine($ā€œOriginal DisplayName: {originalMessage.Attachments[0].DisplayName}ā€);
Console.WriteLine($ā€œOriginal LongFileName: {originalMessage.Attachments[0].LongFileName}ā€);
}

            // Replace attachment contents
            var afterReplacementFileName = Path.Join(path, "container_email_after_replacement.msg");
            using (MailMessage message = MailMessage.Load(originalMsgFileStream))
            {
                var replacementContent = Path.Join(path, "replacement_email.msg");
                using var replacementAttachment = new Aspose.Email.Attachment(replacementContent, Path.GetFileName(replacementContent));
                //replacementAttachment.DisplayName = displayName;//'Attachment' does not contain a definition for 'DisplayName'
                replacementAttachment.Name = "Some fantastic display name";
                message.Attachments.RemoveAt(0);
                message.Attachments.Insert(0, replacementAttachment);

                Console.WriteLine($"While replacing Name {message.Attachments[0].Name}");                                

                using (var outs = new MemoryStream())
                {
                    message.Save(outs, SaveOptions.CreateSaveOptions(MailMessageSaveType.OutlookMessageFormat));                    
                    File.WriteAllBytes(afterReplacementFileName, outs.ToArray());
                }
            }

            // Parse the replaced file
            using var afterReplacementFileStream = File.OpenRead(afterReplacementFileName);
            using (MapiMessage message = MapiMessage.Load(afterReplacementFileStream))
            {
                Console.WriteLine($"After replacement DisplayName: {message.Attachments[0].DisplayName}");
                Console.WriteLine($"After replacement LongFileName: {message.Attachments[0].LongFileName}");
            }

Output:

 Original DisplayName: Attached email subject
Original LongFileName: Attached email subject.msg
While replacing Name Some fantastic display name
After replacement DisplayName: Some fantastic display name
After replacement LongFileName: **Some fantastic display name**

Thanks for your help!

@Macarea_Dabbah

We have logged this problem in our issue tracking system as EMAILNET-40735. We will inform you once there is an update available on it.

We apologize for your inconvenience.

thanks @tahir.manzoor! Looking forward to a fix :slight_smile:

@Macarea_Dabbah

We have added MapiAttacment.DisplayName property in Aspose.Email APIs. This feature will be available in Aspose.Email for .NET 22.10. We will inform you once it is released.

MapiMessage msg = MapiMessage.Load(fileName);
msg.Attachments[0].DisplayName = "New display name 1";
msg.Attachments[1].DisplayName = "New display name 2";
1 Like

Thatā€™s fantastic, thanks!