Load Email document with options

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
DisplayBccEmailAddressOption to display or hide “Bcc” email address
DisplayCcEmailAddressOption to display or hide “Cc” email address
DisplayEmailAddress Option to display or hide email address
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)
ResourceLoadingTimeoutSpecifies the timeout of loading the external resources.
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:

With v24.10 and later:

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);
}

Before v24.10:

Func<LoadOptions> getLoadOptions = () => 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:

With v24.10 and later:

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);
}

Before v24.10:

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, () => loadOptions))
{
    var index = 1;
    var options = new PdfConvertOptions();
    // Note: index = 1 is the email itself, all following indexes are attachments
    converter.Convert(() => new FileStream($"converted-{index++}.pdf", FileMode.Create) , options);
}

Localize email fields captions

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

With v24.10 and later:

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);
}

Before v24.10:

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, () => 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);
}