Various situations can arise when you need to embed the content of an E-mail message in a Web or desktop application. In some cases, E-mail needs to be converted into a format that is easy to embed and display, and one possible solution is E-mail to PDF conversion. This article provides step-by-step instructions and code examples on how to convert E-mail to PDF using C#.

Aspose.Email is a C# class library designed to learn from NET applications create and handle popular E-mail formats internally. Used in conjunction with Aspose.Words for.net, this API enables you to convert email to PDF format with high fidelity. (Click download)

Steps to convert email to PDF using C#

Here are the steps to convert email to PDF in C#.

  • Load E-mail files using the MailMessage class.

    // Load email message using file
    MailMessage mailMsg = MailMessage.Load("message.msg");
    Copy the code
  • Save E-mail as a MemoryStream object in default MHTML format.

    // Create memory stream
    MemoryStream ms = new MemoryStream();
    
    // Save email message into memory stream
    MailMessage.Save(ms, Aspose.Email.SaveOptions.DefaultMhtml);
    Copy the code
  • Create an instance of the asposite.words.LoadOptions class and set the loading format to MHTML.

    // Create and set load options
    var loadOptions = new Aspose.Words.LoadOptions();
    loadOptions.LoadFormat = LoadFormat.Mhtml;
    Copy the code
  • Instantiate the Aspose.words.Document class and pass MemoryStream and LoadOptions objects as arguments to its constructor.

    // Create an instance of Document and load the MTHML from MemoryStream
    var document = new Aspose.Words.Document(ms, loadOptions);
    Copy the code
  • Create a Aspose. Words. Saving. PdfSaveOptions instances of the class.

    // create an instance of PDFSaveOptions class
    var pdfSaveOptions = new Aspose.Words.Saving.PdfSaveOptions();
    Copy the code
  • Convert E-mail messages to PDF using document. Save (” email-to-pdf.pdf “, PdfSaveOptions).

    // Save email as PDF
    document.Save("email-to-pdf.pdf", pdfSaveOptions);
    Copy the code

Convert email to PDF complete code using C#

// Load email message
MailMessage mailMsg = MailMessage.Load("message.msg");

MemoryStream ms = new MemoryStream();
mailMsg.Save(ms, Aspose.Email.SaveOptions.DefaultMhtml);

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

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

// create an instance of HtmlSaveOptions
var saveOptions = new Aspose.Words.Saving.PdfSaveOptions();
document.Save("email-to-pdf.pdf", saveOptions);
Copy the code

If you have any questions or requirements, please feel free to join the Aspose Technology Exchange Group (761297826), we are happy to provide you with inquiries and consultation.