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.
using System.IO;
using System.Collections.Generic;
using GroupDocs.Viewer;
using GroupDocs.Viewer.Results;
// ...

using (Viewer viewer = new Viewer("with_attachments.msg"))
{
    // Get list of attachments.
    IList<Attachment> attachments = viewer.GetAttachments();
    // Save each attachment to the output folder.
    Directory.CreateDirectory("output");
    foreach (Attachment attachment in attachments)
    {
        string filePath = Path.Combine("output", attachment.FileName);
        using (FileStream stream = File.Create(filePath))
        {
            viewer.SaveAttachment(attachment, stream);
        }
    }
}      
Imports System.IO
Imports System.Collections.Generic
Imports GroupDocs.Viewer
Imports GroupDocs.Viewer.Results
' ...

Module Program
    Sub Main(args As String())
        Using viewer As Viewer = New Viewer("with_attachments.msg")
            ' Get list of attachments.
            Dim attachments As IList(Of Attachment) = viewer.GetAttachments()
            ' Save each attachment to the output folder.
            Directory.CreateDirectory("output")
            For Each attachment As Attachment In attachments
                Dim filePath As String = Path.Combine("output", attachment.FileName)
                Using stream As FileStream = File.Create(filePath)
                    viewer.SaveAttachment(attachment, stream)
                End Using
            Next
        End Using
    End Sub
End Module

with_attachments.msg is the sample file used in this example. Click here to download it.

output/delivery-note-0912.xlsx (7 KB)
output/invoice-NWR-2026-0912.pdf (194 KB)

Download full output