Error "Cannot read the item" when opening an MSG attachment of a PDF file

Hello,

Our goal is to convert a MSG file to PDF, and keep the MSG attachments as attachments of the generated PDF, so far so good.
The PDF is created and the original email attachments are in the PDF attachments list as you can see in this screenshot:
screenshot of genereted PDF with attchments.png (58.2 KB)

I also uploaded the PDF it self so you can check:
FinalPDFwithAttachments.pdf (1.2 MB)

The problem is that in the generated PDF if you go to the attachments list and try to open a MSG file it gives an error “Cannot read the item” even if download the attachment and try to open it from file system. The other file extensions attachments work fine.

Is this a known bug? Can you please verify whats wrong?

Here is the code used:

    // Get MSG file bytes
    byte[] fileContent = System.IO.File.ReadAllBytes(sourceFileFullPath);

    List<Aspose.Email.Mapi.MapiAttachment> mailAttachments = new List<Aspose.Email.Mapi.MapiAttachment>();
    Aspose.Email.Mapi.MapiMessage messageMapi;

    using (System.IO.MemoryStream memStream = new System.IO.MemoryStream(fileContent))
    {
        //Create an instance of MapiMessage and load an email file
        messageMapi = Aspose.Email.Mapi.MapiMessage.Load(memStream);
    }

    using (System.IO.MemoryStream msOut = new System.IO.MemoryStream())
    {
        using (var msgStream = new System.IO.MemoryStream())
        {
            // Setup the email attchments
            Aspose.Email.Mapi.MapiAttachmentCollection attachments = messageMapi.Attachments;
            if (attachments.Count > 0)
            {
                for (int index = 0; index < attachments.Count; index++)
                {
                    Aspose.Email.Mapi.MapiAttachment attachment = attachments[index];
                    if (IsInlineAttachment(attachment, messageMapi.BodyType))
                    {
                        // workaround to force inline attachment to be embedded instread of exporting it in output MHTML
                        attachment.SetProperty(new Aspose.Email.Mapi.MapiProperty(Aspose.Email.Mapi.MapiPropertyTag.PR_ATTACH_METHOD, BitConverter.GetBytes((long)5))); // file embedded in a MSG contains a PR_ATTACH_METHOD whose value equals 5
                    }
                    else
                    {
                        mailAttachments.Add(attachment);
                    }
                }
            }

            Aspose.Email.MhtSaveOptions mhtSaveOptions = new Aspose.Email.MhtSaveOptions
            {
                SaveAttachments = false // this avoids exporting attachment in output MHTML
            };

            // Save email changes 
            messageMapi.Save(msgStream, mhtSaveOptions);

            msgStream.Position = 0;

            Aspose.Words.Loading.HtmlLoadOptions htmlLoadOptions = new Aspose.Words.Loading.HtmlLoadOptions();
            htmlLoadOptions.LoadFormat = Aspose.Words.LoadFormat.Mhtml;
            htmlLoadOptions.WebRequestTimeout = 2000; // 2s timeout
            //htmlLoadOptions.ResourceLoadingCallback = new ExternalResourceLoader(loadExternalResources);
            var document = new Aspose.Words.Document(msgStream, htmlLoadOptions);

            // Save word document MHTML
            document.Save(msOut, new Aspose.Words.Saving.PdfSaveOptions());
            
            if (mailAttachments.Count > 0)
            {
                Aspose.Pdf.Document pdfDocument = new Aspose.Pdf.Document(msOut);
                
                foreach (Aspose.Email.Mapi.MapiAttachment pdfAttachment in mailAttachments)
                {
                    // Get attachment bytes
                    byte[] data = pdfAttachment.BinaryData != null ? pdfAttachment.BinaryData : pdfAttachment.ObjectData.Data;

                    using (System.IO.Stream attachmentContentStream = new System.IO.MemoryStream(data))
                    {
                        // Setup new file to be added as attachment to the main PDF
                        Aspose.Pdf.FileSpecification pdfEmbeddedFile = new Aspose.Pdf.FileSpecification(attachmentContentStream, pdfAttachment.LongFileName);

                        // Add attachment to document's attachment collection
                        pdfDocument.EmbeddedFiles.Add(pdfEmbeddedFile);
                    }
                }

                // Save the PDF with the attchments
                pdfDocument.Save(msOut);
            }
        }

        System.IO.File.WriteAllBytes("d:\\output.pdf", msOut.ToArray());
    }

I also upload a sample project where you can replicate the problem:
PdfAttachments.zip (16.8 KB)

And the original MSG file to be converted to PDF with attachments:
MSG file to convert to PDF.zip (3.3 MB)

@duvidasAspose

We are checking it and will get back to you shortly.

Hello,

Do you have any news on this issue?

Thanks

@duvidasAspose
asad.ali has no opportunity to answer yet. I will look at the problem and write to you today. I’ll try in the next few hours.

1 Like

@duvidasAspose

when we saved .msg attachment of attached PDF file and try to open it (separate file on disk) - it didn’t open. Either the msg file itself is incorrect, or it was somehow incorrectly attached. Nevertheless, an investigation ticket as PDFNET-53936 has been logged in our issue tracking system for further analysis. We will let you know once it is resolved. Please spare us some time.

1 Like

Hello,

More than a year has passed, any updates about this issue?

Thanks

@duvidasAspose

We are afraid that the ticket could not get resolved due to other pending issues in the queue. However, we have recorded your concerns and will surely inform you as soon as we make some definite progress towards ticket resolution. We apologize for the delay and the inconvenience.