Instantiate the Viewer object. Specify a file that contains attachments.
Call the GetAttachments method. It returns the attachment collection.
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.
usingSystem.IO;usingSystem.Collections.Generic;usingGroupDocs.Viewer;usingGroupDocs.Viewer.Results;// ...using(Viewerviewer=newViewer("with_attachments.msg")){// Get list of attachments.IList<Attachment>attachments=viewer.GetAttachments();// Save each attachment to the output folder.Directory.CreateDirectory("output");foreach(Attachmentattachmentinattachments){stringfilePath=Path.Combine("output",attachment.FileName);using(FileStreamstream=File.Create(filePath)){viewer.SaveAttachment(attachment,stream);}}}
ImportsSystem.IOImportsSystem.Collections.GenericImportsGroupDocs.ViewerImportsGroupDocs.Viewer.Results' ...
ModuleProgramSubMain(argsAsString())UsingviewerAsViewer=NewViewer("with_attachments.msg")' Get list of attachments.
DimattachmentsAsIList(OfAttachment)=viewer.GetAttachments()' Save each attachment to the output folder.
Directory.CreateDirectory("output")ForEachattachmentAsAttachmentInattachmentsDimfilePathAsString=Path.Combine("output",attachment.FileName)UsingstreamAsFileStream=File.Create(filePath)viewer.SaveAttachment(attachment,stream)EndUsingNextEndUsingEndSubEndModule
with_attachments.msg is the sample file used in this example. Click here to download it.