Free Support Forum - aspose.com

Convert email to PDF (C# .NET)

Converting MSG or EML files to PDF is one of important requirement and it gives users the flexibility to print the email document and also share with anyone concerned in the form of PDF.

Loading MSG/EML and Save to PDF using intermediate MHTML output

Aspose.Email for .NET does not have direct support of exporting EML and MSG files to PDF. However, if Aspose.Email and Aspose.Words are used together this requirement can be easily met. This will be a two steps strategy:

Step 1: The EML/MSG files will be converted to MHTML format, which will serve as intermediate output format.

Step 2: The exported MHTML will be processed by Aspose.Words and converted to PDF.

One thing to consider here is that an email message can contain attachments as well. Since each attachment can be of different media types, Aspose.Email ignores these attachments while converting to MHTML i.e. only inline images in a message will be part of MHTML and any regular attachments will be ignored.

    using (MemoryStream ms = new MemoryStream())
    {
        // Load the EML or MSG file
        MailMessage message = MailMessage.Load("Message.msg");
                
        message.Save(ms, Aspose.Email.SaveOptions.DefaultMhtml);

        //Resetting the Memory stream position
        ms.Position = 0;

        // create an instance of Aspose.Words LoadOptions and set the LoadFormat to Mhtml
        Aspose.Words.LoadOptions loadOptions = new Aspose.Words.LoadOptions();
        loadOptions.LoadFormat = LoadFormat.Mhtml;

        // create an instance of Words Document and load the MTHML from MemoryStream
        Aspose.Words.Document document = new Aspose.Words.Document(ms, loadOptions);

        // create an instance of PdfSaveOptions
        var saveOptions = new Aspose.Words.Saving.PdfSaveOptions();

        // Save loaded MHTML to PDF
        document.Save("SaveEmailAsPDF.pdf", saveOptions);
    }

You can also find similar forum threads related to category of Email to PDF for your further exploration.

For more details on converting MSG/EML files to PDF, please visit the documentation section:
Converting Email message to PDF
Saving MailMessage to Stream
Saving Document to PDF

You can also visit and explore our latest working Aspose.Email working demo applications for further reference.