Classes

Class MIMEContent() Professional

Class function of the MIMEContent class.

Subclass
Description

The MIMEContent class provides services for inspecting the contents of an e-mail when it is retrieved from a POP3 server via a POP3Client object. Therefore, MIMEContent objects are usually not created via the :new()method but are retrieved from a MIMEMessage object that represents the retrieved e-mail message. The general programming pattern for this is as follows:

oMIMEMessage := oPOP3Client:getMessage( nIndex ) 
aContent     := oMIMEMessage:getContent() 

FOR i:=1 TO Len( aContent ) 
oContent := aContent[i] 

<examine data of oContent> 
NEXT 

The contents of a simple e-mail message contain simply the character string of the e-mail body, plus the content-type and the content-transfer-encoding used to encode the text. However, an e-mail may be comprised of multiple parts containing not only the text but also file attachments, for example. An e-mail is then transferred as a multi-part message, and each part may also contain multi-part content. Therefore, the contents of an e-mail message is always returned as an array of MIMEContent objects which must be inspected individually in order to obtain all parts of an e-mail message.

In case of multi-part messages, a content can be one of three types. If it contains binary information to be attached to the e-mail, then the contents is usually added as mixed type. An example for a binary attachment is an image or a file.

If the content type of an e-mail is "text/html", the html part may reference images that can be added as a related content type.

In case the content of an e-mail is of type "text/html", a content of type "text/plain" can be added as alternative content. This way, the mail can also be read by e-mail readers that cannot display html pages.

Instance variable
:uniqueString
Unique character string
Class methods
:encodeBase64()
Create a MIMEContent object from a binary character string.
:encodeQuotedPrintable()
Create a MIMEContent object from an ASCII character string.
:createFromFile()
Create a MIMEContent object from a file.
Methods for composing multipart content
:attachAlternative()
Attach MIMEContent object as alternative content
:attachMixed()
Attach MIMEContent object as mixed content
:attachRelated()
Attach MIMEContent object as related content
Methods for examining e-mail contents
:getContent()
Retrieves the contents of a single part when it has multi-part content.
:getContentTransferEncoding()
Retrieves the content transfer encoding of a single part.
:getContentType()
Retrieves the content type of a single part.
:getFileName()
Retrieves the original file name of a single part.
:getMessage()
Retrieves the message body of a single part.
:isConverting()
Tests whether the message in a single part is converted automatically.
:isMultiPart()
Determines if a single part is comprised of multiple content parts.
:setCharset()
Specifies the charset of a single part.
Examples
Examining multi-part messages
// This example demonstrates how to program a recursive 
// routine for inspecting multi-part e-mails. 

#include "asinet.ch" 

#define CRLF  Chr(13)+Chr(10) 

#pragma library("ASINET10.LIB") 

// 
// 
// Usage: <cPopServer>  Your mailserver ( "example.domain.de" ) 
//        <cUsername>   Username for authentification 
//        <cPasswor>    Password for authentification 
// 
PROCEDURE main( cPopServer, cUsername, cPassword ) 
   LOCAL oPop3, nCount, i, oMail, aContent 

   // Check Parameter 
   IF PCount() < 3 
     ? "Error: Parameter missing" 
     ? "Usage:", AppName(), "popserver username password" 
     WAIT 
     QUIT 
   ENDIF 

   // 
   // Create connection to the Pop server 
   // 
   oPop3 := POP3Client():new( cPopServer, NIL    , ; 
                              cUsername,  cPassword , ; 
                              LogWriter():new(), 2   ) 
   IF .NOT. oPOP3:connect() 
      ? "Unable to establish connection to:", cPopServer 
      QUIT 
   ENDIF 

   // 
   // Iterate all messages and dump the contents 
   // 

   nCount := oPOP3:getNumberOfNewMessages() 
   FOR i:=1 TO nCount 
      // Obtain MIMEMessage object for e-mail 
      oMail := oPOP3:getMessage( i ) 
      aContent := oMail:getContent() 
      GetAllContent( aContent ) 
   NEXT 

RETURN 

// 
// Printout: content-type 
//           content-transfer-encoding 
//           filename if any 
//           message body 
// 
// In case  the MIMEContent is a file attachment 
// then write the file to drive 
// 
PROCEDURE GetAllContent( aContent ) 
   LOCAL oContent, i, nLen, nHandle 
   LOCAL cType, cEncoding, cFilename, cBody 

   nLen := Len( aContent ) 

   FOR i:=1 TO nLen 
      oContent := aContent[i] 

      // In case the content is a multipart message the 
      // content contains an array of other MIMEContent 
      // objects. So call this function recursive 
      IF oContent:isMultiPart() 
         GetAllContent( oContent:getContent() ) 
         LOOP 
      ENDIF 

      // 
      // Printout parts of the message 
      // 

      cType     := oContent:getContentType() 
      cEncoding := oContent:getContentTransferEncoding() 
      cFilename := oContent:getFileName() 
      cBody     := oContent:getMessage() 

      ? "Content-Type:", cType 
      ? "Content-Transfer-Encoding:", cEncoding 

      IF .NOT. Empty( cFilename ) 
         ? "Attached file:", cFilename 

         // Create local copy of file 
         nHandle := FCreate( cFilename ) 
         FWrite( nHandle, cBody ) 
         Fclose( nHandle ) 
      ENDIF 

      // parts of type text/xxx can be viewed directly 
      IF "text/" $ Lower( cType ) 
         ? "Content:", cBody 
      ENDIF 

      ? "End-Part" 
   NEXT 
RETURN 

// 
// User-defined CLASS FOR log-data processing 
// 
CLASS LogWriter 
   EXPORTED: 
   INLINE METHOD write( cLogData ) 
      ? cLogData 
   RETURN 
ENDCLASS 
Feedback

If you see anything in the documentation that is not correct, does not match your experience with the particular feature or requires further clarification, please use this form to report a documentation issue.