Archived Forum Post

Index of archived forum posts

Question:

How do you parse a multipart/mixed message?

Aug 27 '13 at 16:48

I have an email message with a Multipart/mixed content type. The get_NumParts() method returns 1 but the getBodyDecoded() returns an empty string. Below is an example of the mail I am trying to parse.

content-Type: multipart/mixed; boundary="----msg_border_oyclZ0HtKZ"

This is a multi-part message in MIME format.

------msg_border_oyclZ0HtKZ
Date: Mon, 26 Aug 2013 12:46:24 -0700
Content-Type: multipart/alternative; boundary="----alt_border_Fu87pgEhas_1"

------alt_border_Fu87pgEhas_1
Content-Type: text/plain;
    charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

Having trouble viewing this message? Click here=2E=20

------alt_border_Fu87pgEhas_1
Content-Type: text/html;
    charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<=21doctype html>
<html xmlns=3D=22http://www=2Ew3=2Eorg/1999/xhtml=22>
<body>
<p>This is an html document</p>
</body>

</html>

------alt_border_Fu87pgEhas_1--

------msg_border_oyclZ0HtKZ--

Answer

MIME messages (such as this) are hierarchical and have a tree structure (just like XML). Conceptually, a MIME node can have child nodes, but the child nodes themselves (and the content within the child nodes) are not considered the "content" (i.e. body) of the parent. Typically, a MIME part (i.e. node) that is multipart, meaning that it has child nodes, will have no "content". It only has child nodes. It is only the leaf nodes that have a body. (Chilkat's XML API follows this same idea.)

Therefore, you should navigate to the leaf node, such as the "text/plain" or the "text/html" to get either the plain-text body or the HTML body.


Answer

Yes. Call GetPart(n) to get the Nth immediate child of a mime object. For example:

CkMime *child = mime.GetPart(0);   // Pass 0 to get the 1st child, 1 to get the 2nd, etc.
Check to make sure child is non-null. Your app will have to delete any objects returned by a Chilkat method. After getting the child, check the ContentType, if it's multipart (has NumParts > 0) then get that object's sub-parts, etc., until you reach the part where the content-type is text/plain or whatever you want..