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 .NET 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";
using (Viewer viewer = new Viewer("with_attachments.msg"))
{
    IList<Attachment> attachments = viewer.GetAttachments();
    foreach(Attachment attachment in attachments)
    {
        string filePath = Path.Combine(outputPath, attachment.FileName);  
        viewer.SaveAttachment(attachment, File.OpenWrite(filePath)); 
    }
}