Re: MIME Part 2

Giganews Newsgroups
Subject: Re: MIME Part 2
Posted by:  Don (kingfi…@joesgarage.org)
Date: Fri, 9 Dec 2005

> Help please, I am new to using Indy.Sockets and I am
> currently working on using it retreive emails from a pop3
> account.

> I can get the system to connect and retreive the subject
> and from parts, the problem starts when trying to retreive
> the main text of the email.

> the code i'm using is:

> for (int i = 0; i <= xMsg.MessageParts.Count-1; i++)
> {
> txtBody.Text +=
> xMsg.MessageParts[i].ResolveContentType(xMsg.Body.ToString()); }
> txtBody.Text += xMsg.Body.ToString();

> This piece of code works fine plain text messages but when
> it tries to retrive the text from a MIME email it just displays.

> This is a multi-part message in MIME format.
> This is a multi-part message in MIME format.
> This is a multi-part message in MIME format.

> How do I get indy.sockets to retrive the contents from MIME emails.

First of all, I don't believe that ResolveContentType was intended for use
in this manner. It appears to be used to fix up the ContentType for a
message part when used in a "multipart/related" or a "message/digest"
context. It just happens to work for simple messages because it's not doing
anything that affects the returned value.

The message body (xMsg.Body) is going to be used when the ContentType for
the message is a member of the "text/*" MIME family (xMsg.ContentType starts
with either "text/plain" or "text/html"). When the xMsg.ContentType contains
a value in the "multipart/*" MIME family, use the MessageParts collection to
locate one or more possible representations of the message body.

A MessagePart is a candidate for use as a message body when its has a
ContentType in the "text/*" MIME family (xMsg.MessageParts[i].ContentType
starts with "text/") or the PartType class method  returns mptText
(xMsg.MessageParts[i].PartType == mptText).  Deciding which text message
part is preferred is a programmer issue... but normally you'd use one with a
ContentType of "text/plain" or "text/html".

In a perfect world, everyone would send both "text/plain" and "text/html"
representations so you could just pick one or the other.  (Actually, in a
perfect world HTML would not be allowed in email messages<g>).  Since this
is not a perfect world, your client needs to implement an algorithm to find
the best or preferred representation. "text/plain" content in a Body can be
stuffed directly into a TextBox.  "text/html" content requires more
handling; either remove the HTML tags for use in a TextBox or pass the
string representation of the Body to something that can render HTML
properly.

This could be used (please excuse any syntax errors - I'm not a C#
programmer)...

string body = "Unable to locate a message body";
string contentType = "text/plain";

if (xMsg.ContentType.StartsWith("text/")
{
    contentType = xMsg.ContentType;
    body = xMsg.Body.ToString();
}
else if (xMsg.Encoding == meMIME)
{
  // find the first text message part
  for (int i = 0; i <= xMsg.MessageParts.Count-1; i++)
  {
    if (xMsg.MessageParts[i].PartType == mptText)
    {
      contentType = xMsg.MessageParts[i].ContentType;
      body = xMsg.MessageParts[i].Body.ToString();
      break;
    }
  }
}

// TODO: prepare the body for rendering based on the actual content type
located

hth...

Replies

None

In response to

MIME Part 2 posted by Peter Edley on Fri, 9 Dec 2005