Solved

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

Posted on 2002-03-07
18
121,879 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Newbie Thread Programming 1 161
Delphi: how to implement a User Shortcut mapper? 1 120
Delphi XE10 Round Image 2 160
Dynamically Created Query 3 62
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

830 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