Solved

How to convert a .msg file into a .txt file

Posted on 2002-03-07
18
121,565 Views
Last Modified: 2011-08-18
I want to get a result just the same as if i opened a .msg-file in notepad and saving it to a txt-file

Can i use CreateProcess(with notepad.exe) to open  a .msg file and save it again.

This...

CreateOK := CreateProcess(nil, PChar('Notepad.exe '+FileName), nil, nil,False,
CREATE_NEW_PROCESS_GROUP+NORMAL_PRIORITY_CLASS,
nil, nil, StartInfo, ProcInfo);

Works file to open a file with notepad.... but can i save it??
0
Comment
Question by:hessmac
  • 4
  • 3
  • 3
  • +6
18 Comments
 

Author Comment

by:hessmac
ID: 6848635
I mean ... Works fiNe to open a file....
0
 
LVL 2

Expert Comment

by:DelphiArchitect
ID: 6849774
I don't think there is any way to control Notepad from another process (other than maybe poking keys which is flaky).

I'm not understanding what it is that Notepad does to the .msg file that you want.  Is the .msg file a text file?  If you can describe what effect opening and saving the .msg file in notepad has I'm quite certain I can help you achieve this in Delphi code (I have lots of file I/O experience).

Thanks,
Delphi Architect
James Higgins
0
 
LVL 3

Expert Comment

by:gandalf_the_white
ID: 6850032
DelphiArchitect is right

you can do all things that notepad can do
also in delphi with a memofield
so please tell us why do you need the
notepad save option?

gandalf
0
 
LVL 17

Expert Comment

by:Wim ten Brink
ID: 6850293
If the .msg file is a textfile you can just copy or rename it to a .txt file. But aren't .msg files some weird binary files? I believe these are outlook (not express) email files which are saved to files. Anyway, if it's a binary fileformat you will have to write a convertor which means you can't use Notepad. If it's a textfile then Delphi itself offers a lot of ways to handle the contents even without using notepad.

So, question back: which application generates these .msg files and what's it's file format?
0
 

Author Comment

by:hessmac
ID: 6854313
The .msg file is a outlook msg file . if you open it in notepad some parts you can read and som parts are weird(binary) characters.
The thing is.. I want to retreive all the email adresses which are in the msg-file, the msg-file can also contain a msg-file within itself(but notepad does also see that msg-file contents).
I have tried to open the msg-file with a memo component but it does not show the total contents, it leaves a lot of information not shown. So I opened  a msg-file with notepad which does show all the characters and saved it to the txt-format (from notepad), after that i opened it in a Memo or Richedit component and than you will see the total contents of the file so i could scan for all the email adresses. If you try it yourself you will see what i mean.  
0
 
LVL 17

Expert Comment

by:Wim ten Brink
ID: 6855758
Well, at home I use Outlook Express and Express will store emails in the .EML format which is just plain text. The Outlook .MSG file format however is a binary format which is probably a structured storage format. You might be able to read it if you know about COM+ and structured storage.
What you need to look into is the IStorage object combined with the StgOpenStorage API function and other storage related omethods and objects. Using this you might discover that the .MSG format holds a lot of valuable information once you've succesfully read the contents.

Actually, that's the difference between the .EML format and the .MSG format. The EML stores the email in it's raw format while the MSG stores it as an object. Which is quite nice but it makes the MSG format unreadable unless you use IStorage to read it's contents.

If you want to read a .MSG file, you'll have to learn a lot about COM. It's not too difficult but it's a lot of information and it overwhelmes inexperienced developers.
0
 
LVL 3

Expert Comment

by:gandalf_the_white
ID: 6856198
you could try to import it with an rtf-component
and then save it.

for instance: RTFapi.txt
from http://www.toolhouse-westware.de
is such a component

i haven't tested it myself, just read
about its features.

gandalf
0
 
LVL 17

Expert Comment

by:Wim ten Brink
ID: 6856518
Sorry, but .MSG files aren't rich-text files either. They are more compatible with MS-Word. Unfortunately, even Word can't read them correctly.

If you go to http://www.newriders.com/download/1578702216/ you will find two ZIP files containing sourcecode from the book "Delphi COM Programming" by Eric Harmon. (ISBN1-57870-221-6) Open the newest one and extract the file 'samples08.zip' and within this zip the folder/project 'SSView'. This is a structured storage viewer. If you use it to view the contents of the .MSG file you will see that it is indeed a structured storage file.
What it will show you is a treeview showing the objects stored in this file. All you have to do is determine which nodes to read to get the information you want.
I hope the sample source can help you. Otherwise consider purchasing the book I mentioned to learn a bit more about structured storage files. (Actually, word-documents, Excel-spreadsheets and several other Microsoft files are stored in the structured storage format so it is useful to be familiar with it.)
0
 
LVL 17

Expert Comment

by:Wim ten Brink
ID: 6856605
Sorry, but .MSG files aren't rich-text files either. They are more compatible with MS-Word. Unfortunately, even Word can't read them correctly.

If you go to http://www.newriders.com/download/1578702216/ you will find two ZIP files containing sourcecode from the book "Delphi COM Programming" by Eric Harmon. (ISBN1-57870-221-6) Open the newest one and extract the file 'samples08.zip' and within this zip the folder/project 'SSView'. This is a structured storage viewer. If you use it to view the contents of the .MSG file you will see that it is indeed a structured storage file.
What it will show you is a treeview showing the objects stored in this file. All you have to do is determine which nodes to read to get the information you want.
I hope the sample source can help you. Otherwise consider purchasing the book I mentioned to learn a bit more about structured storage files. (Actually, word-documents, Excel-spreadsheets and several other Microsoft files are stored in the structured storage format so it is useful to be familiar with it.)
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 6

Accepted Solution

by:
reddarin earned 200 total points
ID: 6856836
Hi everyone,

I think hessmac is looking for something a little cruder than delving into structured storage. You just want to access the file contents then grab all the email addresses you can find, right?

Try this:

procedure TForm1.Button1Click(Sender: TObject);
var
  sMem: TMemoryStream;
  sFile: string;
  sBig: string;
  sList: TStringList;
begin
  try
  sMem  := TMemoryStream.Create;
  sFile := 'Filenamehere.msg';
  sMem.LoadFromFile(sFile);
  SetLength(sBig,sMem.Size);
  sMem.Read(sBig[1],sMem.Size);
  finally
  sMem.Free;
  end;

  try
    sList := TStringList.Create;
    nPos := pos('@',sBig);

    while nPos <> 0 do
      begin
      //your code here to parse for email addresses
      end;
    memo1.Lines.Assign(sList);
  finally
    freeandnil(sList);
  end;
end;

reddarin
0
 
LVL 6

Expert Comment

by:reddarin
ID: 6856847
Looking at the file in notepad, it appears that the email addresses will be separated by a space on either end, if there is just one, or a space on the left and a semi-colon on the right if there are more than one, like this:

asdfsdf reddarin@blah.com;reddarin@blah.org asdasdfasdfasdfiluh reddarin@blah.gov asdfasdfkuh

reddarin
0
 
LVL 3

Expert Comment

by:darinw
ID: 6856864
Looking at the file in notepad, it appears that the email addresses will be separated by a space on either end, if there is just one, or a space on the left and a semi-colon on the right if there are more than one, like this:

asdfsdf reddarin@blah.com;reddarin@blah.org asdasdfasdfasdfiluh reddarin@blah.gov asdfasdfkuh

reddarin
0
 
LVL 3

Expert Comment

by:darinw
ID: 6857839
Ack!

Sorry about that. I forgot I was in as my moderator account.

Darin
0
 

Author Comment

by:hessmac
ID: 6878473
Just what I needed ! thanks a lot :-)
0
 

Expert Comment

by:sandy2000
ID: 7590448
How can i access .msg data using a vb code? I am looking for a similar code as given by reddarin
0
 
LVL 6

Expert Comment

by:reddarin
ID: 7590524
Hi sandy2000, try the VB topic area sandy2000:

http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/

...or maybe the Outlook topic area:

http://www.experts-exchange.com/Applications/MS_Office/Outlook/

You can point them back to this question for reference ( http://www.experts-exchange.com/Programming/Programming_Languages/Delphi/Q_20274682.html ) or copy paste the code.

reddarin
0
 

Expert Comment

by:akhachab
ID: 11755244
you can also use  Readln(),Writeln() or CreateFile() ,Readfile(),Writefile()
0
 

Expert Comment

by:ebi1
ID: 23318112
can someone please help me, and look at my post regarding reading MSG files from disk into outlook?
http://www.experts-exchange.com/Programming/Editors_IDEs/Delphi/Q_24023925.html

i need this REALLY urgent.
please someone help me
thank you
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

747 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now