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:
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:
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:
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. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Copyright © 2023 Catalyst Development Corporation. All rights reserved. |