It may be necessary to "save" a message to a storage medium, or to "load" a message from a storage medium. PowerTCP fully supports this. Messages can be "saved" using the Store method. Messages can be "loaded" by passing in the Stream containing the message into the MessageStream constructor.
To store and load a message, 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.
- Create a MessageStream object. This is the object that will be "stored". 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"
- Store the message to a file. You may wish to store a message so you can look at the source, or perhaps you may wish to store it to reload it into a new MessageStream in the future.
C#
Copy Code// Store the message FileStream f = new FileStream("C:\\storedmessage.msg", FileMode.Create); msg.Store(f); f.Close();
Visual Basic
Copy Code' Store the message Dim f As New FileStream("C:\\storedmessage.msg", FileMode.Create) msg.Store(f) f.Close()
- Load the message from the file. This will re-create the MessageStream object, allowing you to use the MessageStream within your application.
C#
Copy Code// Reload the message msg = new MessageStream(new FileStream("C:\\storedmessage.msg", FileMode.Open));
Visual Basic
Copy Code' Reload the message msg = New MessageStream(New FileStream("C:\\storedmessage.msg", FileMode.Open))
- 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.