When getting mail from a POP server, attachments are decoded in one of two ways:
- Set UseMemoryStreams to false and attachments are decoded and stored as FileStreams.
- Set UseMemoryStreams to true and attachments are decoded and stored as MemoryStreams.
When decoding attachments and storing them as MemoryStreams, the attachments will be found in the Attachments collection of a MessageStream object. This is useful if you do not need to store the attachment to disk.
To decode attachments and save them as MemoryStreams.
- Add the Pop component to a new form.
- Import the namespace of the component you would like to use using "Using" (C#) or "Imports" (VB.NET). Place the following code at the top of your file.
C#
Copy Codeusing Dart.PowerTCP.Mail;Visual Basic
Copy CodeImports Dart.PowerTCP.Mail - Add a button to the form.
- Set the properties required to properly get all messages from the server. Many of these default to the correct value required for getting all messages, but are still demonstrated for emphasis. Place the following code in the Click event of the button added to the form in step 3.
C#
Copy Code// Be sure the Pop component is configured to automatically retrieve messages. pop1.AutoGet = MessageSection.Complete; // Set UseMemoryStreams to true. The attachment will be decoded and // stored in a MemoryStream. pop1.UseMemoryStreams = true;
Visual Basic
Copy Code' Be sure the Pop component is configured to automatically retrieve messages. Pop1.AutoGet = Complete ' Set UseMemoryStreams to true. The attachment will be decoded and ' stored in a MemoryStream. pop1.UseMemoryStreams = true
- Login to the server, all messages will be automatically retrieved. After login successfully occurs, logout will automatically occur (since Pop.AutoLogout = true). Place this code directly after the code from step 5.
C#
Copy Code// Login to the POP server. pop1.Login("mail.myserver.com", "test", "pass");
Visual Basic
Copy Code' Login to the POP server. Pop1.Login("mail.myserver.com", "test", "pass")
- At this point, all attachments are accessible as MemoryStreams. This is useful if you want to use these attachments in memory only. For example, perhaps you wish to display a "text/plain" attachment in a TextBox. For the purposes of this example, simply find the first "text/plain" attachment and display it in a TextBox.
C#
Copy Code// Create byte array for holding attachment data in memory. byte[] buffer = null; // Iterate through all messages foreach(PopMessage msg in pop1.Messages) { // Iterate through all attachments for this message. foreach(AttachmentStream att in msg.Message.Attachments) { // Get the data into a byte array. buffer = new byte[att.Length]; att.Position = 0; att.Read(buffer); // Display attachment data within the application Debug.WriteLine("Filename = " + att.FileName); Debug.WriteLine("File Data Follows"); Debug.WriteLine(System.Text.Encoding.Default.GetString(buffer)); } }
Visual Basic
Copy Code' Create a byte array to hold the attachment data. Dim buffer() As Byte ' Iterate through all messages. Dim msg As PopMessage For Each msg In Pop1.Messages ' Iterate through all attachments for the message. Dim att As AttachmentStream For Each att In msg.Message.Attachments ' Get the data from the AttachmentStream buffer(att.Length) = New Byte() att.Position = 0; att.Read(buffer); ' Display attachment data within the application Debug.WriteLine("Filename = " + att.FileName) Debug.WriteLine("File Data Follows") Debug.WriteLine(System.Text.Encoding.Default.GetString(buffer)) Next Next