Showing posts with label Messages. Show all posts
Showing posts with label Messages. Show all posts

Tuesday, December 19, 2017

Parsing Mime Messages

Parsing Mime Messages
MIME means Multipurpose Internet Mail Extensions, and refers to an official Internet standard that specifies how messages must be formatted so that they can be exchanged between different email systems. MIME is a very flexible format, permitting one to include virtually any type of file or document in an email message. Specifically, MIME messages can contain text, images, audio, video, or other application-specific data.

The MIME protocol is made up of the extensions to the Internet mail format documented in RFC 822, Standard for the Format of ARPA Internet Text Messages, August 1982. The MIME protocol, documented in a series of MIME RFC, adds these features:

* The ability to send rich information through the Internet
* The ability to encode and attach binary (non-ASCII) content to messages
* A framework for multipart mail messages that contain differing body parts
* A way to identify the content type associated with a message body part
* A standardized and interpretable set of body part types

MIME messages can include attachments and non-ASCII data. To conform with RFC 822, which requires mail message characters to be in ASCII, MIME uses an encoding algorithm to convert binary data to ASCII characters. For content that requires encoding, MIME specifies two encoding types, either Quoted-Printable or BASE64, which are described more fully in MIME Encoding Types.

In addition to the ability to build multi-media messages in MIME format, the MIME API of the Messaging Access SDK provides a parsing facility for messages. This generic MIME parser takes a MIME-encoded email message and decodes all or parts of it, depending on the preferences of the application. The MIME parser is described in Parsing MIME Messages.

The MIME parser does not support the full MIME standard, but does support common uses of MIME. You can send the messages to the broker over HTTP or over other transport types.

Parsing MIME Messages

For parsing encoded messages, the Messaging Access SDK provides these options:

* Parsing the Entire Message. Use this option when the message to be parsed is available in its entirety when you begin parsing.
* Dynamic Parsing. Use the dynamic parser when the entire message is not available when you begin parsing, but becomes available block by block. This could happen when you are receiving a message from a server.

Parsing the Entire Message

You can use MIMEParser class methods to parse and decode encoded messages retrieved through email protocol APIs, such as POP3 and IMAP4. First, create a MIMEParser object; then call MIMEParser.parseEntireMessage.

This method parses an entire MIME message in one operation and returns the parsed message:

public MIMEMessage parseEntireMessage(
InputStream input) throws MIMEException

Supply the identifier of the input stream for the message.

The following section of code uses MIMEParser.parseEntireMessage as part of a routine that parses an entire file.

MIMEMessagePart msg = parseEntireMessage(inputStream);

Dynamic Parsing

* This section describes the steps involved in using the dynamic parser. Dynamic parsing contrasts with standard MIME parsing, as described in Parsing the Entire Message, in several ways.
* The dynamic parser can parse a message in chunks, rather than in its entirety, in a single operation. The dynamic parser decodes the message on the fly, passing the data to the user right away without waiting for the whole message.
* The dynamic parser returns parsed message data to the caller using callbacks in the data sink. For information about this, see Creating a Data Sink. The MIMEParser.parseEntireMessage method does not use callbacks; instead, it passes the entire parsed message to the user after parsing is complete.
* The dynamic parser does not decode the Base64/QP-encoded parts of the message. To do this, use the utility methods in the MIMEHelper class.

Steps in Dynamic Parsing

Using the dynamic parser involves these operations:

* Create a MIMEDataSink object and callback methods for the parser. The MIME data sink contains one call for each piece of information that the parser can return. For example, for a header, it contains a header callback method.
* Create a parser object, which takes the data sink as a parameter.
* Begin parsing.
* As long as there is more data to parse, continue to call a dynamic parsing method that matches the source of the data. Keep calling this method until there is no more data. See Running the Parser.
* When there is no more data to parse, indicate that parsing is complete.

All dynamic parser methods are defined in the MIMEDynamicParser class.

Tatiana Tokarieva has an experience working with backup software for SharePoint, Exchange Server and other. Learn more about backup software.