EldoS | Feel safer!

Software components for data protection, secure storage and transfer

Saving just the Mailbody, no headers/fields

Also by EldoS: Rethync
The cross-platform framework that simplifies synchronizing data between mobile and desktop applications and servers and cloud storages
#22867
Posted: 12/18/2012 08:19:23
by Anton Kufer (Standard support level)
Joined: 12/18/2012
Posts: 16

I'm connecting to a SSL POP3 Server with the TElPOP3Client component. That all works pretty well by now.

Now i want to save just the mail-body to disc via ReceiveMessage(index,t_Stream). The stream is a tfilestream. Yet the function saves the complete message to disc, not just the plain body i need.

I don't need the header information, just the plain messagetext in it's raw format. utf8->ansi conversion may be fine, though.

How can that be done?

Actual code:

Code
        t_Index := t_MessageIDs[i].Index;
        t_MessageID := t_POP3.GetMessageID(t_Index);

        if t_POP3.ReceiveMessageHeader(t_Index, t_Charset, true, t_Header)=EL_OK then
        begin
          t_Size := t_POP3.GetMessageSize(t_Index);

          if t_Header.GetField('Subject') <> nil then
          begin
            t_Subject := trim(t_Header.GetField('Subject').Value);
          end;

          t_EmailOK := false;
          if trim(eMailSubject)<>'' then
          begin
            if copy(AnsiLowercase(t_Subject),1,length(eMailSubject))=AnsiLowercase(eMailSubject) then
            begin
              t_EmailOK := true;
            end;
          end
          else begin
            t_EmailOK := true;
          end;

            t_Dateiname := IncludeTrailingBackslash('c:\email')+FormatDateTime('yyyymmddhhnnsszzz',now)+'.bak';
            try
              t_Stream := TFileStream.Create(t_Dateiname, Classes.fmCreate);
              try
                t_POP3.ReceiveMessage(t_Index, t_Stream);
              except
                on e:Exception do
                begin
                  MessageDlg(e.Message,mtError,[mbOK],0);
                end;
              end;
            finally
              FreeAndNil(t_Stream);
            end;
            t_POP3.DeleteMessage(t_Index);
#22869
Posted: 12/18/2012 08:31:20
by Vsevolod Ievgiienko (EldoS Corp.)

Thank you for getting in touch with us.

There is no way to retrieve a message body separately from its headers. You can solve the issue in two ways:

1) use
Code
TElPOP3Client.ReceiveMessage(Index: Integer; const HeaderCharset,
  BodyCharset: string; Options: TElMessageParsingOptions;
  IgnoreHeaderNativeCharset, IgnoreBodyNativeCharset,
  ActivatePartHandlers: Boolean;
  out Message: TElMessage): ELMIMERESULT;

method. It will download a full message to a temporary TMemoryStream and will parse it using TElMessage. Then you'll be able to retrieve its body using TElMessage properties.

However this method works fine only for *small* messages as you can run out of memory with *big* ones.

2) Download a full message to a temporary TFileStream or TMemoryStream depending on message size and then parse it manually using above mentioned TElMessage.
#22872
Posted: 12/18/2012 08:58:01
by Anton Kufer (Standard support level)
Joined: 12/18/2012
Posts: 16

TIDMessage from indy has a body property to easily read the body from. seems like i can't that in an TELMessage. How would you parse a mail in memory? The XML Content i need to save/parse/work will not be that big, so all can be done in memory.
#22873
Posted: 12/18/2012 09:01:04
by Vsevolod Ievgiienko (EldoS Corp.)

Please refer to our sample from \EldoS\SecureBlackbox.VCL\Samples\Delphi\MIMEBlackbox\Viewer folder. It shows how to use TElMessage for e-mail parsing.
#22875
Posted: 12/18/2012 09:43:10
by Anton Kufer (Standard support level)
Joined: 12/18/2012
Posts: 16

the viewer demo doesn't work in delphi XE nor in delphi 5. Delphi XE can't load the project and delphi 5 brings an error with the html component thing.

i'm digging through lines and lines of code. is there no EASY way to solve this problem?!
#22877
Posted: 12/18/2012 09:46:33
by Anton Kufer (Standard support level)
Joined: 12/18/2012
Posts: 16

message.mainpart.gettext?
#22879
Posted: 12/18/2012 09:49:04
by Vsevolod Ievgiienko (EldoS Corp.)

Yes thats it.
#22880
Posted: 12/18/2012 09:52:10
by Anton Kufer (Standard support level)
Joined: 12/18/2012
Posts: 16

how can i set a mail as read? seems like the server doesn't do the DeleteMessage and keeps showing me all messages on the account.

or did i miss something?
#22882
Posted: 12/18/2012 09:55:52
by Vsevolod Ievgiienko (EldoS Corp.)

Messages are deleted after you close the connection. This is how POP3 protocol works.
#22919
Posted: 12/19/2012 03:33:55
by Anton Kufer (Standard support level)
Joined: 12/18/2012
Posts: 16

i do

Code
    t_POP3.Open;
    t_POP3.Login;

    t_Anzahl := t_POP3.GetMessageCount;

    t_POP3.GetMessageIDList(t_MessageIDs);
    for i := 0 to Length(t_MessageIDs)-1 do

...

            t_POP3.DeleteMessage(t_Index);

...

    end;
    if t_POP3.Active then
    begin
      t_POP3.Close(true);
    end;


which should delete/mark-as-read 2 mails on the account. yet every time i open the connection and check for mails it returns all 3 mails on the account.

what could be the problem here?

with indy we just call pop3.delete and have had no problems so far, or the mailservers don't deliver already polled mails.
Also by EldoS: RawDisk
Access locked and protected files in Windows, read and write disks and partitions and more.

Reply

Statistics

Topic viewed 1764 times

Number of guests: 1, registered members: 0, in total hidden: 0




|

Back to top

As of July 15, 2016 EldoS Corporation will operate as a division of /n software inc. For more information, please read the announcement.

Got it!