Skip to content

Instantly share code, notes, and snippets.

@conholdate-gists
Created November 15, 2021 06:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save conholdate-gists/7e1594cdf39a2d6efa1c01708fd02bb4 to your computer and use it in GitHub Desktop.
Save conholdate-gists/7e1594cdf39a2d6efa1c01708fd02bb4 to your computer and use it in GitHub Desktop.
Save Attachments from Emails using C#
string outputPath = @"C:\Files\Viewer\";
// Initialize API and load the MSG file
Viewer viewer = new Viewer(@"C:\Files\Viewer\with_attachments.msg");
// Get attachments
IList<Attachment> attachments = viewer.GetAttachments();
foreach (Attachment attachment in attachments)
{
// Save attachment
string filePath = Path.Combine(outputPath, attachment.FileName);
viewer.SaveAttachment(attachment, File.OpenWrite(filePath));
}
// Initialize attachment
Attachment attachment = new Attachment("attachment-word.doc", "attachment-word.doc");
MemoryStream attachmentStream = new MemoryStream();
// Initialize API and load MSG file
using (Viewer viewer = new Viewer(@"C:\Files\Viewer\with_attachments.msg"))
{
// Save attachment in stream
viewer.SaveAttachment(attachment, attachmentStream);
}
// Initialize API and load attachment stream
using (Viewer viewer = new Viewer(attachmentStream))
{
// Define PDF view options
PdfViewOptions viewOptions = new PdfViewOptions("C:\\Files\\Viewer\\output.pdf");
// View as PDF
viewer.View(viewOptions);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment