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.
- 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.
C#
Copy Codeusing Dart.PowerTCP.Mail;Visual Basic
Copy CodeImports Dart.PowerTCP.Mail - Add a button to the form.
- 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"
- 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");
- 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")
- 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.