PowerTCP Mail for .NET
Making an Smtp Trace Log
Send comments on this topic.



Glossary Item Box

Anytime data is sent or received over the TCP connection, the Trace event is raised. Made accessible to this event is the data sent. The Trace event makes it easy to create a text log of data sent/received.

To create a log of commands, 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. The Trace event will be raised when data is sent or received. "Wire-up" the Trace event. Add a button to the form.
  4. Send a quick message. Place the following code in the Click event of the button added to the form in step 3.
    C# Copy Code
    // Set the server
    smtp1.Server = "mail.test.com";
    
    // Send a message
    smtp1.Send("test@yourserver.com", "me@myserver.com", "test", "This is a test.");
    Visual Basic Copy Code
    ' Set the server
    Smtp1.Server = "mail.test.com"
    
    ' Send a message
    Smtp1.Send("test@yourserver.com", "me@myserver.com", "test", "This is a test.")
  5. Add code to the Trace event to display the data sent/received.
    C# Copy Code
    private void smtp1_Trace(object sender, Dart.PowerTCP.Mail.SegmentEventArgs e)
    {
       // Get the event data into a byte array.
       byte[] buffer = System.Text.Encoding.Default.GetBytes(e.Segment.ToString());
                            
       // Create a FileStream
       FileStream f = new FileStream("C:\\temp\\smtplog.log", FileMode.Append);
    
       // Write the data to the stream
       f.Write(buffer, 0, buffer.Length);
    
       // Close the FileStream
       f.Close();
    }
    Visual Basic Copy Code
    Private Sub Smtp1_Trace(ByVal sender As Object, ByVal e As Dart.PowerTCP.Mail.SegmentEventArgs) Handles Smtp1.Trace
       ' Get the event data into a byte array.
       Dim buffer As Byte() = System.Text.Encoding.Default.GetBytes(e.Segment.ToString())
    
       ' Create a FileStream
       Dim f As New FileStream("C:\temp\smtplog.log", FileMode.Append)
    
       ' Write the data to the stream
       f.Write(buffer, 0, buffer.Length)
    
       ' Close the FileStream
       f.Close()
    End Sub
  6. Compile and run the application. After it successfully runs, check the log file. You should be able to see the communication required to get all of the messages.
Documentation Version 3.2
© 2010 Dart Communications. All Rights Reserved.