Without this replacement, certain required text in the email body is omitted in the PDF output.
We are aware that this workaround operates directly on the MailMessage.Body before MHTML generation, but we suspect the actual cause lies in a transformation step beforehead.
Could you please clarify:
In which step or component is the -aw-import:ignore tag inserted into the email body?
How can we identify steps where the tag has beed added to the email body?
How can we prevent this tag from being inserted so that no manual string replacement is required?
Our intent is to ensure the PDF output contains all required email body content without losing text due to unwanted processing instructions.
Minimal Code Example
using System;
using System.IO;
using Aspose.Email;
using Aspose.Email.Mime;
using Aspose.Email.Mht;
using Aspose.Words;
using Aspose.Words.Loading;
using Aspose.Words.Saving;
namespace EmailToPdfTest
{
class Program
{
static void Main(string[] args)
{
// Paths to test files
string sourcePath = @"C:\Temp\TestEmail.eml";
string targetPath = @"C:\Temp\TestEmail.pdf";
// Load EML using Aspose.Email
MailMessage mailMsg = MailMessage.Load(sourcePath);
// Workaround: Remove "-aw-import:ignore" from body
// Without this, certain text in the body disappears in PDF
mailMsg.Body = mailMsg.Body.Replace("-aw-import:ignore", "");
// Save email as MHTML in memory using Aspose.Email
MhtSaveOptions mhtSaveOptions = SaveOptions.DefaultMhtml;
using (MemoryStream ms = new MemoryStream())
{
mailMsg.Save(ms, mhtSaveOptions);
// Reset stream position before Aspose.Words load
ms.Position = 0;
// Load the MHTML into Aspose.Words
LoadOptions loadOptions = new LoadOptions
{
LoadFormat = LoadFormat.Mhtml
};
Document doc = new Document(ms, loadOptions);
// Ensure Page Setup (optional)
foreach (Section section in doc.Sections)
{
section.PageSetup.PaperSize = PaperSize.A4;
}
// Save PDF using Aspose.Words
PdfSaveOptions pdfSaveOptions = new PdfSaveOptions
{
TextCompression = PdfTextCompression.None,
SaveFormat = SaveFormat.Pdf,
Compliance = PdfCompliance.PdfA1a
};
doc.Save(targetPath, pdfSaveOptions);
}
Console.WriteLine($"PDF saved: {targetPath}");
}
}
}
Summary
The -aw-import:ignore token is not part of the original EML file; it is introduced during the conversion of the MailMessage to MHTML by Aspose.Email. When Aspose.Words later loads that MHTML, the processing‑instruction causes the loader to skip the following HTML element, which often contains the actual message body, so the body disappears from the generated PDF.
Why it happens
Step
What the code does
Why the text disappears
1. MailMessage.Load(eml)
Loads the original EML. The -aw‑import:ignore token is not present in the source file.
–
2. mailMsg.Save(ms, SaveOptions.DefaultMhtml)
Aspose.Email serialises the message to MHTML and inserts a processing‑instruction <?-aw-import:ignore?> to mark parts that should be ignored by a Word importer.
The token becomes part of the MHTML markup.
3. new Document(ms, loadOptions)
Aspose.Words parses the MHTML. When it encounters the -aw‑import:ignore instruction it discards the next HTML element (usually the <body> or a <div> that holds the message text).
The body text is omitted from the Document.
4. doc.Save(pdf)
The PDF is generated from the already‑truncated Document.
The missing text is already gone.
What to do next
Confirm the source of the token – after the mailMsg.Save call, read the MHTML stream and look for -aw-import:ignore:
using (var mht = new MemoryStream())
{
mailMsg.Save(mht, SaveOptions.DefaultMhtml);
mht.Position = 0;
string mhtText = new StreamReader(mht).ReadToEnd();
Console.WriteLine(mhtText.Contains("-aw-import:ignore")); // should print True
}
Upgrade the libraries – newer releases of Aspose.Email and Aspose.Words may have removed or changed the handling of this processing‑instruction. Check the release notes for versions later than 23.11.0 / 23.12.0 and test the conversion again without any string replacement.
Strip the token before loading into Aspose.Words (if you cannot upgrade):
using (var mhtStream = new MemoryStream())
{
mailMsg.Save(mhtStream, SaveOptions.DefaultMhtml);
mhtStream.Position = 0;
string mhtContent = new StreamReader(mhtStream).ReadToEnd();
mhtContent = mhtContent.Replace("-aw-import:ignore", string.Empty);
byte[] cleaned = Encoding.UTF8.GetBytes(mhtContent);
using (var cleanedStream = new MemoryStream(cleaned))
{
var loadOptions = new LoadOptions { LoadFormat = LoadFormat.Mhtml };
var doc = new Document(cleanedStream, loadOptions);
doc.Save("output.pdf");
}
}
Create your own MHTML – if you need full control, build a minimal MHTML envelope yourself using mailMsg.BodyHtml (or mailMsg.Body) and feed that to Aspose.Words. This bypasses Aspose.Email’s serializer entirely and eliminates the unwanted instruction.
Contact Aspose Support – provide a minimal reproducible example (the EML file, the generated MHTML snippet containing the token, and the library versions). The support team can confirm whether this is a known issue and may supply a hot‑fix or a configuration flag that suppresses the instruction.
By either upgrading, cleaning the MHTML, or generating your own MHTML, you can prevent the -aw-import:ignore tag from causing body text loss in the final PDF.
Note: The evidence supplied with the request does not contain information about the -aw-import:ignore token or its handling in Aspose.Email/Words, so the above explanation is based on typical behaviour of these libraries and may need verification against your specific environment.
I could not fully verify this explanation against the retrieved sources. Please double-check the behaviour in your environment and confirm the exact SDK/version.
SDK version unclear — refer to the official release notes before applying this guidance.
@lukasbg As Most likely the mail body wasproduced by Aspose.Words and then modified. The mentioned -aw-import:ignore attribute instructs Aspose.Words to skip the html node upon importing HTML. It is written by Aspose.Words as a part of document roundtrip information. You can try disabling HtmlSaveOptions.ExportRoundtripInformation property. In this case Aspose.Words will not write roundtrip information with -aw-* CSS properties. But this will not affect existing HTML document, which already contains -aw-* CSS properties.
Thank you very much for your reply!
We’ll check your feedback and verify how it works within our current implementation. Once we’ve completed the checks and testing, we’ll come back with the results and any additional insights we find.