Using Caché Internet Utilities
Sending and Receiving Email
|
|
This chapter describes how you can use Caché to send and receive MIME email messages. It discusses the following topics:
Also see the class documentation for examples and extensive comments.
Note:
The examples in this chapter are organized so that the methods for managing email messages can be used with different email servers, which is useful during testing and demonstrations. This is not necessarily the code organization that is most suitable for production needs.
Email sends messages across the Internet using standard protocols. Caché supports three of these protocols, as follows:
-
-
You can send email via an SMTP server. SMTP (Simple Mail Transport Protocol) is the Internet standard for sending email.
-
You can also retrieve email from an email server via POP3, the most common standard for retrieving email from remote servers.
Note:
Caché does not provide a mail server. Instead, it provides the ability to connect to and interact with mail servers.
First, it is useful to understand how Caché represents MIME email messages.
In general, a multipart MIME message consists of the following pieces:
-
A set of
message headers, each of which contains information such as the address to which the message is sent. This also includes the
Mime-Type header and the
Content-Type header for the entire message.
-
-
-
A
body, which is either text or binary, and which can be in a different character set than the bodies of other parts.
The following sections provide details.
-
-
-
To The list of email addresses to which this message will be sent. This property is a standard Caché list class; to work with it, you use the standard list methods:
Insert(),
GetAt(),
RemoveAt(),
Count(), and
Clear().
-
From The email address this message is sent from.
-
Subject The subject of the message, if this is required by the SMTP server you are using.
-
-
If the message is not plain text, set the following properties to indicate the kind of message you are creating:
-
If this is an HTML message, set the
IsHTML property to 1.
-
If this is a binary message, set the
IsBinary property to 1.
-
Important:
It is important to specify the character set
before you add the contents of the message.
-
Add the contents of the message:
Tip:
When you specify the
Filename property of the stream, be sure to use a directory to which the users will have write access.
Note:
You should be aware of the requirements of the SMTP server that you are using. For example, some SMTP servers require that you include a
Subject header. Similarly, some SMTP servers do not permit arbitrary
From headers.
Similarly, some SMTP servers recognize the
Priority header and others recognize
X-Priority instead.
The following method creates a simple message and specifies the addresses for it:
ClassMethod CreateTextMessage() As %Net.MailMessage
{
Set msg = ##class(%Net.MailMessage).%New()
set msg.From = "test@test.com"
Do msg.To.Insert("xxx@xxx.com")
Do msg.Cc.Insert("yyy@yyy.com")
Do msg.Bcc.Insert("zzz@zzz.com")
Set msg.Subject="subject line here"
Set msg.IsBinary=0
Set msg.IsHTML=0
Do msg.TextData.Write("This is the message.")
Quit msg
}
ClassMethod SimpleMessage() As %Net.MailMessage
{
Set msg = ##class(%Net.MailMessage).%New()
Set msg.Subject="Simple message "_$h
Set msg.IsBinary=0
Set msg.IsHTML=0
Do msg.TextData.Write("This is the message.")
Quit msg
}
To create a multipart email message:
-
-
-
-
-
For the parent email message, set the
Parts property, which is an array. Insert each child message part into this array.
As noted previously, both the message itself and each part of a message has a set of headers.
The headers of a given message part are in the character set specified by the
Charset property of that part.
Note:
You should be aware of the requirements of the SMTP server that you are using. For example, some SMTP servers require that you include a
Subject header. Similarly, some SMTP servers do not permit arbitrary
From headers.
Similarly, some SMTP servers recognize the
Priority header and others recognize
X-Priority instead.
-
To (Required) The list of email addresses to which this message will be sent. This property is a standard Caché list; to work with it, you use the standard list methods:
Insert(),
GetAt(),
RemoveAt(),
Count(), and
Clear().
-
From (Required) The email address this message is sent from.
-
Date The date of this message.
-
Subject (Required) A string containing the subject for this message.
-
Sender The actual sender of the message.
-
Cc The list of carbon copy addresses to which this message will be sent.
-
Bcc The list of blind carbon copy addresses to which this message will be sent.
When you send the message, the
Content-Type header for the message and for each message part is automatically set as follows:
The default is as follows:
-
For a binary message or message part:
"base64"
-
You use this property to contain additional headers such as
X-Priority and others. For example:
do msg.Headers.SetAt(1,"X-Priority")
do msg.Headers.SetAt("High","X-MSMail-Priority")
do msg.Headers.SetAt("High","Importance")
Different email servers and clients recognize different headers, so it can be useful to set multiple similar headers to be sure that the server or client receives a message with a header it can recognize.
Each of these methods adds the attachment to the
Parts array of the original message (or message part), and automatically sets the
IsMultiPart property to 1.
method AttachFile(Dir As %String,
File As %String,
isBinary As %Boolean = 1,
charset As %String = "",
ByRef count As %Integer) as %Status
Attaches the given file to the email message. By default the file is sent as a binary attachment, but you can specify instead that it is text. You can also specify the character set that the file uses if it is text.
This method also sets the
Dir and
FileName properties of the message or message part.
method AttachStream(stream As %Stream.Object,
Filename As %String,
isBinary As %Boolean = 1,
charset As %String = "",
ByRef count As %Integer) as %Status
Attaches the given stream to the email message. The attachment is considered a file attachment if
Filename is specified. Otherwise it is considered an inline attachment. See the comments for
AttachFile().
method AttachNewMessage() as %Net.MailMessagePart
method AttachEmail(mailmsg As %Net.MailMessage)
ClassMethod MessageWithAttachment() As %Net.MailMessage
{
Set msg = ##class(%Net.MailMessage).%New()
Set msg.Subject="Message with attachment "_$h
Set msg.IsBinary=0
Set msg.IsHTML=0
Do msg.TextData.Write("This is the main message body.")
//add an attachment
Set status=msg.AttachFile("c:\", "GNET.pdf")
If $$$ISERR(status) {
Do $System.Status.DisplayError(status)
Quit $$$NULLOREF
}
Quit msg
}
If you have access to an SMTP server, you can send email messages. The SMTP server must be running and you must have the needed permissions to use it. To send email, do the following:
-
Create an instance of
%Net.SMTP and set its properties as needed, especially the following:
-
smtpserver is the name of the SMTP server you are using.
-
port is the port you are using on the SMTP server; the default is 25.
-
This object describes the SMTP server you will use.
-
If the SMTP server requires authentication, specify the necessary credentials. To do so:
-
-
-
-
-
To use an SSL/TLS connection to the SMTP server:
-
Set the
SSLConfiguration property to the name of the activated SSL/TLS configuration to use.
-
In most cases, use the value 0. Use the value 1 for the case in which the server interaction begins on a normal TCP socket and then switches to TLS on the same port as the normal socket. For details, see
RFC 3207.
-
Optionally set the
SSLCheckServerIdentity property to 1. Do this if you want to verify the host server name in the certificate.
-
-
Call the
Send() method of your SMTP instance. This method returns a status, which you should check.
-
If the returned status indicates an error, check the
Error property, which contains the error message itself.
-
Check the
FailedSend property, which contains a list of email addresses for which the send action failed.
The examples in the following sections use a couple of different free SMTP services that were available at the time this manual was written. No particular endorsement is implied by the selection of these services. Also note that the examples do not show the actual passwords.
Important:
%Net.SMTP writes the message body into a temporary file stream. By default, this file is written to the namespace directory and if the directory requires special write permissions, the file is not created and you get an empty message body.
You can define a new path for these temporary files and choose a path that does not restrict write access (for example, /tmp). To do so, set the global node
%SYS("StreamLocation",namespace) where
namespace is the namespace in which your code is running. For example:
Set ^%SYS("StreamLocation","SAMPLES")="/tmp"
This example consists of two methods that you use together. The first creates an instance of
%Net.SMTP that uses a test account that has already been set up on the HotPOP SMTP server:
ClassMethod HotPOPAsSMTP() As %Net.SMTP
{
Set server=##class(%Net.SMTP).%New()
Set server.smtpserver="smtp.hotpop.com"
//HotPOP SMTP server uses the default port (25)
Set server.port=25
//Create object to carry authentication
Set auth=##class(%Net.Authenticator).%New()
Set auth.UserName="isctest@hotpop.com"
Set auth.Password="123pass"
Set server.authenticator=auth
Set server.AuthFrom=auth.UserName
Quit server
}
The next method sends a simple, unique message, using an SMTP server that you provide as the argument:
ClassMethod SendSimpleMessage(server As %Net.SMTP) As %List
{
Set msg = ##class(%Net.MailMessage).%New()
Set From=server.authenticator.UserName
Set:From="" From="xxx@xxx.com"
Set msg.From = From
Do msg.To.Insert("xxx@xxx.com")
//Do msg.Cc.Insert("yyy@yyy.com")
//Do msg.Bcc.Insert("zzz@zzz.com")
Set msg.Subject="Unique subject line here "_$H
Set msg.IsBinary=0
Set msg.IsHTML=0
Do msg.TextData.Write("This is the message.")
Set status=server.Send(msg)
If $$$ISERR(status) {
Do $System.Status.DisplayError(status)
Write server.Error
Quit ""
}
Quit server.FailedSend
}
This example creates an instance of an instance of
%Net.SMTP that uses YPOPs, which is client software that provides SMTP and POP3 access to a Yahoo email account. It uses a test account that has already been set up for this purpose:
ClassMethod YPOPsAsSMTP() As %Net.SMTP
{
Set server=##class(%Net.SMTP).%New()
//local host acts as the server
Set server.smtpserver="127.0.0.1"
//YPOPs uses default port, apparently
Set server.port=25
//Create object to carry authentication
Set auth=##class(%Net.Authenticator).%New()
//YPOPs works with a Yahoo email account
Set auth.UserName="isc.test@yahoo.com"
Set auth.Password="123pass"
Set server.authenticator=auth
Set server.AuthFrom=auth.UserName
Quit server
}
The following, more flexible method accepts both an SMTP server and an email message. The email message should already include a subject line (if required by the SMTP server), but does not have to include addresses. This method then sends the email message to a set of hardcoded test destinations:
ClassMethod SendMessage(server As %Net.SMTP, msg as %Net.MailMessage) as %Status
{
Set From=server.authenticator.UserName
//make sure From: user is same as used in authentication
Set msg.From = From
//finish addressing the message
Do msg.To.Insert("xxx@xxx.com")
//send the message to various test email addresses
Do msg.To.Insert("isctest@hotpop.com")
Do msg.To.Insert("isc_test@hotmail.com")
Do msg.To.Insert("isctest001@gmail.com")
Do msg.To.Insert("isc.test@yahoo.com")
Set status=server.Send(msg)
If $$$ISERR(status) {
Do $System.Status.DisplayError(status)
Write server.Error
Quit $$$ERROR($$$GeneralError,"Failed to send message")
}
Quit $$$OK
}
The
%Net.SMTP class also has some other properties that you might need, depending on the SMTP server you are using:
-
AllowHeaderEncoding specifies whether the
Send() method encodes non-ASCII header text. The default is 1, which means that non-ASCII header text is encoded as specified by
RFC 2047.
-
-
ShowBcc specifies whether the Bcc headers are written to the email message. These will normally be filtered out by the SMTP server.
This section discusses how to use the
%Net.POP3 class to fetch email messages. It includes the following topics:
If you have the needed permissions and if the mail server is running, you can download and process email messages from it using the POP3 protocol. In general, to communicate with a POP3 server, you log in, perform a series of actions that affect a mailbox, and then either commit or roll back any changes. To do this in Caché:
-
-
-
port Specifies the port you will use; the default is 110.
-
timeout Specifies the read timeout in seconds; the default is 30 seconds.
-
Note that this setting does nothing unless
AttachDir is also set.
-
Note that this setting does nothing unless
AttachDir is also set.
-
AttachDir Specifies the directory into which the attachment are saved. There is no default. Make sure to terminate the name of the directory with a slash (
/) or backslash (
\), as appropriate for the operating system. Also make sure that this is directory already exists, and the users have write access to it.
-
IgnoreInvalidBase64Chars Specifies whether to ignore invalid characters found during base64 decoding. The default is false (and invalid characters result in an error). Note that
RFC 2045 is ambiguous about whether unexpected characters should be ignored or should result in an error during base64 decoding.
-
To use an SSL/TLS connection to the POP3 server:
-
Set the
SSLConfiguration property to the name of the activated SSL/TLS configuration to use.
-
In most cases, use the value 0. Use the value 1 for the case in which the server interaction begins on a normal TCP socket and then switches to TLS on the same port as the normal socket. For details, see
RFC 2595.
-
Optionally set the
SSLCheckServerIdentity property to 1. Do this if you want to verify the host server name in the certificate.
-
Call the
Connect() method of your instance. This method takes three arguments, in order:
-
The name of the POP3 server
-
-
-
Use the methods of your instance to examine the mailbox, retrieve messages, and delete messages. The following sections provide details.
-
-
-
When you are done making changes to the mailbox, call one of the following methods:
Each of these methods returns a status, which you should check before continuing. Also see the class reference for
%Net.POP3 for complete method signatures.
The examples in the following sections use two different free POP3 services that were available at the time this manual was written. No particular endorsement is implied by the selection of these services. Also note that the examples do not show the actual passwords.
Example 1: HotPOPAsPOP3()
The following method logs into the HotPOP POP3 server using an account that was previously set up for this purpose:
ClassMethod HotPOPAsPOP3() As %Net.POP3
{
Set server=##class(%Net.POP3).%New()
//HotPOP POP3 server uses the default port
//but let's set it anyway
Set server.port=110
//just in case we plan to fetch any messages
//that have attachments
Set server.StoreAttachToFile=1
Set server.StoreInlineToFile=1
Set server.AttachDir="c:\DOWNLOADS\"
Set servername="pop.hotpop.com"
Set user="isctest@hotpop.com"
Set pass="123pass"
Set status=server.Connect(servername,user,pass)
If $$$ISERR(status) {
Do $System.Status.DisplayError(status)
Quit $$$NULLOREF
}
Quit server
}
The following method also returns a
%Net.POP3 server instance. In this case, we are using YPOPs, which is client software that provides SMTP and POP3 access to a Yahoo email account. It uses a test account that has already been set up for this purpose:
ClassMethod YPOPsAsPOP3() As %Net.POP3
{
Set server=##class(%Net.POP3).%New()
//YPOPs uses the default port
//but let's set it anyway
Set server.port=110
//just in case we plan to fetch any messages
//that have attachments
Set server.StoreAttachToFile=1
Set server.StoreInlineToFile=1
Set server.AttachDir="c:\DOWNLOADS\"
//local host acts as the server
Set servername="127.0.0.1"
//YPOPs works with a Yahoo email account
Set user="isc.test@yahoo.com"
Set pass="123pass"
Set status=server.Connect(servername,user,pass)
If $$$ISERR(status) {
Do $System.Status.DisplayError(status)
Quit $$$NULLOREF
}
Quit server
}
While you are connected to a POP3 server, you are logged into a user account and have access to the mailbox for that user account. Use the following methods to find what the mailbox contains:
Returns, by reference, the number of messages in the mailbox and the number of bytes that the mailbox uses.
If given an empty string as the first argument, this method returns, by reference, an array of information about the messages in the mailbox (excluding any that are currently marked for deletion). Each element in this array contains the following information about one message:
If given an empty string as the first argument, this method returns, by reference, an array of information about the messages in the mailbox (excluding any that are currently marked for deletion). Each element in this array contains the following information about one message:
For example, the following method writes information about the mailbox that we are currently accessing:
ClassMethod ShowMailbox(server as %Net.POP3)
{
Set status=server.GetMailBoxStatus(.count,.size)
If $$$ISERR(status) {
Do $System.Status.DisplayError(status)
Quit
}
Write "Mailbox information *****",!
Write "Number of messages in mailbox: ",count,!
Write "Size of messages: ",size,!
Set status=server.GetMessageUIDArray(,.uids)
Set status=server.GetSizeOfMessages(,.sizes)
//iterate through messages, get info, and write it
For i=1:1:count {
Set uid=uids.GetAt(i)
Set size=sizes.GetAt(i)
Write "Msg number:", i," UID:",uid, " size:",size,!
}
}
This method generates output similar to the following:
Mailbox information *****
Number of messages in mailbox: 4
Size of messages: 18634
Msg number:1 UID:6ef78df6fd660391 size:7245
Msg number:2 UID:7410041a6faf4a87 size:5409
Msg number:3 UID:5555af7fa489e406 size:5121
Msg number:4 UID:299ad2b54c01a6be size:859
Given a message number as the first argument, this method returns (by reference, as the second argument) an instance of
%Net.MailMessage that contains that message.
Given a message number as the first argument, this method returns (by reference) information such as the
From and
To and other common headers, an array containing
all the headers (including the common ones), and the message contents themselves
Each of these methods returns a status, which you should check before continuing. Note that these methods return an error status if the message is currently marked for deletion.
ClassMethod FetchMailbox(server As %Net.POP3)
{
Set status=server.GetMailBoxStatus(.count,.size)
If $$$ISERR(status) {
Do $System.Status.DisplayError(status)
Quit $$$NULLOREF
}
Write "Mailbox information *****",!
Write "Number of messages in mailbox: ",count,!
Write "Size of messages: ",size,!
Set status=server.GetMessageUIDArray(,.uids)
Set status=server.GetSizeOfMessages(,.sizes)
//iterate through messages, get info, and write it
For i=1:1:count {
Set uid=uids.GetAt(i)
Set size=sizes.GetAt(i)
Set status=server.Fetch(i,.msg)
If $$$ISERR(status) {
Set subj="***error***"
} else{
Set subj=msg.Subject
}
Write "Msg number:", i," UID:",uid, " Size:",size
Write " Subject: ",subj,!
}
}
Content-Disposition: attachment; filename=genome.jpeg;
-
-
-
-
Specify a valid directory for
AttachDir. Make sure to terminate the name of the directory with a slash (
/) or backslash (
\), as appropriate for the operating system. Also make sure that this is directory already exists, and the users have write access to it.
-
Each filename is determined as follows:
-
-
Otherwise, if the
Content-Type header specifies a filename, that filename is used.
-
Note the following points:
-
If the file already exists, the attachment is not downloaded.
-
-
The size of the attachment is not limited by Caché but might be limited by the file system.
-
The following example method retrieves an entire message, given an instance of
%Net.POP3 and a message number:
ClassMethod GetMsg(server as %Net.POP3,msgno as %Integer) as %Net.MailMessage
{
Set status=server.Fetch(msgno,.msg)
If $$$ISERR(status) {
Do $System.Status.DisplayError(status)
Quit $$$NULLOREF
}
Quit msg
}
While you are connected to a mailbox, you can download any email messages that are attached to the email messages in the inbox. To do so, use the
GetAttachedEmail() method of your
%Net.POP3 instance to retrieve the contents of the enclosed email.
This section lists all the methods of
%Net.POP3 that you can use to examine and retrieve messages.
method Fetch(MessageNumber As %Integer,
ByRef MailMsg As %Net.MailMessage,
Delete As %Boolean = 0,
messageStream As %BinaryStream) as %Status
Returns (by reference) the message indicated by
MessageNumber and optionally marks the message for deletion. Note that this method returns an error status if the message is already marked for deletion.
If
messageStream is specified, then the original message is written to this binary stream.
method FetchFromStream(messageStream As %BinaryStream, ByRef Msg As %Net.MailMessage) as %Status
Retrieves a single email message from the given binary stream.
messageStream must be a binary stream containing the message. The message is returned by reference in
Msg. This could be a multipart message.
method FetchMessage(MessageNumber As %Integer,
ByRef From As %String,
ByRef To As %String,
ByRef Date As %String,
ByRef Subject As %String,
ByRef MessageSize As %Integer,
ByRef MsgHeaders As %ArrayOfDataTypes,
ByRef MailMsg As %Net.MailMessage,
Delete As %Boolean = 0) as %Status
Returns (by reference) specific message headers, the message size, the message header array, and the message itself and optionally marks the message for deletion. Note that this method returns an error status if the message is already marked for deletion.
method FetchMessageHeaders(MessageNumber As %Integer,
ByRef MsgHeadersArray As %String) as %Status
Given a message number, this method returns (by reference) an array containing all the headers of that message. This method returns an error status if the message is currently marked for deletion.
method FetchMessageInfo(MessageNumber As %Integer,
Lines As %Integer,
ByRef From As %String,
ByRef To As %String,
ByRef Date As %String,
ByRef Subject As %String,
ByRef MessageSize As %Integer,
ByRef MsgHeaders As %ArrayOfDataTypes,
ByRef MessageText As %String) as %Status
Given a message number, this method returns (by reference) specific message headers, the message size, the message header array, and the given number of lines of text from this message. This method returns an error status if the message is currently marked for deletion.
method GetAttachedEmail(msgpart As %Net.MailMessagePart,
Output mailmsg As %Net.MailMessage) as %Status
Given a message part, this method returns (as an output parameter) a single-part email message that is initialized with the data from the message part.
method GetMessageUID(MessageNumber As %Integer,
ByRef UniqueID As %String) as %Status
Returns, by reference, the UID of a message, given a message number. See the previous section for details on message numbers and UIDs. This method returns an error status if the message is currently marked for deletion.
method GetMessageUIDArray(MessageNumber As %String = "",
ByRef ListOfUniqueIDs As %ArrayOfDataTypes) as %Status
If given an empty string as the first argument, this method returns, by reference, an array of information about the messages in the mailbox (excluding any that are currently marked for deletion). Each element in this array contains the following information about one message:
Or, given a message number, this method returns a one-element array that contains the UID of that message. In this case, the method returns an error status if the message is currently marked for deletion.
method GetSizeOfMessages(MessageNumber As %String = "",
ByRef ListOfSizes As %ArrayOfDataTypes) as %Status
If given an empty string as the first argument, this method returns, by reference, an array of information about the messages in the mailbox (excluding any that are currently marked for deletion). Each element in this array contains the following information about one message:
Or, given a message number, this method returns a one-element array that contains the size (in bytes) of that message. In this case, this method returns an error status if the message is currently marked for deletion.
While you are connected to a mailbox, you can mark messages for deletion in the mailbox that you are logged into. You can do this in a couple of ways.
-
You can use the
DeleteMessage() method. This method takes one argument, the message number to delete.
-
When you retrieve a message with the
Fetch() or
FetchMessage() method, you can specify an optional argument that tells the POP3 server to mark the message for deletion after you have retrieved it.
Remember the following points:
-
These methods do not delete a message; they mark it for deletion. The message is not deleted until you complete the POP3 transaction with
QuitAndCommit(). If you simply disconnect from the server, your changes are discarded.
-
You can call the
RollbackDeletes() method to change the messages so that they are no longer marked for deletion.
-
Each of these methods returns a status, which you should check.
Example: GetMsgAndDelete() and CommitChanges()
The following example retrieves a message and marks it for deletion:
ClassMethod GetMsgAndDelete(ByRef server As %Net.POP3, msgno As %Integer) As %Net.MailMessage
{
//third argument to Fetch says whether to
//mark for deletion
Set status=server.Fetch(msgno,.msg,1)
If $$$ISERR(status) {
Do $System.Status.DisplayError(status)
Quit $$$NULLOREF
}
Quit msg
}
Note that this message returns (by reference) an altered version of the
%Net.POP3; the altered version contains the information about which message is marked for deletion.
You would use the preceding method with a method like the following:
ClassMethod CommitChanges(server As %Net.POP3) As %Status
{
//commit all changes and log out
Set status=server.QuitAndCommit()
If $$$ISERR(status) {
Do $System.Status.DisplayError(status)
Quit $$$ERROR($$$GeneralError,"Failed to commit changes")
}
Quit $$$OK
}
Both the message itself and each part of a message has a set of headers.
Also, if you have retrieved a message via
%Net.POP3, you can use the
GetAttribute() method. Given a header name and an attribute, this method returns the value of that attribute.
Once you know what the general message structure is, use the following techniques to retrieve the contents:
-
-
For a binary message (or message part), use the
BinaryData property.
-
For a text message (or message part), use the
TextData property.
Note that the email client that sends a message determines any wrapping in the message. The mail server has no control over this; nor does Caché.
The
MessageSize property indicates the total length of the message, apart from any attached email messages.
The following methods provide additional information about the message:
Returns the date and time when the message was retrieved, converted to local time in
$HOROLOG format.
Returns the date and time when the message was retrieved, converted to UTC in
$HOROLOG format.
Returns the date and time when the message was retrieved, in seconds since 12/31/1840.
The following class methods are also available for time/date conversion:
A class method that converts a date/time in
$HOROLOG format to seconds since 12/31/1840.
A class method that converts seconds since 12/31/1840 to a date/time in
$HOROLOG format.
ClassMethod ShowMsgInfo(msg as %Net.MailMessage)
{
Write "Message details *****",!
Write "To (count): ", msg.To.Count(),!
Write "From: ", msg.From,!
Write "Cc (count): ", msg.Cc.Count(),!
Write "Bcc (count): ", msg.Bcc.Count(),!
Write "Date: ", msg.Date,!
Write "Subject: ", msg.Subject,!
Write "Sender: ", msg.Sender,!
Write "IsMultipart: ", msg.IsMultiPart,!
Write "Number of parts: ", msg.Parts.Count(),!
Write "Number of headers: ", msg.Headers.Count(),!
Write "IsBinary: ", msg.IsBinary,!
Write "IsHTML: ", msg.IsHTML,!
Write "TextData: ", msg.TextData.Read(),!
Write "BinaryData: ", msg.BinaryData.Read(),!
}
This method produces output similar to the following:
Message details *****
To (count): 1
From: "XXX XXX" <XXX@XXX.com>
Cc (count): 0
Bcc (count): 0
Date: Fri, 16 Nov 2007 11:57:46 -0500
Subject: test 5
Sender:
IsMultipart: 0
Number of parts: 0
Number of headers: 16
IsBinary: 0
IsHTML: 0
TextData: This is test number 5, which is plain text.
BinaryData:
The following method writes information about a
part of a message:
ClassMethod ShowMsgPartInfo(msg as %Net.MailMessage, partno as %Integer)
{
Set part=msg.Parts.GetAt(partno)
Write "Message part details *****",!
Write "Message part: ", partno,!
Write "IsMultipart: ", part.IsMultiPart,!
Write "Number of parts: ", part.Parts.Count(),!
Write "Number of headers: ", part.Headers.Count(),!
Write "IsBinary: ", part.IsBinary,!
Write "IsHTML: ", part.IsHTML,!
Write "TextData: ", part.TextData.Read(),!
Write "BinaryData: ", part.BinaryData.Read(),!
}
This produces output similar to the following (given a different message than previously shown):
Message part details *****
Message part: 1
IsMultipart: 0
Number of parts: 0
Number of headers: 2
IsBinary: 0
IsHTML: 0
TextData: 1 test string
BinaryData:
The following method writes information about the headers of a message; you could write a similar method that did the same for a message part.
ClassMethod ShowMsgHeaders(msg as %Net.MailMessage)
{
Set headers=msg.Headers
Write "Number of headers: ", headers.Count(),!
//iterate through the headers
Set key=""
For {
Set value=headers.GetNext(.key)
Quit:key=""
Write "Header:",key,!
Write "Value: ",value,!!
}
}
This produces output similar to the following:
Number of headers: 16
Header: content-class
Value: urn:content-classes:message
Header: content-type
Value: multipart/alternative; boundary="----_=_NextPart_001_01C8286D.D9A7F3B1"
Header: date
Value: Fri, 16 Nov 2007 11:29:24 -0500
Header: from
Value: "XXX XXX" <XXX@XXX.com>
Header: message-id
Value: <895A9EF10DBA1F46A2DDB3AAF061ECD501801E86@Exchange1_backup>
Header: mime-version
Value: 1.0
...
A email message part contains information about both the character sets used and the content-transfer-encoding used (if any). For reference, this section describes how this information is used.
If you do not specify the
Charset property for a given part, when you send the message, the following defaults are used for this part:
-
On Unicode systems, the default is UTF-8.
-
On 8bit systems, the default is the system default character set.
Then
%Net.POP3 checks the
Content-Type header of each message part. This affects the
Charset property of the message part and also controls the translation table used when the message part is created in Caché.