Composing Messages  
 

The SocketTools Mail Message control is designed to give the developer access to the internal structure of a mail message and enable you to easily create new messages or modify existing messages. To understand how this control can be used, it's useful to understand how a message is actually formatted. Here is an example of a simple, plain text email 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=utf-8

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 control's 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.

The following is list of the most commonly used properties read, create or modify a message:

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).
Content-ID The content identifier for the current message part.
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 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.
Organization The name of the sender's organization or company.
Part The current message part in a multipart message.
PartCount The number of parts in a multipart message.
Priority The current message priority.
Recipient The address of one of the message recipients.
Recipients The number of recipients for the current message.
ReplyTo The address to which replies should be sent.
Subject The subject of the current message.
Text The text in the current message part.
TimeZone The current timezone offset for the local system.
To One or more message recipients.

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.

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

Method Description
AppendMessage Append text to the current message.
AttachFile Attach a file to the current message.
ClearMessage Clear the contents of the current message.
ComposeMessage Compose a new 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 which would create a short message:

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

The resulting message would look like this:

From: johndoe@company.com
To: janedoe@company.com
Date: Fri, 01 Nov 2002 12:00:00 -0800
Subject: This is the message subject
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit

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:

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

The ComposeMessage method has the following arguments:

Property Description
From A string value which specifies the email address of the person sending the message. This argument is required.
To A string value which specifies one or more email addresses of those who will receive the message. Multiple addresses may be separated by a comma (such as "johndoe@company.com, janedoe@company.com"). This argument is required.
Cc An optional string value which specifies recipients who should receive a copy of the message. Multiple recipients may be separated by a comma and the addresses are included in the header of the message.
Bcc An optional string value which specifies recipients who should receive a copy of the message, however, these addresses are not included in the header of the message. Multiple recipients may be separated by a comma.
Subject An optional string value which specifies the subject of the message. If the argument is not specified then the message is created without a subject.
MessageText An optional string value which specifies the body of the message. If the argument is not specified, then the message is created without a body.
MessageHTML An optional string value which specifies an HTML version of the message. If this argument is provided along with the MessageText argument, then a multipart message is created which contains both plain text and HTML versions of the message. If the MessageText argument has not been specified, then only an HTML message is created. If this argument is omitted, then the message is sent with only a plain text body.
CharacterSet An optional integer value which specifies a character set to use when composing the message. This typically only needs to be set for languages which use extended characters. For more information on the available character sets, consult the technical reference.
EncodingType An optional integer value which specifies an encoding type to be used with the character set that was selected. For more information about the encoding types available, consult the technical reference.

Once the message has been created, it can be further modified by setting properties or calling methods such as SetHeader. Note that you are not restricted to changing only certain header fields. You can create, modify or delete any header in the message that you wish. You can also add your own custom header fields if you wish.

Now that a simple message has been created, let's attach a file to the message. This can be easily done using the AttachFile method:

MailMessage1.AttachFile "c:\temp\image.gif"

Although this is a simple operation, it makes some significant changes to the message (some portions of the attachment data has been omitted):

From: johndoe@company.com
To: janedoe@company.com
Date: Fri, 01 Nov 2002 12:00:00 -0800
Subject: This is the message subject
MIME-Version: 1.0
Content-Type: multipart/mixed;
              boundary="----=_ST4020_0001_0BCF2D17_179E5A2E"
Content-Transfer-Encoding: 8bit

This is a multipart message in MIME format.

------=_ST4020_0001_0BCF2D17_179E5A2E
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit

This is an example of a new message.
------=_ST4020_0001_0BCF2D17_179E5A2E
Content-Type: image/gif
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="image.gif"
Content-Length: 6434

R0lGODlhRgEyAPcAAJaWqqKitp6esqamuqamtrKywq6uvsrK176+yrq6xra2wsbG0sLCztLS
287O18LCytvb49fX38rK0sbGzt/f5/f3+/Pz9+/v8+vr7+fn6+Pj59/f49vb311hil1hhmFl
uXvrtV8rFWQ7l2qbuZJrt3rLFomrFovruRRhq4bbuZwbu1hLubBbuZF7tn7WuE2RurLWu7DL
u3nrt7Kbr3LbM+Pnu6hrFC2atcfburarucL7uY/Lurl7uVpLu5CLvJyLu9obvZO7u900W7jA
K7jkW72maxHGC72kWxbAu7xuq77DS7ip67qgl77va73fqxGryxDMK7/m67zgixEBAQA7

------=_ST4020_0001_0BCF2D17_179E5A2E--

The message has now become a multipart message that contains both human-readable text as well as data for the file attachment. Rather than having a single group of headers followed by a message body, the message is now broken into sections, each with its own group of headers and body. Each of these sections are called a message part, and can be accessed individually using the Part property. Each message part is identified by a part number which starts at zero and increases for each subsequent part. Part 0 of this message consists of the following:

From: johndoe@company.com
To: janedoe@company.com
Date: Fri, 01 Nov 2002 12:00:00 -0800
Subject: This is the message subject
MIME-Version: 1.0
Content-Type: multipart/mixed;
              boundary="----=_ST4020_0001_0BCF2D17_179E5A2E"
Content-Transfer-Encoding: 8bit
      
This is a multipart message in MIME format.

Part 0 of any message always refers to the headers and body of the main message. In the previous message, part 0 contains the entire message. Here, part 0 consists primarily of headers and a brief message that this is now a multipart message. This is automatically done for the benefit of older mail clients which may not understand a MIME formatted message, so the user has a message that at least identifies what the message is. Another thing that has changed is the value of the Content-Type header. In the previous message it had a value of "text/plain; charset=utf-8" which tells the mail client that this is a plain text message. With the file attachment, this has changed to a type called "multipart/mixed" which indicates that the message contains multiple parts with mixed types of information. The boundary value is what is used to actually designate the different parts of the message. As the message is being processed, the mail client knows that it has found a new message part when the boundary string is encountered.

The next part of the message, part 1, contains the message that was in the original version of the message:

Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
This is an example of a new message

Note that the content type is back to plain text, just as it was with the original. When a mail client processes a message, it scans the message for plain text message parts which contain information to be displayed to the user.

The last part of the message, part 2, contains the actual file data that was attached to the message:

Content-Type: image/gif
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="image.gif"
Content-Length: 6434

R0lGODlhRgEyAPcAAJaWqqKitp6esqamuqamtrKywq6uvsrK176+yrq6xra2wsbG0sLCztLS
287O18LCytvb49fX38rK0sbGzt/f5/f3+/Pz9+/v8+vr7+fn6+Pj59/f49vb311hil1hhmFl
uXvrtV8rFWQ7l2qbuZJrt3rLFomrFovruRRhq4bbuZwbu1hLubBbuZF7tn7WuE2RurLWu7DL
u3nrt7Kbr3LbM+Pnu6hrFC2atcfburarucL7uY/Lurl7uVpLu5CLvJyLu9obvZO7u900W7jA
K7jkW72maxHGC72kWxbAu7xuq77DS7ip67qgl77va73fqxGryxDMK7/m67zgixEBAQA7

Here the Content-Type header tells the mail client that this is an image file in the GIF format. The other header fields in this message part are used by applications to extract the file attachment once it has been delivered to the recipient. Because email messages must be sent over systems which may not be able to handle binary data, the image file data has been encoded using a standard algorithm called base64. This algorithm converts binary data into plain 7-bit text data that can be safely exchanged with other mail servers. The process of encoding and decoding attachments is automatically handled by the control when the file is attached. The ExtractFile method is essentially the reverse of the AttachFile method, automatically decoding and storing a file attachment on the local system.