Load Email document with options

View as MarkdownMarkdown

Set up your AI agent

Give your coding agent live access to the GroupDocs documentation and API reference through the GroupDocs Docs MCP server — no install, no API key. Learn more

claude mcp add --transport http groupdocs-docs https://docs.groupdocs.com/mcp

Also process documents locally

The GroupDocs.Conversion MCP server lets your agent work with files on your machine; they are never uploaded. It needs the .NET 10 SDK or Docker.

claude mcp add groupdocs-conversion -- dnx GroupDocs.Conversion.Mcp --yes
Folders, licensing and more →

GroupDocs.Conversion provides the EmailLoadOptions class to give you control over how the source email document will be processed. The following options could be set:

OptionDescription
FormatThe document type is auto-detected during loading, however, you can specify explicitly the type of the source email document. Available options are: Msg, Eml, Emlx, Pst, Ost, Vcf, Mht 
ConvertOwned  Controls whether the owned documents in the documents container must be converted
ConvertOwner  Controls whether the documents container itself must be converted If this property is true the documents container will be the first converted document
Depth  Controls how many levels in depth to perform the conversion
MergeAttachmentsWhether to convert an EML, EMLX or MSG message and its attachments into a single paginated PDF, with a bookmark for each part. Only PDF is supported as the target format. Default: false. Available since version 26.11. See Merge an email and its attachments into one PDF.
DisplayBccEmailAddressOption to display or hide “Bcc” email address
DisplayCcEmailAddressOption to display or hide “Cc” email address
DisplayEmailAddresses Option to display or hide the email addresses next to the names, for example John Doe <john.doe@sample.com> or only John Doe. Default: true.
DisplayFromEmailAddressOption to display or hide “from” email address
DisplayHeaderOption to display or hide the email header
DisplayToEmailAddressOption to display or hide “to” email address
FieldTextMap The mapping between email message EmailField and field text representation
PreserveOriginalDate defines whether to keep the original date header string in the mail message when saving (Default value is true)
ExternalResourceHandlerA handler called for each resource the message asks to load; it can allow the resource, skip it or supply its content. Default: null. Available since version 26.10.
ResourceLoadingTimeoutSpecifies the timeout of loading the external resources.
SkipExternalResourcesWhether to skip the external resources the message references, such as remote images. Default: true. Content the message carries, local files inside its folder and resources that match WhitelistedResources still load. See Skip loading of external resources.
WhitelistedResourcesAbsolute URI prefixes, such as https://cdn.example.com/assets/, of external resources to load while SkipExternalResources is true. Since version 26.10, an entry that is not an absolute URI prefix makes the load fail with InvalidLoadOptionsException.
TimeZoneOffset Gets or sets the Coordinated Universal Time (UTC) offset for the message dates. This property defines the time zone difference, between the local time and UTC.

Control fields visibility

The following code snippet shows how to convert an Email document and control the visibility of the fields:

Func<LoadContext, LoadOptions> getLoadOptions = loadContext => new EmailLoadOptions
{
    DisplayHeader = false,
    DisplayFromEmailAddress = false,
    DisplayToEmailAddress = false,
    DisplayCcEmailAddress = false,
    DisplayBccEmailAddress = false
};
using (Converter converter = new Converter("sample.msg", getLoadOptions))
{
    PdfConvertOptions options = new PdfConvertOptions();
    converter.Convert("converted.pdf", options);
}

Converting email attachments

The following code snippet shows how to convert an Email document and all attachments:

var source = "sample-with-attachment.eml";
var loadOptions = new EmailLoadOptions {
    ConvertOwner = true,
    ConvertOwned = true,
    // convert email itself and the attachments
    Depth = 2
};
using (var converter = new Converter(source, (LoadContext loadContext) => loadOptions))
{
    var index = 1;
    var options = new PdfConvertOptions();
    // Note: index = 1 is the email itself, all following indexes are attachments
    converter.Convert((SaveContext saveContext) => new FileStream($"converted-{index++}.{saveContext.TargetFormat.Extension}", FileMode.Create) , options);
}

Merge an email and its attachments into one PDF

Since version 26.11, the MergeAttachments option converts an EML, EMLX or MSG message and its attachments into a single paginated PDF instead of one file per item. The option is off by default.

The following code snippet shows how to convert an email and its attachments into one PDF:

using GroupDocs.Conversion;
using GroupDocs.Conversion.Options.Convert;
using GroupDocs.Conversion.Options.Load;

using (var converter = new Converter("message.eml",
    context => context.HierarchyLevel == 0 ? new EmailLoadOptions { MergeAttachments = true } : null))
{
    converter.Convert("message.pdf", new PdfConvertOptions());
}

HierarchyLevel == 0 applies the email load options to the message itself. Attachments are loaded with their default options.

To get a PDF/A document, for example PDF/A-1b for archiving, set the PDF format:

using (var converter = new Converter("message.eml",
    context => context.HierarchyLevel == 0 ? new EmailLoadOptions { MergeAttachments = true } : null))
{
    var options = new PdfConvertOptions
    {
        PdfOptions = new PdfOptions { PdfFormat = PdfFormats.PdfA_1B }
    };
    converter.Convert("message.pdf", options);
}

The merged PDF is built as follows:

  • The message body comes first, followed by the attachments in the order they appear in the message.
  • Each part gets a bookmark. Attachment bookmarks are nested under their message.
  • An attached email is merged together with its own attachments.
  • PDF settings such as PDF/A, password, watermark and page selection are applied once to the whole merged PDF.
  • If an attachment can’t be converted, it is skipped and the rest is still merged. The failure is reported through ConversionEvents.OnDocumentFailed.
  • Only PDF is supported as the target format with this option.

To convert each attachment to a separate file instead, see Converting email attachments.

Localize email fields captions

The following code snippet shows how to convert an Email document and localize the Email fields:

var source = "sample.eml";
var loadOptions = new EmailLoadOptions
{
    FieldTextMap = new Dictionary<EmailField, string>
    {
        { EmailField.Subject, "Gegenstand" },
        { EmailField.From, "Von" },
        { EmailField.Attachments, "Anhänge" }
    }
};
using (var converter = new Converter(source, (LoadContext loadContext) => loadOptions))
{
    var options = new PdfConvertOptions();
    converter.Convert("converted.pdf" , options);
}

Control date/time format

The following code snippet shows how to convert an Email document and modify the date/time format:

var source = "sample.eml";
CultureInfo.CurrentCulture = new CultureInfo("fr-FR");
using (var converter = new Converter(source))
{
    var options = new PdfConvertOptions();
    converter.Convert("converted.pdf" , options);
}