PowerTCP Mail for .NET
Storing and Loading Messages
Send comments on this topic.



Glossary Item Box

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.

  1. Add the Smtp component to a new form.
  2. 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 Code
    using Dart.PowerTCP.Mail;
    using System.IO;
    Visual Basic Copy Code
    Imports Dart.PowerTCP.Mail
    Imports System.IO
  3. Add a button to the form.
  4. 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"
  5. 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()
  6. 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))
  7. 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)
  8. Compile and run the application.
Documentation Version 3.2
© 2010 Dart Communications. All Rights Reserved.