Free Support Forum - aspose.cloud

Convert MHT to PDF in C# using Aspose.PDF REST API is throwing Object reference not set error

Hello, we have started to evaluate your cloud PPD and Mail services.

But we get the following error in your applications:

Aspose.Pdf.Cloud.Sdk.Client.ApiException: Error calling GetMhtInStorageToPdf: {“RequestId”:“Root=1-60376348-6ed6f615099ef9eb6129c229”,“Error”:{“Code”:“internalError”,“Message”:“Object reference not set to an instance of an object.”,“Description”:“Operation Failed. Internal error.”,“DateTime”:“2021-02-25T08:43:53.0682323Z”,“InnerError”:null}}
at Aspose.Pdf.Cloud.Sdk.Api.PdfApi.GetMhtInStorageToPdfAsyncWithHttpInfo(String srcPath, String storage)
at Aspose.Pdf.Cloud.Sdk.Api.PdfApi.GetMhtInStorageToPdfAsync(String srcPath, String storage)
at AsposeTestEmailAndPdf.Program.UseAsposeCloud(String emlFullPath, String resultsPath) in D:\spikes\AsposeTestEmailAndPdf\AsposeTestEmailAndPdf\Program.cs:line 89
at AsposeTestEmailAndPdf.Program.Main(String[] args) in D:\spikes\AsposeTestEmailAndPdf\AsposeTestEmailAndPdf\Program.cs:line 27
at AsposeTestEmailAndPdf.Program.(String[] args)

The error comes when calling:
var pdfResponseStream = await pdfApi.GetMhtInStorageToPdfAsync(destinationHtmlFile);

@geostian

We are sorry for the inconvenience. We will appreciate it if you please share your sample code along with the input file with us. We will investigate the issue and will guide you.

Epost med mange formater cleaned.zip (1021.1 KB)

class Program
    {
        static async Task Main(string[] args)
        {
            var basePath = @"D:\spikes\AsposeTestEmailAndPdf\AsposeTestEmailAndPdf\";
            var emailFilePath = @"dummy_emails\Epost med mange formater cleaned.msg";

            var emailFileName = Path.GetFileName(emailFilePath);
            var emailFullPath = Path.Combine(basePath, "testfiles", emailFilePath);

            var resultsPath = Path.Combine(basePath, "results", emailFilePath);

            await UseAsposeCloud(emailFullPath, resultsPath);
     }
        private static async Task UseAsposeCloud(string emailFullPath, string resultsPath)
        {
            var clientSecret = "***************";
            var clientId = "**************";
            var api = new EmailCloud(clientSecret, clientId);
            
            var mhtmlResultFullPath = Path.Combine(resultsPath, "result.html");

            using (var emlStream = File.OpenRead(emailFullPath))
            {
                // https://docs.aspose.cloud/email/email-message-files/#how-to-convert-email-from-one-format-to-another
                var resultStream = await api.Email.ConvertAsync(new EmailConvertRequest(
                    "MSG", "MHTML", emlStream));

                Directory.CreateDirectory(resultsPath);
                    
                using (var fileStream = new FileStream(
                        mhtmlResultFullPath, FileMode.Create, FileAccess.Write, FileShare.Write, 4096, useAsync: true))
                {
                    await resultStream.CopyToAsync(fileStream);
                }
            }
            
            var pdfApi = new PdfApi(clientSecret, clientId);

            var destinationHtmlFile = Guid.NewGuid() + ".html";
            await UploadFile(pdfApi, mhtmlResultFullPath, destinationHtmlFile);
            
            // https://github.com/aspose-pdf-cloud/aspose-pdf-cloud-dotnet/blob/master/Examples/ConvertToPDFExamples.cs
            var pdfResponseStream = await pdfApi.GetMhtInStorageToPdfAsync(destinationHtmlFile);
            
            using (var fileStream = new FileStream(
                Path.Combine(resultsPath, "mailbody.pdf"), FileMode.Create, FileAccess.Write, FileShare.Write, 4096, useAsync: true))
            {
                await pdfResponseStream.CopyToAsync(fileStream);
            }
        }
        
        private static async Task UploadFile(IPdfApi api, string sourcePath, string serverFileName)
        {
            using (var file = File.OpenRead(Path.Combine("", sourcePath)))
            {
                var response = await api.UploadFileAsync(Path.Combine("", serverFileName), file);
            }
        }
}

@geostian

Thanks for sharing your sample code. We have reproduced the issue and logged a ticket PDFCLOUD-1962 for further investigation. We will share our findings with you soon.