Convert each email attachment to different format

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 a flexible API to control the conversion of documents that contain other documents.

The following code snippet shows how to convert each attachment to a different format based on attachment type:

const fs = require('fs').promises;

const emailLoadOptions = new groupdocs.conversion.EmailLoadOptions();
emailLoadOptions.setConvertOwned(true);
emailLoadOptions.setConvertOwner(true);
emailLoadOptions.setDepth(2);

const converter = new groupdocs.conversion.Converter("sample_with_attachments.eml", emailLoadOptions);

const convertOptions = new groupdocs.conversion.PdfConvertOptions();

convertOptions.setPassword("12345");
convertOptions.setDpi(300);

const fileOutputStreams = [];
try{
    await converter.convert(async (t) => {
        try {
            const fileOutputStream = await fs.open(`converted-${fileOutputStreams.length}.pdf`, 'w');
            fileOutputStreams.push(fileOutputStream);
            return fileOutputStream;
        } catch (e) {
            throw new Error(e);
        }
    }, options);
} finally {
    try {
        for (const fileOutputStream of fileOutputStreams) {
            await fileOutputStream.close();
        }
    } catch (e) {
        // throw an exception
    }
}