Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Open eml files from Delphi

Posted on 2011-10-05
9
Medium Priority
?
2,922 Views
Last Modified: 2012-06-21

Hello,

Almost every mail clients as Thunderbird can save and opem eml files.

I need to open such an Eml file in Delphi (only for display, to show the Html contents of the email).
I know that I can open the default email client and pass the file as parameter but I need one that doesn't depend on this.


Can you help me in this? Thank you
0
Comment
Question by:starhu
8 Comments
 
LVL 2

Accepted Solution

by:
BlueVD earned 1336 total points
ID: 36920403
Hello,

The most simple way to open EML files is by using Indy (TIdMessage)
Here's a small example:
procedure TForm.OpenButtonClick(Sender: TObject);
var
IndyStringList: TStringList;
IndyStream: TMemoryStream;
I: Integer;
const
CRLF = #13#10;
begin
if not OpenDialog1.Execute then
Exit;
IndyStringList := TStringList.Create;
IndyStream := TMemoryStream.Create;
try
IndyStringList.LoadFromFile(OpenDialog1.FileName);
IndyStringList.Add(CRLF + '.' + CRLF);
IndyStringList.SaveToStream(IndyStream);
IndyStream.Position := 0;
IdMessage.Clear;
IdMessage.LoadFromStream(IndyStream);
Memo1.Clear;
Memo1.Lines.Assign(IdMessage.Headers);
Memo2.Clear;
Memo2.Lines.Add(IdMessage.From.Text);
Memo2.Lines.Add(IdMessage.Recipients.EMailAddresses);
Memo2.Lines.Add(IdMessage.Subject);
Memo2.Lines.Add(DateTimeToStr(IdMessage.Date));
Memo2.Lines.Add('');
for I := 0 to IdMessage.MessageParts.Count - 1 do
if IdMessage.MessageParts.Items[i] is TidText then

Memo2.Lines.Add(TidText(IdMessage.MessageParts.Items[i]).Body.Text);
finally
IndyStringList.Free;
IndyStream.Free;
end;
end;

Open in new window


A few notes on this example: you should check each line if it starts with a dot character (since they as seen as message terminators) and replace them with double dots or any other character. Also, MIME encoded emails (the ones with html body) have the body of the email as the first atachment (you can easily find out how to read the atachments in the indy doc).
Good luck!
0
 

Author Comment

by:starhu
ID: 36921048
Thank you.

I haven't tried it yet but I suppose this won't show the html emails correctly.

I mean that TMemo cannot show formatted text ,  so the characters in bold, colors, tables etc. won't be seen.

Isn't there a way to show it in a TWebBrowser, for example?
0
 
LVL 2

Assisted Solution

by:BlueVD
BlueVD earned 1336 total points
ID: 36922782
Here's a procedure to load html code into a TWebBrowser via Streams (faster and less dirty than saving and dealing with temporary files):
procedure LoadStream(WebBrowser: TWebBrowser; Stream: TStream);
var
  PersistStreamInit: IPersistStreamInit;
  StreamAdapter: IStream;
  MemoryStream: TMemoryStream;
begin
  WebBrowser.Navigate('about:blank');
  repeat
    Application.ProcessMessages;
    Sleep(0);
  until
    WebBrowser.ReadyState = READYSTATE_COMPLETE;
  if WebBrowser.Document.QueryInterface(IPersistStreamInit, PersistStreamInit) = S_OK then
  begin
    if PersistStreamInit.InitNew = S_OK then
    begin
      MemoryStream := TMemoryStream.Create;
      try
        MemoryStream.CopyFrom(Stream, 0);
        MemoryStream.Position := 0;
      except
        MemoryStream.Free;
        raise;
      end;
      StreamAdapter := TStreamAdapter.Create(MemoryStream, soOwned);
      PersistStreamInit.Load(StreamAdapter);
    end;
  end;
end;

Open in new window

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:starhu
ID: 36923095
So you mean to combine your 2 examples?

first this
"IndyStringList := TStringList.Create;
IndyStream := TMemoryStream.Create;
try
IndyStringList.LoadFromFile(OpenDialog1.FileName);
IndyStringList.Add(CRLF + '.' + CRLF);
IndyStringList.SaveToStream(IndyStream);
IndyStream.Position := 0;
IdMessage.Clear;
IdMessage.LoadFromStream(IndyStream);"

and then give the stream as parameter to procedure LoadStream(WebBrowser: TWebBrowser; Stream: TStream); ?
0
 
LVL 9

Assisted Solution

by:rinfo
rinfo earned 664 total points
ID: 36923561
Have you considered using chilkat  com object.
You can have an example code here on this link
http://www.example-code.com/delphi/parse-eml.asp
0
 
LVL 2

Expert Comment

by:BlueVD
ID: 36923743
After you load the message from the tstringlist, you check the MIME to see if it's an encoded html email. If so, check the first attachment (whitch is the html message) and if everything is ok, use the LoadStream procedure to load it in the twebbrower instance
you could also load plaintext with the LoadStream procedure (twebbrowser would just render it as plain text, thought you might have to encode special characters like <> using &raquo; etc codes...)
0
 

Author Comment

by:starhu
ID: 36939198
I will try it, thank you and I will let you know the result
0
 
LVL 101

Expert Comment

by:mlmcc
ID: 37144320
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
This is an update to some code that someone else posted on Experts Exchange. It is an alternate approach, I think a little easier to use, & makes sure that things like the Task Bar will update.
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Screencast - Getting to Know the Pipeline
Suggested Courses
Course of the Month11 days, 16 hours left to enroll

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question