Rebex



Sample: Extract Attachments for Pocket PC and Windows Mobile

Extracts and saves all attachments from the specified message. Also validates and decrypts the message if appropriate.

screenshot

More info

The sample demonstrates:

  • Loading MailMessage from a local file
  • Iterating through attachment collection
  • Saving attachments to the a local file
  • 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: