MIME attachments are represented by MimeAttachmentStream objects, and are managed by use of the Message.Attachments property, a collection of MimeAttachmentStreams. Due to the stream-based approach of PowerTCP Mail for .NET, adding attachments is very flexible. The source of attachments can be a file, a MemoryStream, or any other type of .NET Framework Stream. As a result, the ways an attachment can be created is limitless. This topic demonstrates three ways of creating an attachment.
To send an email with attachments, use the following steps.
- Add the Smtp 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. Also, import System.IO as a FileStream will be used in this example.
C#
Copy Codeusing Dart.PowerTCP.Mail; using System.IO;
Visual Basic
Copy CodeImports Dart.PowerTCP.Mail Imports System.IO
- Add a button to the form.
- Email messages are represented by MessageStream objects. Create a MessageStream object. Place the following code in the Click event of the button added to the form in step 3.
C#
Copy Code// Create a MessageStream MessageStream msg = new MessageStream(); // Set "To", "From", and "Subject" msg.To.Add(new MailAddress("you@yourserver.com")); msg.From = new MailAddress("me@myserver.com"); msg.Subject = "Test Message"; // Set the message text msg.Text = "This is a test message";
Visual Basic
Copy Code' Create a MessageStream Dim msg As New MessageStream() ' Set "To", "From", and "Subject" msg.To.Add(New MailAddress("you@yourserver.com")) msg.From = New MailAddress("me@myserver.com") msg.Subject = "Test Message" ' Set the message text msg.Text = "This is a test message"
In this example, three different attachments will be added, demonstrating three different techniques. First, create a MimeAttachmentStream object to use to encapsulate the attachments.
C#
Copy CodeMimeAttachmentStream attach = null;Visual Basic
Copy CodeDim attach As MimeAttachmentStream = Nothing
- The first technique demonstrated passes a string filename into the MimeAttachmentStream constructor to create the attachment.
C#
Copy Code// Method 1. Add an attachment using the filename attach = new MimeAttachmentStream("C:\\test\\file1.txt"); // Add it to the message. msg.Attachments.Add(attach); attach = null;
Visual Basic
Copy Code' Method 1. Add an attachment using the filename attach = New MimeAttachmentStream("C:\test\file1.txt") ' Add it to the message. msg.Attachments.Add(attach) attach = Nothing
- The second technique demonstrated passes a FileStream into the MimeAttachmentStream constructor to create the attachment.
C#
Copy Code// Method 2. Add an attachment using a FileStream attach = new MimeAttachmentStream(new FileStream("C:\\test\\file2.txt", FileMode.Open)); // Add it to the message. msg.Attachments.Add(attach); attach = null;
Visual Basic
Copy Code' Method 2. Add an attachment using a FileStream attach = New MimeAttachmentStream(New FileStream("C:\test\file2.txt", FileMode.Open)) ' Add it to the message. msg.Attachments.Add(attach) attach = Nothing
- The third technique demonstrated passes a MemoryStream into the MimeAttachmentStream constructor to create the attachment.
C#
Copy Code// Method 3. Add an attachment from data in memory. string s = "This is a new attachment"; byte[] content = System.Text.Encoding.Default.GetBytes(s); MemoryStream m = new MemoryStream(content); attach = new MimeAttachmentStream(m, "file3.txt", TextPlain, QuotedPrintable, ""); // Add it to the message. msg.Attachments.Add(attach); attach = null;
Visual Basic
Copy Code' Method 3. Add an attachment from data in memory. Dim s As String = "This is a new attachment" Dim content As Byte() = System.Text.Encoding.Default.GetBytes(s) Dim m As New MemoryStream(content) attach = New MimeAttachmentStream(m, "file3.txt", TextPlain, QuotedPrintable, "") ' Add it to the message. msg.Attachments.Add(attach) attach = Nothing
- The message is now created and contains 3 attachments. Send the message.
C#
Copy Code// First set the server. smtp1.Server = "mail.test.com"; // Then send. smtp1.Send(msg);
Visual Basic
Copy Code' First set the server. Smtp1.Server = "mail.test.com" ' Then send. Smtp1.Send(msg)
- Compile and run the application. Check the inbox of the email address to which the message was sent. There should be a new message with 3 attachments.