Catalyst Internet Mail .NET

Composing Messages

Many of the component properties are designed to give the developer access to the internal structure of the current message. To understand how these properties can be used, it's useful to understand how a message is actually formatted. Here is an example of a simple, plain text e-mail message:

From: John Doe <johndoe@company.com>
To: Jane Doe <janedoe@company.com>
Date: Mon, 1 Jul 2002 12:00:00 -0800 (PST)
Subject: Meeting scheduled for next week
Message-ID: <20020601200000.15637@mail01.company.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii

I wanted to confirm that you would be able to attend the meeting.
If there are any scheduling conflicts, please let me know.

The first thing that is apparent is that the message has two discrete sections. The first section consists of one or more header fields, followed by a colon and then a value. The second section contains the body of the message, with the headers and body separated by a single blank line.

Therefore, using this example message, reading the value of From property would return the string "John Doe <johndoe@company.com>", which is the address of the person who sent the message. To change the From header field, simply set the From property to a new string value. Most of the message-related properties correspond to specific header fields, such as To, From and Subject. Reading those properties return their respective header values while setting them changes their value in the current message.

Message Properties

Property Description
Attachment The name of a file attachment in the current message part.
Bcc One or more message recipients (blind carbon copy).
Cc One or more message recipients (carbon copy).
ContentLength The size of the current message part in bytes.
ContentType The content type for the current message part.
Date The date for the current message.
Encoding The encoding type for the current message part.
From The sender of the message.
Localize Enable or disable message time localization.
Mailer The name of the application that generated the message.
Message The complete message, including headers and body.
MessageID A unique identifier string from the current message.
MessagePart The current message part in a multipart message.
MessageParts The number of parts in a multipart message.
MessageText The text in the current message part.
Organization The name of the sender's organization or company.
Priority The current message priority.
Recipient The address of one of the specified message recipient.
Recipients The number of recipients for the current message.
ReplyTo The address to which replies should be sent.
ReturnReceipt The address to which a return-receipt message should be sent.
Subject The subject of the current message.
TimeZone The current timezone offset for the local system.
To One or more message recipients.

For more complex message processing such as attaching files or creating multipart messages, there are a number of methods which can be used to manage the current message:

Message Methods

Property Description
AppendMessage Append text to the current message.
AttachFile Attach a file to the current message.
ComposeMessage Compose a new message, replacing the current message.
ClearMessage Clear the contents of the current message.
CreatePart Create a new message part in a multipart message.
DeleteHeader Delete a header from the message.
DeletePart Delete a message part.
ExportMessage Export the complete message to a text file.
ExtractFile Extract a file attachment.
GetFirstHeader Return the first header in the current message part.
GetHeader Return the value of a specified header field.
GetNextHeader Return the next header in the current message part.
ImportMessage Import a message from a text file.
ParseMessage Parse a string, adding the contents to the current message.
SetHeader Set the value of the specified header field.

The header related methods such as GetHeader and SetHeader, enable an application to read, create or modify any header field regardless of whether or not there is a predefined property value for it. Because there can be a potentially unlimited number of header fields in a message, these methods give the developer more control over the header portion of the message.

New messages can be created by setting properties which comprise the message. Here is an example of some Visual Basic code which would create a short message:

InternetMail1.From = "johndoe@company.com"
InternetMail1.To = "janedoe@company.com"
InternetMail1.Date = Date
InternetMail1.Subject = "This is the message subject"
InternetMail1.MessageText = "This is an example of a new message"

The resulting message would look like this:

From: johndoe@company.com
To: janedoe@company.com
Date: Thu, 04 Oct 2007 12:00:00 -0800
Subject: This is the message subject
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

This is an example of a new message.

Note that in addition to those properties that were set, there were a number of additional header fields such as MIME-Version and Content-Type that were automatically created.

Although setting properties is one way to create a new message, it involves writing a fair amount of code. There is a simpler way to do it using a single method called ComposeMessage. The equivalent code would look like this:

InternetMail1.ComposeMessage("johndoe@company.com", _
                             "janedoe@company.com", _
                             "This is the message subject", _
                             "This is an example of a new message")

Once the message has been created, it can be further modified by setting properties or calling methods such as SetHeader. From this point, the message could be further modified by calling the AttachFile method to attach the contents of a specified file to the message.