I have a EML file that has email content plus 1 PDF attachment and 2 JPG attachments.
When I run the below code to convert EML to PDF, I get a PDF document that is outputted which includes the JPG attachments but the PDF attachment is not added. Blank page is added instead of the actual PDF attachment.
Pop3MessageInfoCollection infos = client.ListMessages();
foreach (Pop3MessageInfo info in infos)
{
client.SaveMessage(info.SequenceNumber, @"C:\" + info.SequenceNumber + ".eml");
}
// Save message to disk by message sequence number
client.Dispose();
//Load the EML file
MailMessage mailMsg = MailMessage.Load(@"C:\" + 2 + ".eml");
MemoryStream ms = new MemoryStream();
mailMsg.Save(ms, Aspose.Email.SaveOptions.DefaultMhtml);
// create an instance of LoadOptions and set the LoadFormat to Mhtml
var loadOptions = new Aspose.Words.LoadOptions();
loadOptions.LoadFormat = Aspose.Words.LoadFormat.Mhtml;
// create an instance of Document and load the MTHML from MemoryStream
var document = new Aspose.Words.Document(ms, loadOptions);
// create an instance of HtmlSaveOptions and set the SaveFormat to Html
var saveOptions = new Aspose.Words.Saving.PdfSaveOptions();
document.Save(@"C:\" + "SaveEmailAsPDF_out.pdf", saveOptions);
To ensure a timely and accurate response, please attach the following resources here for testing:
Your input EML and MHTML documents.
Please attach the output PDF file that shows the undesired behavior.
Please attach the expected output PDF file that shows the desired behavior.
As soon as you get these pieces of information ready, we will start investigation into your issue and provide you more information. Thanks for your cooperation.
PS: To attach these resources, please zip and upload them.
Please note that Aspose.Words mimics the behavior of MS Word. If you export the MHTML file generated by Aspose.Email to PDF using MS Word, you will get the same output.
Your query is more related to Aspose.Email API. We are moving this thread to Aspose.Email forum where you will be guided appropriately.
I have observed your requirements and like to share that there is no direct mechanism available to export the EML to PDF. Aspose.Email allows to export EML to MHTML. Then Aspose.Words is further used to export MHTML to PDF. Please visit this documentation link for your further kind reference.
See the code I initially posted. I’m using Apose.Email to create EML file in a folder and then I’m using Aspose.Words to convert to PDF. The code that I have converted the EML to PDF fine but any PDF, DOC, XLS etc… attachments in the EML are not added to the PDF. Please direct me to someone that can help me.
I regret to share that there is no support available to export the EML with included attachment being rendered in output. The API by design when export the EML to MHTML, it only export the content part to MHTML.
The conversion from EML to PDF is including JPG attachments in the PDF created. Seems like it’s supporting something.
Can we do the following?
Convert EML to PDF not to include any attachments (Right now the code below includes JPG attachments)
Manually download each attachment -
Merge each attachment to the PDF from the first step.
string sourcePath = @“C:\Aspose”;
Pop3MessageInfoCollection infos = client.ListMessages();
foreach (Pop3MessageInfo info in infos)
{
client.SaveMessage(info.SequenceNumber, sourcePath + info.SequenceNumber + “.eml”);
//Load the EML file
MailMessage mailMsg = MailMessage.Load(sourcePath + info.SequenceNumber + ".eml");
MemoryStream ms = new MemoryStream();
mailMsg.Save(ms, Aspose.Email.SaveOptions.DefaultMhtml);
// create an instance of LoadOptions and set the LoadFormat to Mhtml
var loadOptions = new Aspose.Words.LoadOptions();
loadOptions.LoadFormat = Aspose.Words.LoadFormat.Mhtml;
// create an instance of Document and load the MTHML from MemoryStream
var document = new Aspose.Words.Document(ms, loadOptions);
// create an instance of HtmlSaveOptions and set the SaveFormat to Html
var saveOptions = new Aspose.Words.Saving.PdfSaveOptions();
document.Save(sourcePath + info.SequenceNumber + ".pdf", saveOptions);
}
Can you provide me sample code of how this can be done? For the first step we just need to figure out how NOT to include the JPG’s. For the second step, I have the code to download all the attachments from the EML. For the third step, need sample code on how this can be done.
I have shared sample code with you. This will remove attachments from your source file. Please share feedback with us if there is still an issue. I have also shared my generated output with you for your kind reference. outputremove.pdf (94.2 KB)
string path = “F:\Aspose Work\”;
Aspose.Email.MailMessage theMessage = Aspose.Email.MailMessage.Load(path+"4.eml");
// Suggestion to turn off attachents in generated output
MhtSaveOptions opt = new MhtSaveOptions();
opt.SaveAttachments = false;
//theMessage.Save(@"EMLMessage18.3.mht", opt);
MemoryStream stream = new MemoryStream();
theMessage.Save(stream, opt);
stream.Seek(0, SeekOrigin.Begin);
Aspose.Words.LoadOptions loadOptions = new Aspose.Words.LoadOptions();
loadOptions.LoadFormat = Aspose.Words.LoadFormat.Mhtml;
Aspose.Words.Document doc = new Aspose.Words.Document(stream, loadOptions);
Aspose.Words.Saving.PdfSaveOptions options = new
Aspose.Words.Saving.PdfSaveOptions();
options.Compliance = PdfCompliance.Pdf15;
doc.Save(path+"outputremove.pdf", options);
}
This worked. I have just the email message saved in the PDF now.
Question: You said that you don’t support attachments being rendered in the output but why were we getting JPG attachments rendered in the output? In my sample only the PDF attachments weren’t being rendered in the output.
Also, how can I now manually merge different types of documents like JPG’s and PDF’s to an existing PDF document?
So the opt.SkipInlineImages = true; line of code is to do what exactly?
The opt.SaveAttachments = false; line of code that was previously provided was able to save the EML without any attachments.
The question that I had is that you had mentioned that attachments are not supported to be saved to the PDF. But JPG attachments were being saved.
It looks like attachment support does exist from EML to PDF. If it does why are PDF’s, DOC not included? Can you let me know what attachment types are supported?