Converting e-mail to PDF

Hello Aspose,

I have installed a trial version of Aspose Total for .NET (from On-Premise APIs & Components | Aspose.Total Product Family), and I am testing different conversions in that.

I have problem with converting e-mails to PDF. I have copied the code from your sample problem, but I get error here:

var document = new Aspose.Words.Document(memoryStream, loadOptions);

The error says: “The document appears to be corrupted and cannot be loaded.”

I am using DLLs for net4.0 in Visual Studio 2015.

It works from my Console Program (.exe), but it fails when I put the code in a C# project (DLL)!

Do you have any idea, what is wrong here?

Best regards,

Akbar Azizi

1 Like

@axaz,

From the details it seems that you are trying to convert Email to PDF using options exposed by Aspose.Words API. You may use the following code snippet to achieve the task:

CODE:

string inputfile = "";
string outputFile = "";
MailMessage message = MailMessage.Load(inputfile);
message.TimeZoneOffset = TimeZone.CurrentTimeZone.GetUtcOffset(message.Date);
MhtSaveOptions mhtSaveOptions = new MhtSaveOptions
{
    MhtFormatOptions = MhtFormatOptions.WriteHeader | MhtFormatOptions.WriteCompleteEmailAddress
};
mhtSaveOptions.SkipInlineImages = false;
MemoryStream msgStream = new MemoryStream();
message.Save(msgStream, mhtSaveOptions);
msgStream.Position = 0;
var options = new Aspose.Words.LoadOptions()
{
    LoadFormat = Aspose.Words.LoadFormat.Mhtml
};
var document = new Aspose.Words.Document(msgStream, options);
document.Save(outputFile, Aspose.Words.SaveFormat.Pdf);

Try it at your end update us with your feedback. In case of any issue please share sample input file and sample code along with the code to reproduce the issue.

Hi Ikram,

Thank you for the help, but I have still the same problem (please see the attached document).

As I wrote in my earlier post, it works from my Console Program (PdfConverterTest.exe), but it fails when I put the code in a C# project (PdfConverter.dll) and call it from my .exe program!

My mail has only the text “Test” and nothing else.

Best regards,
Akbar Azizi

PdfConverter.PNG (89.5 KB)

@axaz,

We have tried to reproduce the said issue. We are unable to reproduce it. Sample solution along with sample input and output file is attached for your reference.

Test_Email_To_PDF.zip (7.4 KB)
sample_input_output.zip (62.0 KB)

Hi Ikram,

Thank you for the sample code.

I can see that you are not using Words DLL version net4.0 as I do.
All my references are to net4.0 DLLs.

When I change my reference to Words from net4.0 to net2.0_authenticodesigned, then it works for me too.

BR,
Akbar

@axaz,

Thank for writing us back. We are using the updated versions of Aspose.Words and Aspose.Email. Screenshots of the referenced DLLs are attached.
project_references.jpg (78.5 KB)

It is not possible to see the path on your picture

@axaz,

DLLs are referenced from NuGet packages as:

E:\Test_Email_To_PDF\packages\Aspose.Words.17.11\lib\net40

Your zip files do not contain packages.

When do you publish these updated DLLs?

Would you please send me the licens file too?

I can see you use it in your sample program:

objConvert.LicenseFilePath = @“E:\lic\Aspose.Total.lic”;

@axaz,

The unwanted folders and the one that can be generated were removed from the ZIP to make it lightweight. The latest versions of APIs were published in the current month. Further, we cannot share the license file with as per our policy. In case you do not have a valid license you may get one. Access the link to get a temporary license for 30 days.

That is okay.

I have another question:

It takes long time to convert a mail which contains an image hashcode like the following.
Is there a way to skip this?

Best regards,
Akbar Azizi

base64 png image data.zip (46.3 KB)
Rendered Image is: cbimage.png (24.6 KB)

@axaz,

Share the sample input file that is taking long time with us. Further please update us about do you want to skip/leave the images in the conversion process? In this way the images will not be present in the resultant file.

Hi Ikram,

It happens often that our customers make copy/paste texts from other systems, thus images will not be copied correct, but their hashcodes will be inserted into their mails.

When we create a .PDF file, we want to keep all pictures, but skip only hashcodes!

I have attached a test mail here.

Test mail.zip (675.0 KB)

@axaz,

Thank you for sharing sample. This is to update you that each of the embedded image in the message is contained in MailMessage.LinkedResource collection. If a message contains any such thing, it will be processed/read by Aspose.Email. There is no way to skip it or avoid it.

Thank you for your response.

BR,
Akbar