Handling attachments and Embedded content in Body of the Msg file read by IndependentSoft MSG.NET package
As we know that the MSG.NET package by IndependentSoft is used for creating, parsing, reading and converting the Msg email without requiring Outlook to be Installed on System.
The link http://www.independentsoft.de/msg/tutorial/readmessage.html shows how to parse and read the msg file by passing the path of the msg file.
To read the msg file parsed by the package in a proper format, there are two important points regarding attachments that must be handled :
1.Differentiating the attachments and embedded content in Body Text From Attachments List Object of parsed Msg File By IndependentSoft MSG.NET package
The Most important part, in this case, is to handle the attachments and the embedded content when the .msg file is parsed. Attachments and the embedded content like inline images are not parsed into different Objects, these are parsed as single Attachments List Object when the path of the msg file is passed to the Message Class constructor of the IndependentSoft MSG.NET package.
But there are some different conditions that we can apply to handle the attachments Object to display the actual list of file attachments and insert the list of files of embedded content into Body text at its actual place to show the actual view of Msg file in our Custom Viewer.
Screenshot below shows the custom Viewer that displays the parsed msg file in custom viewer created in an ASP.net C# web application :
On click of attachment, image file opens in the next tab of browser as specified in the c# custom code.
Screenshot of Visual studio debugging window below shows the attachments List Object (including both the actual attachments and embedded images/files in the body text) :
Screenshot below shows the list of attachments on expansion of the attachment list Object.
Screenshot of the Visual studio Debugging window shows the Null value for Content Id in the ContentId field of the file which is embedded in the body text in the attachment Object On expansion :
Screenshot of the Visual studio Debugging window below shows the Null value for Content Id for the case it is the Actual attached file in email :
Content Id Value in the body Text is formatted as :
cid:<content_id>
So we replace the content id of the image/file by inserting the image at the time of displaying the body in Custom Viewer Page.
At one exceptional place, Content Id for one of the actual file attachment was not null, (But that contentid was also not existing in the body text place).
So I added the following general condition in C# code to find the list of actual attachments :
foreach (var v in message.Attachments) { if (v.ContentId == "" || v.ContentId==null || !(this.MessageBody.Content.Contains("cid:" + v.ContentId))){ // Add your code to display the hyperlinks in the panel box } }
2. Handling Outlook Item attachments :
There can be cases when the outlook items itself is attached inside the msg file for eg a contact outlook item is attached in the msg file. In this case, attachment object is not parsed normally as other files like image files, text files are read by the package. In case of outlook item, instead, it is further parsed by the package. Screenshot below of the custom viewer shows the outlook items – contact type and email type attached to my original email type msg file.
Screenshot below shows the Original email file in outlook :
Debugging window screenshots in Visual studio below describes the difference between the outlook item attachment and other file attachments like images and text
files:
Red colored rectangles in the screenshot below for the first attachment object which is a
pdf file shows the name of the file in the fields like ‘Filename’ ‘DisplayName’ And ‘EmbeddedMessage’ field in this is empty. Extension field, in this case, contains the ‘.pdf’:
But in case of outlook item, the outlook item is further parsed by the MSG.NET package into the Embedded message object field as the original msg file is parsed.
Screenshot below shows that embedded message is not null. And also the file name field and long file name does not contain extension with the filename. And Extension field is null in this case :
Screenshot below shows how the contact type outlook item is further parsed by the Independentsoft Msg.Net package :
As I had a requirement to download the attached msg file on click of the link in the attachments panel that contains hyperlink of msg file, So I need to create the msg file from the ‘EmbeddedMessage’ object. We can handle the outlook attachment in this case by creating the msg file on our machine by using the ‘save’ method from the MSG.Net package message class explained at the link http://www.independentsoft.de/msg/tutorial/createmessage.html.
message.Save("c:\\temp\\message.msg", true);
Note: Directly Saving the embedded message to create a new msg file will not work in this case. It will throw ‘Can not read the file’ error if you try to open this file:
This happens because when the outlook item attachment is parsed by the package, the flag ‘IsEmbedded’ is set to true, you need to set this flag to false manually. After setting the flag to false , if you will save the embedded message as msg file, it will be saved correctly:
Below General Code for creating the msg file from the embedded Message :
Message message = new Message(filePath); foreach (var v in message.Attachments) { --------------------// your code if (v.EmbeddedMessage != null){ Message newmessage= new Message(); newmessage = (Message)v.EmbeddedMessage; newmessage.IsEmbedded = false; // set the flag to false newmessage.Save(attachFilePath, true); // Create Msg file from embedded message } }
On click of Hyperlink , the file can be made downloadable using the Http response method by setting the response content type as
response.ContentType = “APPLICATION/OCTET-STREAM”;
Great post