May 15 2023 06:52 AM
May 15 2023 06:52 AM
I am using the method “Attachments.Add(Object, Object, Object, Object)” to add attachments to a new email, but the DisplayName parameter is not working was supposed.
By DisplayName object definition, if the mail item is in Plain Text or HTML format, then the attachment is displayed using the file name in the Source parameter. But in my case, the mail item is in HTML format and the attachment is displayed using the DisplayName.
When will Microsoft solve this?
See the DisplayName object definition here:
Thanks & regards,
May 15 2023 01:03 PM
The `DisplayName` parameter in the `Attachments.Add` method is not used to control the display name of the attachment within the email body. Instead, it represents the name of the attachment file itself. The display name of the attachment within the email body is typically derived from the file name specified in the `Source` parameter.
If you want to control the display name of the attachment within the email body, you can modify the `Content-Disposition` header of the attachment. Here's an example of how you can achieve this:
Outlook.Application outlookApp = new Outlook.Application(); Outlook.MailItem mailItem = (Outlook.MailItem)outlookApp.CreateItem(Outlook.OlItemType.olMailItem); mailItem.Subject = "Email Subject"; mailItem.HTMLBody = "<html><body>Email Body</body></html>"; // Attach a file with a custom display name string attachmentPath = "C:\\Path\\To\\Attachment.pdf"; Outlook.Attachment attachment = mailItem.Attachments.Add(attachmentPath, Outlook.OlAttachmentType.olByValue, 1, "Custom Display Name"); // Modify the Content-Disposition header to set the desired display name attachment.PropertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/proptag/0x3712001F", "New Display Name"); // Send or display the email mailItem.Display();
In the code snippet above, the `Attachment` object's `PropertyAccessor` is used to access the `Content-Disposition` header property (`0x3712001F`). By setting the value of this property, you can control the display name of the attachment within the email body.
Please note that this approach uses the Outlook interop library and is specific to Outlook desktop applications. If you are working with a different email client or using a different method to send emails, the attachment display name behavior may vary.
If I have answered your question, please mark your post as Solved
If you like my response, please give it a like
May 16 2023 12:19 AM
I do not want to control the display name of the attachment, I just want is behavior as in definition that Microsoft gives: “if the mail item is in Plain Text or HTML format, then the attachment is displayed using the file name in the Source parameter”.
The point is: I need to use the DisplayName property to save other information that is not supposed to be displayed.