Convert Email to PDF

private static MemoryStream GetMhtml(Stream input)
{
using (input)
using (var mm = MailMessage.Load(input))
{
var output = new MemoryStream();
mm.Save(output, SaveOptions.DefaultMhtml);
return output;
}
}

    private static MemoryStream GetPdf(Stream input)
    {
        using (input)
        {
            var output = new MemoryStream();
            new Document(input, new LoadOptions { LoadFormat = LoadFormat.Mhtml }).Save(output, SaveFormat.Pdf);
            return output;
        }
    }

    private static MemoryStream GetPdfA(Stream input)
    {
        using (input)
        using (var doc = new Aspose.Pdf.Document(input))
        {
            doc.Convert(new Aspose.Pdf.PdfFormatConversionOptions(Aspose.Pdf.PdfFormat.PDF_A_3A));
            var output = new MemoryStream();
            doc.Save(output);
            return output;
        }
    }

    internal static MemoryStream ConvertEmlToPdf(byte[] input)
    {
        return GetPdfA(GetPdf(GetMhtml(new MemoryStream(input))));
    }

Hi the above code produces an error at the doc.Convert line of code - Access to the path ‘C:\WINDOWS\SysWOW64\inetsrv\ConversionLog.xml’ is denied.

any clues why and what am I missing?

Thanks Mike

@mikesoffice

Thanks for contacting support.

Would you please share some more details like which Aspose API version you are using to convert Email to PDF, along with sample input file. This will help us replicating the issue in our environment and address it accordingly.

Hi

I’m using the latest version of the Aspose software suite ver 18.2.0.0
ProcessDocument.zip (13.9 KB)
I have attached the sample code and Email I’m trying to convert.
I think that this is a problem with the Aspose trying to use disk to page out for the convert command. Can I assign a disk/path for it to process this command?

Thanks Mike

@mikesoffice

Thanks for sharing sample EML file.

We have tested the scenario in our environment by using latest version of all APIs (i.e Aspose.PDF, Aspose.Words, Aspose.Email) and were unable to notice any exception. However, we have modified two methods from your code as follows:

private static MemoryStream GetPdf(Stream input)
{
  using (input)
  {
    var output = new MemoryStream();
    new Aspose.Words.Document(input, new Aspose.Words.LoadOptions { LoadFormat = Aspose.Words.LoadFormat.Mhtml }).Save(output, Aspose.Words.SaveFormat.Pdf);
     return output;
  }
}

private static MemoryStream GetPdfA(Stream input)
{
  using (input)
  using (var doc = new Aspose.Pdf.Document(input))
  {
   doc.Convert(new MemoryStream(), Aspose.Pdf.PdfFormat.PDF_A_3A, ConvertErrorAction.Delete);
   var output = new MemoryStream();
   doc.Save(output);
   return output;
  }
}

EmailtoPDF1.pdf (35.7 KB)

For your kind reference, we have also attached an output, generated by above code snippet. Please try using Convert() method, like in above code snippet and in case you still face any issue, please share your environment details e.g OS Version, Application Type, etc. We will again test the scenario in our environment and address it accordingly.

Hi

this may work in your environment but having changed my code to reflect your changes I still get an EML file but
corrupt2fd70d30-632c-48ba-bd1c-ae143c2382e5.zip (33.0 KB)

Please find attached result - I’m saving this file into sharepoint btw

Thanks Mike

@mikesoffice

Thanks for getting back to us.

Earlier the scenario was to convert EML file into PDF document, which we have tested and shared generated output PDF with you. However, you have now shared an EML file with us which is corrupt and it seems it is an output. Would you please clarify if you are converting EML file into PDF or generating it from some other file format. We will test the scenario in our environment and address it accordingly.