PowerTCP Mail for .NET
Adding Header Lines
Send comments on this topic.



Glossary Item Box

PowerTCP Mail for .NET has extensive support for adding header lines to a message. Header lines are automatically set using high-level properties such as To, Subject, etc. Header lines can also be managed using the Header collection, making it easy to add and remove header lines.

To create a message and add headers, 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.
    C# Copy Code
    using Dart.PowerTCP.Mail;
    Visual Basic Copy Code
    Imports Dart.PowerTCP.Mail
  3. Add a button to the form.
  4. Create a MessageStream object. Setting properties of the MessageStream object causes some headers to be automatically created. Place the following code in the Click event of the button added to the form in step 3.
    C# Copy Code
    // Create a message
    MessageStream msg = new MessageStream();
                            
    // Set the "TO:", "FROM:", and "SUBJECT:" header lines using high-level properties. 
    msg.To.Add(new MailAddress("you@yourserver.com"));
    msg.From = new MailAddress("me@myserver.com");
    msg.Subject = "hello";
    msg.Text = "This is a test";
    Visual Basic Copy Code
    ' Create a MessageStream 
    Dim msg As New MessageStream()
    
    ' Set the "TO:", "FROM:", and "SUBJECT:" header lines using high-level properties. 
    msg.To.Add(New MailAddress("you@yourserver.com"))
    msg.From = new MailAddress("me@myserver.com")
    msg.Subject = "hello"
    msg.Text = "This is a test"
  5. Now add a commonly used header line. For this example "Organization:" is used.
    C# Copy Code
    // Add a commonly used header line.
    msg.Header.Add(Organization, "My Company");
    Visual Basic Copy Code
    ' Add a commonly used header line.
    msg.Header.Add(Organization, "My Company");
  6. Now add a custom header line.
    C# Copy Code
    // Add a custom header line.
    msg.Header.Add("X-MYHEADER: My value");
    Visual Basic Copy Code
    ' Add a custom header line.
    msg.Header.Add("X-MYHEADER: My value")
  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.