Link to home
Start Free TrialLog in
Avatar of graga
graga

asked on

Reading and displaying email body using Indy TIdMessage

Hi Experts,

I'm building a simple email client application.
I retrieve message with:

IdPOP3.Retrieve(iMessageCount, NewMessage)

but I'm having problems displaying the message body. I'm using sample code from Indy but id displays much more information that is stored in the message body, for example:
================
--_NextPart_2altrfkindysadvnqw3nerasdf
Content-Type: text/plain; charset"iso-8859-1"
Content-Transfer-Encoding: quoted-printable

this is a test message
================

followed by more stuff.
What I want is something like any other email client, select email and display content of email body in correct format.

I need it urgently so 500 points for a complete solution
Avatar of Validor
Validor

Mail is sent as one great big text block.  It is up to the mail client to pick out the headers (the to, from, cc, bcc, subject, etc.) from the body and attachments.  TIDMessage does all this for you.  It sounds like you're using TIDPop3.RetreieveRaw() instead of .Retrieve.  If you use .Retrieve, it will put this into a TIDMessage for you (just drop one on the form) and you can reference the individual pieces.  Here is code from a Delphi Demo:

   //get message and put into MSG
   ShowStatus('Retrieving message "' + lvHeaders.Selected.SubItems.Strings[3] + '"');
   POP.Retrieve(lvHeaders.Selected.Index + 1, Msg);
   statusbar1.Panels[0].text := lvHeaders.Selected.SubItems.Strings[3];

   //Setup fields on screen from MSG
   From.Caption := Msg.From.Text;
   Recipients.Caption := Msg.Recipients.EmailAddresses;
   Cc.Caption := Msg.CCList.EMailAddresses;
   Subject.Caption := Msg.Subject;
   Date.Caption := FormatDateTime('dd mmm yyyy hh:mm:ss', Msg.Date);
   Receipt.Caption := Msg.ReceiptRecipient.Text;
   Organization.Caption := Msg.Organization;
   Priority.Caption := IntToStr(Ord(Msg.Priority) + 1);

   //Setup attachments list
   ShowStatus('Decoding attachments (' + IntToStr(Msg.MessageParts.Count) + ')');
   for intIndex := 0 to Pred(Msg.MessageParts.Count) do
      begin
         if (Msg.MessageParts.Items[intIndex] is TIdAttachment) then
            begin //general attachment
               pnlAttachments.visible := true;
               li := lvMessageParts.Items.Add;
               li.ImageIndex := 8;
               li.Caption := TIdAttachment(Msg.MessageParts.Items[intIndex]).Filename;
               li.SubItems.Add(TIdAttachment(Msg.MessageParts.Items[intIndex]).ContentType);
            end
         else
            begin //body text
               if Msg.MessageParts.Items[intIndex] is TIdText then
                  begin
                     Memo1.Lines.Clear;
                     Memo1.Lines.AddStrings(TIdText(Msg.MessageParts.Items[intIndex]).Body);
                  end
            end;
      end;
Sorry, I now see that you're posting the .Retrieve code you used... the code should still apply, though.  The BODY property of the message isn't really where you want to get the body.  The messageparts contains a TIDText object which contains the body.
Avatar of graga

ASKER

Thank you Validor,

This is the code I'm using. I store the message in a database first in a blobfield and then retrieve. I think that's where I may have the error. But, when I decoded the message straight away from POP3 the code works okay for plain text messages but messages stored in HTML don't work and I need to display it in the correct format. Any ideas?
The definition of an email message is as follows:

header
. (a line with only a dot)
body

The header consists of name value pairs where the name part is separated from the value(s) with a colon (:)

Normally the body and header are separated by TIdMessageClient.ReceiveHeader

>>      if ((Length(AAltTerm) = 0) and (Result = '.')) or
>>         ({APR: why? (Length(AAltTerm) > 0) and }(Result = AAltTerm)) then begin

My Indy version has the code above. It has a small commented part after APR. Maybe it is not commented in your version? Or maybe it is as well and it will work if you uncomment it.

Anyway if you want to correct the way the header is separated from the body this is the place to alter.

Could you post the "raw" message here you test with here? I mean header and body together (like when it is posted using SMTP). Then I could help you alter the Indy code correctly.

Regards Jacco
Which version of Indy are you using?  

Delphi 6 = version 8
Delphi 7 = version 9
Delphi 8 = version 10

If using Version 10, are you using the version that came with Delphi, or did you download the latest?  If not, download the latest.  Either way, version 10 is in beta and may have a bug.  

If you are using the Indy 10 .NET version for non-delphi environments such as C#, please point that out as well.
Avatar of graga

ASKER

I'm using Indy 9.011 on D5
Have you checked the TIdMessageClient.ReceiveHeader in your version? Can you show what it reads?
Avatar of graga

ASKER

My TIdMessageClient.ReceiveHeader is exactly same as yours.
I have also noted that when using INDY demo of Email Client, the message also displays incorrectly.
Can you paste the header here?
Avatar of graga

ASKER

OK, what I have found so far:

If ContentType is text_plain, the message body text is in IdMessage.Body - simple.

When ContentType is for example multipart/mixed, then everything is stored in MessageParts.

As I understand, IdMessage does not decode MessageParts but dumps everything as text.

The decoding is probably done by IDMessageDecoder or IDMessageDecoderMIME but so far I have no idea how to use it.
Please email me a message such as the one you are having trouble with.  I'll try it and see if I get the same result so that I can better see what the problem is.

validar

-a-t-

netscape.net.
Avatar of graga

ASKER

I have installed a Clever Internet Suite set of components and they do exactly what I want.
Thank you for trying
ASKER CERTIFIED SOLUTION
Avatar of ee_ai_construct
ee_ai_construct
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial