5

I'm using GemBox.Email and GemBox.Document to convert emails to PDF.

This is my code:

static void Main()
{
    MailMessage message = MailMessage.Load("input.eml");
    DocumentModel document = new DocumentModel();

    if (!string.IsNullOrEmpty(message.BodyHtml))
        document.Content.LoadText(message.BodyHtml, LoadOptions.HtmlDefault);
    else
        document.Content.LoadText(message.BodyText, LoadOptions.TxtDefault);

    document.Save("output.pdf");
}

The code works for EML files, but it doesn't for MSG (both MailMessage.BodyHtml and MailMessage.BodyText) are empty.

How can I make this work for MSG as well?

NixonUposseen
  • 53
  • 3
  • 12
  • MSG is a different file format than EML and MS Outlook-Specific. Check if GemBox can actually handle them. Maybe you need to tell it, what to expect. – Fildor Sep 25 '20 at 12:56
  • Hmmm, ... [this](https://www.gemboxsoftware.com/news/outlook-msg-file-format-support-in-gembox-email) says they can. And it's determined by file extension. So your code _should_ just work. Can you verify your .msg actually is a valid Outlook Message file and has content? Can you try another file with msg extension? Can you open it in outlook or one of the "outlook-msg reader" apps? – Fildor Sep 25 '20 at 13:04
  • @Fildor it should support MSG, EML, and MHTML according to their [Convert](https://www.gemboxsoftware.com/email/examples/c-sharp-outlook-msg-eml-mht/106) example. – NixonUposseen Sep 25 '20 at 13:05
  • Yes, I just read that, too. So, either there is a bug (which I doubt) or your MSG File is somehow not a valid ms outlook message file. I'd try another file, and double- and tripple check _that_ file before contacting their support if you're still unlucky and the file _is_ valid. – Fildor Sep 25 '20 at 13:08
  • 1
    @NixonUposseen I believe that the problem could be specific to your MSG file, so try the suggestions from Fildor. If the problem remains, can you upload somewhere your MSG file so that I can take a look at it? – Mario Z Sep 25 '20 at 13:09

1 Answers1

6

The problem occurs with specific MSG files that don't have HTML content within an RTF body, instead, they have a raw RTF body.

The MailMessage class currently doesn't expose API for the RTF body (only plain and HTML body). Nevertheless, you can retrieve it as an Attachment named "Body.rtf".

Also as an FYI, another problem you have is that the images from the email's HTML body are not inlined, and thus, you'll lose them when exporting to PDF.

Anyway, try using the following:

static void Main()
{
    // Load an email (or retrieve it with POP or IMAP).
    MailMessage message = MailMessage.Load("input.msg");

    // Create a new document.
    DocumentModel document = new DocumentModel();

    // Import the email's body to the document.
    LoadBody(message, document);

    // Save the document as PDF.
    document.Save("output.pdf");
}

static void LoadBody(MailMessage message, DocumentModel document)
{
    if (!string.IsNullOrEmpty(message.BodyHtml))
    {
        var htmlOptions = LoadOptions.HtmlDefault;
        // Get the HTML body with embedded images.
        var htmlBody = message.GetEmbeddedBodyHtml();
        // Load the HTML body to the document.
        document.Content.End.LoadText(htmlBody, htmlOptions);
    }
    else if (message.Attachments.Any(a => a.FileName == "Body.rtf"))
    {
        var rtfAttachment = message.Attachments.First(a => a.FileName == "Body.rtf");
        var rtfOptions = LoadOptions.RtfDefault;
        // Get the RTF body from the attachment.
        var rtfBody = rtfOptions.Encoding.GetString(rtfAttachment.Data.ToArray());
        // Load the RTF body to the document.
        document.Content.End.LoadText(rtfBody, rtfOptions);
    }
    else
    {
        // Load TXT body to the document.
        document.Content.End.LoadText(message.BodyText, LoadOptions.TxtDefault);
    }
}

For more information (like how to add email headers and attachments to output PDF), check the Convert Email to PDF examples.

Mario Z
  • 4,328
  • 2
  • 24
  • 38