How to extract and save attachments
You can save/export attachments from Email documents, Outlook data files, Archives and PDF documents with only a few lines of code using GroupDocs.Viewer for Java API.
Follow these steps to get and save (export) attachments:
- Instantiate Viewer object for the file that contains attachment(s);
- Call getAttachments() method which will return document attachments collection;
- Iterate through attachments collection and save attachment by calling saveAttachment(…) method.
Following example demonstrates on how to get and save attachments from MSG files.
String outputPath = "C:\\output";
Viewer viewer = new Viewer("with_attachments.msg");
List<Attachment> attachments = viewer.getAttachments();
for (Attachment attachment : attachments) {
String filePath = Path.combine(outputPath, attachment.getFileName());
viewer.saveAttachment(attachment.getFileName(), new FileOutputStream(filePath));
}
NOTE: provided code example is actual for all document types that support attachments - Email documents, Outlook data files, Archives and PDF documents.
More resources
Advanced Usage Topics
To learn more about document viewing features, please refer to the advanced usage section.
GitHub Examples
You may easily run the code above and see the feature in action in our GitHub examples:
- GroupDocs.Viewer for Java examples, plugins, and showcase
- Document Viewer for .NET App WebForms UI Modern Example
- Document Viewer for Java App Dropwizard UI Modern Example
- Document Viewer for Java Spring UI Example
- GroupDocs.Viewer for .NET samples, plugins and showcase
- Document Viewer for .NET MVC UI Example
Free Online App
Along with full-featured Java library we provide simple but powerful free Apps. You are welcome to view Word, PDF, Excel, PowerPoint documents with free to use online GroupDocs Viewer App.