Rebex

Skip to content, Skip to navigation




Sample: 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 in .EML (MIME) format.

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>

More info

The sample demonstrates:

  • Loading MailMessage from disk
  • Iterating through attachment collection
  • Saving attachments to the disk
  • Validating signed messages.
  • Decrypting encrypted messages.

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

See also: