Chapter 8 Handling Incoming E-mail

Deal with incoming email

This section describes how to handle E-mail messages retrieved via % net.pop3 (% net.mailMessage).

Message Basics

After retrieving an E-mail message (% net.mailMessage), you usually first determine what type of message it is and how to read it; That is, whether it is a multi-part message and whether the parts are binary. In this step, you can use the ContentType attribute. Alternatively, you can use IsBinary, IsHTML, and IsMultiPart properties, which indirectly provide the same information as contentType.

If the message is a multi-part message, each part is an instance of % net.mailMessagePart.

Message Headers

The message itself and each part of the message have a set of headers.

The % net.mailMessage and % Net.mailMessagePart classes provide properties that make it easy to access the most commonly used headers. For example, % net.mailMessage provides attributes such as recipient, sender, subject, and date. The Headers array property allows access to any custom header.

In addition, if the message has been retrieved through % net.pop3, the GetAttribute() method can be used. In the case of a header name and an attribute, this method returns the value of that attribute.

Message Contents

Once you understand the general message structure, retrieve the content using the following techniques:

  • For multi-part messages, usePartsProperty, which is an array of parts.Parts.Count()Give the number of parts. Each component’s key is an integer, starting with 1. useGetAt()Method to retrieve the given part. The message part is%Net.MailMessagePartThe instance.
  • For binary messages (or message parts), useBinaryDataProperties.
  • For text messages (or message parts), useTextDataProperties.
    • ifIsHTMLTo 0, theTextDataProperty is a plain text string.
    • ifIsHTML1, thenTextDataProperty is an HTML text string.

Note that the E-mail client that sent the message determines any packaging in the message. The mail server has no control over this,

Other message information

The MessageSize property represents the total length of the message (excluding any additional E-mail messages).

The following methods provide additional information about the message:

GetLocalDateTime()

Returns the date and time of the retrieval message, converted to local time in $HOROLOG format.

GetUTCDateTime()

Returns the date and time of the retrieval message, converted to UTC in $HOROLOG format.

GetUTCSeconds()

Returns the date and time in seconds for retrieving messages since December 31, 1840.

The following class methods can also be used for time/date conversions:

HToSeconds()

Converts the date/time in $HOROLOG format to a class method of seconds since December 31, 1840.

SecondsToH()

A class method that converts seconds since December 31, 1840 to date/time in $HOROLOG format.

Example 1: ShowMsgInfo()

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(),!
}
Copy the code

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:
Copy the code

Example 2: ShowMsgPartInfo()

The following method writes information about a part of the 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(),!
}
Copy the code

This produces output similar to the following (the given message is different from the one shown earlier) :

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:
Copy the code

Example 3: ShowMsgHeaders()

The following method writes information about the header; You can write a similar method that performs the same action on the 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,!! }}Copy the code

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.Copy the code

Automatic encoding and character translation

The E-mail section contains information about the character set used and the content transfer encoding used, if any. For reference, this section describes how to use this information.

Outgoing email

% NET.SMTP checks the character set properties for each part and then applies the appropriate conversion table.

If no character set attributes are specified for a given part, InterSystems IRIS uses UTF-8.

% net. SMTP also checks the ContentTransferEncoding property. If this attribute is “base64” or “quoted-printable”, % net.SMTP encodes the body as needed when the message is created. (If the content transfer is encoded as “7bit” or “7bit”, no encoding is required.)

Important: Please note that if the content is “Base64” encoded, it cannot contain any Unicode characters. If the content you are sending includes Unicode characters, be sure to convert the content to UTF-8 using $ZCONVERT.

Incoming email

% net.pop3 checks the Content-Transfer-Encoding header for each message part and decodes the body as needed.

% net.pop3 then checks the Content-Type header for each message part. This affects the character set properties of the message part and also controls the conversion table used when creating the message part in InterSystems IRIS.