Save attachments

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.Viewer 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-viewer -- dnx GroupDocs.Viewer.Mcp --yes
Folders, licensing and more →

To get and save attachments, follow these steps:

  1. Instantiate the Viewer object. Specify a file that contains attachments.
  2. Call the getAttachments method. It returns the attachment collection.
  3. Iterate through the collection. To save an attachment, call the saveAttachment method.

The following code snippet shows how to get and save all attachments from the MSG file:

Note
NOTE: provided code snippet suits all format families that support attachments: emails, Outlook data files, archives, and PDF documents.
import com.groupdocs.viewer.Viewer;
import com.groupdocs.viewer.results.Attachment;
// ...

try (Viewer viewer = new Viewer("with_attachments.msg")) {
    List<Attachment> attachments = viewer.getAttachments();

    for (Attachment attachment : attachments) {
        FileOutputStream fileStream = new FileOutputStream(attachment.getFileName());

        viewer.saveAttachment(attachment, fileStream);
    }
}