Exporting email with attachements to PDF

Hi,

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);

Correction. There is no blank page added instead of the PDF attachment.

The PDF attachments are just not getting attached to the outputted PDF.

@linocoelho

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.

Test.zip (382.0 KB)

Attached is the EML which is my input file and the PDF is the file that is outputted.

EML has 1 PDF attachment and 2 JPG attachments.

Output file does not include PDF.

The same thing happens if you have DOC, DOCX, XLS, XLSX, TXT etc… attached in the EML. The output PDF does not include these attachments.

@linocoelho

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.

Basically what I’m trying to do is Download an email to a folder as EML.

These EML’s could have many types of attachments (PDF, JPG, XLSX, XLS, DOCX, DOC, TXT etc…

After this we want to convert the EML to PDF to include the email body plus all the attachments.

@linocoelho,

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.

@linocoelho,

I have observed your following comments:

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?

  1. Convert EML to PDF not to include any attachments (Right now the code below includes JPG attachments)
  2. Manually download each attachment -
  3. 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.

Let me know.

Thanks,

@linocoelho,

I have observed your following comments.

Can you please share the sample EML file with JPG attachment that you are getting exported to PDF with attachment.

Please is the thread. I had already provided this info in the Test.zip file.

Sorry had typo in last response. I had provided the Test.zip file already in one of the replies from 1 day ago.

@linocoelho,

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?

@linocoelho,

Please add following statement in your code.

       opt.SkipInlineImages = true;

Sorry confused to what that line of code is for?

Can you point me to sample code where I can merge different types of documents like JPG’s and PDF’s to an existing PDF document?

@linocoelho,

I have shared the following option in response to your request.

       opt.SkipInlineImages = true;

You can add the above line in sample example that we have shared previously with you.

I suggest you to please consider Aspose.PDF in this regard and may consult Aspose.PDF forum in a separate thread.

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?