Link to home
Start Free TrialLog in
Avatar of hessmac
hessmac

asked on

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

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??
Avatar of hessmac
hessmac

ASKER

I mean ... Works fiNe to open a file....
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
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
Avatar of Wim ten Brink
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?
Avatar of hessmac

ASKER

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.  
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.
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
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.)
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.)
ASKER CERTIFIED SOLUTION
Avatar of reddarin
reddarin

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
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
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
Ack!

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

Darin
Avatar of hessmac

ASKER

Just what I needed ! thanks a lot :-)
How can i access .msg data using a vb code? I am looking for a similar code as given by reddarin
Hi sandy2000, try the VB topic area sandy2000:

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

...or maybe the Outlook topic area:

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

You can point them back to this question for reference ( https://www.experts-exchange.com/questions/20274682/How-to-convert-a-msg-file-into-a-txt-file.html ) or copy paste the code.

reddarin
you can also use  Readln(),Writeln() or CreateFile() ,Readfile(),Writefile()
can someone please help me, and look at my post regarding reading MSG files from disk into outlook?
https://www.experts-exchange.com/questions/24023925/Loading-outlook's-MSG-files-from-disk-into-outlook.html

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