Free Support Forum - aspose.com

How to convert encrypted Outlook email MSG to PDF

I am using the following code snippet to convert an MSG file to PDF.
This works for regular Outlook emails but not for encrypted.
Is there anything I can do to perform conversion of the encrypted email messages?

Outlook Encryption Option.png (11.6 KB)
Result PDF Content.png (34.1 KB)

   public class EmailToPdfConverter : IDocumentConverter
    {
        private readonly byte[] sourceFileBytes;

        public EmailToPdfConverter(byte[] sourceFileBytes)
        {
            this.sourceFileBytes = sourceFileBytes;
            AsposeLicenseInitializer.Initialize();
        }

        public byte[] Convert()
        {
            var sourceStream = new MemoryStream(sourceFileBytes);
            var mailMessage = MailMessage.Load(sourceStream);

            var mailStream = new MemoryStream();
            mailMessage.Save(mailStream, MailMessageSaveType.MHtmlFromat);

            var loadOptions = new LoadOptions { LoadFormat = LoadFormat.Mhtml };
            var wordDocument = new Document(mailStream, loadOptions);

            var targetStream = new MemoryStream();
            wordDocument.Save(targetStream, SaveFormat.Pdf);

            return targetStream.ToArray();
        }
    }

@dan.g

I am moving this thread to Aspose.Email forum where our respective support team will assist you further in this regard.

@dan.g,
Thank you for your request. In your case, you should decrypt the mail messages before converting them to PDF files as below:

var privateCertificateFile = "<path_to_private_certificate_file>";
var privateCertificate = new X509Certificate2(privateCertificateFile, "<password>");

// Decrypt the mail message after loading
mailMessage.Decrypt(privateCertificate);

More details: Utility Features - MailMessage
API Reference: MailMessage Class