Extract attachments
Extracts and saves all attachments from the specified message. Also validates and decrypts the message if appropriate.
Usage
===================================================================== ExtractAttachments.exe ===================================================================== Extracts all attachments from an e-mail message. Supported e-mail formats: .EML (MIME), .MSG (Microsoft Outlook). The program is a sample for Rebex Mail for .NET component. For more info, see http://www.rebex.net/mail.net/ Syntax is: ExtractAttachments.exe <mailfile.eml|mailfile.msg>
More info
The sample demonstrates:
- Loading MailMessage from a local file.
- Iterating through attachment collection.
- Saving attachments to the a local file.
The following code snippet is the core of this sample.
C#
// Load mail message from disk MailMessage mail = new MailMessage(); mail.Load(args[0]); Console.WriteLine( "Message contains {0} attachments.", mail.Attachments.Count ); // If message has no attachments, just exit if (mail.Attachments.Count == 0) return 0; foreach (Attachment attachment in mail.Attachments) { // Save the file Console.WriteLine("Saving '{0}' ({1}).", attachment.FileName, attachment.MediaType); attachment.Save(attachment.FileName); }
VB.NET
' Load mail message from disk Dim mail As New MailMessage mail.Load(args(0)) Console.WriteLine( _ "Message contains {0} attachments.", _ mail.Attachments.Count) ' If message has no attachments, just exit If mail.Attachments.Count = 0 Then Return 0 Dim attachment As Attachment For Each attachment In mail.Attachments ' Save the file Console.WriteLine("Saving '{0}' ({1}).", _ attachment.FileName, attachment.MediaType) attachment.Save(attachment.FileName) Next attachment