List attachments

GroupDocs.Viewer for Java enables you to retrieve a list of document attachments from your emails, Outlook data files, archives and PDF documents.

Follow these steps to get a list of all attachments:

  • Instantiate Viewer object for the file that contains attachment(s);
  • Call getAttachments() method which will return document attachments collection;
  • Iterate through attachments collection.

Following example demonstrates on how to get all attachments from MSG file.

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

    System.out.println("\nAttachments:");
    for (Attachment attachment : attachments) {
        System.out.println(attachment);
    }
}

After running the code above you will see an output like this:

Attachments:
attachment-image.png - Portable Network Graphic (.png); ID: 07c90715-040d-09c8-100a-c6040e05c507; Path: attachment-image.png; Size: 26754B
attachment-word.doc - Microsoft Word Document (.doc); ID: 01c90715-040d-01c8-100a-c6010e05c501; Path: attachment-word.doc; Size: 224768B
Provided code example is actual for all document types that support attachments - Email documents, Outlook data files, Archives and PDF documents.