PowerTCP Mail for .NET
Sending HTML Mail
Send comments on this topic.



Glossary Item Box

PowerTCP Mail for .NET makes it easy to create a complete HTML email message with only a single line of code. The MessageStream object has two constructor overloads which are specifically used to create HTML email messages. Simply create a MessageStream, passing in the filename of the HTML file or a Stream containing HTML data and all associated images and resources will be retrieved and added to the MessageStream, making a ready-to-send HTML message.

To send an HTML email, 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, passing in the filename of the HTML file you wish to send. The MessageStream object will use the relative links contained within the HTML document to gather resources (such as images). Be sure all of these resources are in the correct location. Place the following code in the Click event of the button added to the form in step 3. 

    C# Copy Code
    // Create a MessageStream object, passing in the HTML file
    MessageStream msg = new MessageStream("C:\\html\\index.htm");

    Visual Basic Copy Code
    ' Create a MessageStream object, passing in the HTML file
    Dim msg As New MessageStream("C:\\html\\index.htm")

    Set the other properties of the MessageStream necessary for sending a message.

    C# Copy Code
    // Set "To", "From", and "Subject"
    msg.To.Add(new MailAddress("you@yourserver.com"));
    msg.From = new MailAddress("me@myserver.com");
    msg.Subject = "HTML Message";
    Visual Basic Copy Code
    ' Set "To", "From", and "Subject"
    msg.To.Add(New MailAddress("you@yourserver.com"))
    msg.From = New MailAddress("me@myserver.com")
    msg.Subject = "HTML Message"
  5. 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)
  6. Compile and run the application. Check the inbox of the email address to which the message was sent. There should be a new message containing HTML email.
    Documentation Version 3.2
    © 2010 Dart Communications. All Rights Reserved.