Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 7328
  • Last Modified:

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
0
graga
Asked:
graga
  • 5
  • 4
  • 3
  • +1
1 Solution
 
ValidorCommented:
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;
0
 
ValidorCommented:
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.
0
 
gragaAuthor Commented:
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?
0
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

 
JaccoCommented:
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
0
 
ValidorCommented:
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.
0
 
gragaAuthor Commented:
I'm using Indy 9.011 on D5
0
 
JaccoCommented:
Have you checked the TIdMessageClient.ReceiveHeader in your version? Can you show what it reads?
0
 
gragaAuthor Commented:
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.
0
 
JaccoCommented:
Can you paste the header here?
0
 
gragaAuthor Commented:
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.
0
 
ValidorCommented:
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.
0
 
gragaAuthor Commented:
I have installed a Clever Internet Suite set of components and they do exactly what I want.
Thank you for trying
0
 
ee_ai_constructCommented:
Question answered by asker or dialog valuable.
Closed, 500 points refunded.
ee_ai_construct (replacement part #xm34)
Community Support Admin
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

  • 5
  • 4
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now