Import stream/byte array into MailItem or SafeMailItem?

  • Thread starter Thread starter Arild
  • Start date Start date
A

Arild

Hi,

currently we have a MemoryStream (easily converted to a
byte array) containing all the data from an Outlook
Message. Now we would like to retrieve different
properties from that message.

Is it possible to create an Outlook.MailItem or a
Redemption.SafeMailItem object and then import a
stream/byte array into that object for easy retrieval of
the mail item properties?

Also, we are working with C# - are there known problems
with interop here?

-Arild
 
I will guess that the format is Unicode (but I'm not quite sure).

I've attached some code to illustrate how we get the byte array from an unmanaged memory block. We have imported the Kernel32.dll in C# to lock and get size of the memory block. (Conversion to a Stream is done for further processing in the program).

protected Stream ReadByteStreamFromHandle( IntPtr handle ) {
Stream stream = null;
HandleRef handleRef = new HandleRef( null, handle );
IntPtr lockedHandle = Kernel32Lib.GlobalLock( handleRef );

try {
int size = Kernel32Lib.GlobalSize( handleRef );

byte[] buffer = new byte[size];
Marshal.Copy( lockedHandle, buffer, 0, size );

stream = new MemoryStream( buffer, 0, size );
} finally {
Kernel32Lib.GlobalUnlock( handleRef );
}

return stream;
}

-Arild
 
But what is *inside* of that stream? Message in the MSG, RFC 822 (EML),
TNEF, etc format?

Dmitry Streblechenko (MVP)
http://www.dimastr.com/
OutlookSpy - Outlook, CDO
and MAPI Developer Tool


Arild said:
I will guess that the format is Unicode (but I'm not quite sure).

I've attached some code to illustrate how we get the byte array from an
unmanaged memory block. We have imported the Kernel32.dll in C# to lock and
get size of the memory block. (Conversion to a Stream is done for further
processing in the program).
protected Stream ReadByteStreamFromHandle( IntPtr handle ) {
Stream stream = null;
HandleRef handleRef = new HandleRef( null, handle );
IntPtr lockedHandle = Kernel32Lib.GlobalLock( handleRef );

try {
int size = Kernel32Lib.GlobalSize( handleRef );

byte[] buffer = new byte[size];
Marshal.Copy( lockedHandle, buffer, 0, size );

stream = new MemoryStream( buffer, 0, size );
} finally {
Kernel32Lib.GlobalUnlock( handleRef );
}

return stream;
}

-Arild
-----Original Message-----
What is the format of the message in the stream?
http://www.dimastr.com/
OutlookSpy - Outlook, CDO
and MAPI Developer Tool
 
Sorry,

it is an MSG file from Microsoft Outlook :-)

-Arild

----- Dmitry Streblechenko (MVP) wrote: -----

But what is *inside* of that stream? Message in the MSG, RFC 822 (EML),
TNEF, etc format?

Dmitry Streblechenko (MVP)
http://www.dimastr.com/
OutlookSpy - Outlook, CDO
and MAPI Developer Tool


Arild said:
I will guess that the format is Unicode (but I'm not quite sure).
unmanaged memory block. We have imported the Kernel32.dll in C# to lock and
get size of the memory block. (Conversion to a Stream is done for further
processing in the program).
protected Stream ReadByteStreamFromHandle( IntPtr handle ) {
Stream stream = null;
HandleRef handleRef = new HandleRef( null, handle );
IntPtr lockedHandle = Kernel32Lib.GlobalLock( handleRef );
int size = Kernel32Lib.GlobalSize( handleRef );
byte[] buffer = new byte[size];
Marshal.Copy( lockedHandle, buffer, 0, size );
stream = new MemoryStream( buffer, 0, size );
} finally {
Kernel32Lib.GlobalUnlock( handleRef );
}
return stream; }
What is the format of the message in the stream?
http://www.dimastr.com/
OutlookSpy - Outlook, CDO
and MAPI Developer Tool
 
The only way to import an MSG file back to Outlook is to use
Namespace.CreateItemFromID().
<plug>In case of Redemption, you can also use SafeMailItem.Import</plug>
Note that in both cases the stream needs to be saved to a file first. You
can of course delete the file immediately after you are done.

Dmitry Streblechenko (MVP)
http://www.dimastr.com/
OutlookSpy - Outlook, CDO
and MAPI Developer Tool


Arild said:
Sorry,

it is an MSG file from Microsoft Outlook :-)

-Arild

----- Dmitry Streblechenko (MVP) wrote: -----

But what is *inside* of that stream? Message in the MSG, RFC 822 (EML),
TNEF, etc format?

Dmitry Streblechenko (MVP)
http://www.dimastr.com/
OutlookSpy - Outlook, CDO
and MAPI Developer Tool


Arild said:
I will guess that the format is Unicode (but I'm not quite sure).
from an
unmanaged memory block. We have imported the Kernel32.dll in C# to lock and
get size of the memory block. (Conversion to a Stream is done for further
processing in the program).
protected Stream ReadByteStreamFromHandle( IntPtr handle ) {
Stream stream = null;
HandleRef handleRef = new HandleRef( null, handle );
IntPtr lockedHandle = Kernel32Lib.GlobalLock( handleRef );
int size = Kernel32Lib.GlobalSize( handleRef );
byte[] buffer = new byte[size];
Marshal.Copy( lockedHandle, buffer, 0, size );
stream = new MemoryStream( buffer, 0, size );
} finally {
Kernel32Lib.GlobalUnlock( handleRef );
}
return stream; }
-Arild
-----Original Message-----
What is the format of the message in the stream?
Dmitry Streblechenko (MVP)
http://www.dimastr.com/
OutlookSpy - Outlook, CDO
and MAPI Developer Tool
 
Dmitry

Thank you very much for your answer

Will it alter the case if we are just interrested in reading the MailItems properties
--> Meaning thare we are not going to import the MSG-file into Outlook, but rather import the Stream into a document library in SharePoint together with some metadata from the MailItem -- We hoped that we could avoid saving the stream first (and import it into Outlook) to get it working :-

-Aril

----- Dmitry Streblechenko (MVP) wrote: ----

The only way to import an MSG file back to Outlook is to us
Namespace.CreateItemFromID()
<plug>In case of Redemption, you can also use SafeMailItem.Import</plug
Note that in both cases the stream needs to be saved to a file first. Yo
can of course delete the file immediately after you are done

Dmitry Streblechenko (MVP
http://www.dimastr.com
OutlookSpy - Outlook, CD
and MAPI Developer Tool
 
Next version of Redemption will support standalone MSG files (i.e. you do
not need to import them to Outlook first).
Contact me via private e-mail if you are interested in a copy of the beta.

Dmitry Streblechenko (MVP)
http://www.dimastr.com/
OutlookSpy - Outlook, CDO
and MAPI Developer Tool


Arild said:
Dmitry,

Thank you very much for your answer!

Will it alter the case if we are just interrested in reading the MailItems properties?
--> Meaning thare we are not going to import the MSG-file into Outlook,
but rather import the Stream into a document library in SharePoint together
with some metadata from the MailItem -- We hoped that we could avoid saving
the stream first (and import it into Outlook) to get it working :-)
 
I am deeply interested in the C# code you posted
Can you tell me more about what you are doing with an Outlook Message File in a MemoryStream

FY
An Outlook Message file is an OLE 2.0 Structured Storage Document, also known as a Compound Document
I wrote an an entire API in C# to access all the MAPI properties of an Outlook Message File (.msg) on disk

The API uses a 100% native C# component from http://www.jisys.com/ (Jisys Storage 1.0)
There is also another component, which can also read OLE 2.0 Structured Storage documents at:
http://www.desaware.com/StorageToolsL2.ht
The component is a COM object called StorageTools
I like the Jisys component best, as it is native C#

I thought maybe I could enhance my project, by using your idea with an Outlook Message File in a memory stream

Can you elaborate a bit on what you are doing
Thank
Russell Mange
Las Vegas, NV
 
Dmitry

Thanks again

I'll have to look into the options I've got with the information available. If the next version of Redemption is the answer, I'll certainly contact you

-Aril

----- Dmitry Streblechenko (MVP) wrote: ----

Next version of Redemption will support standalone MSG files (i.e. you d
not need to import them to Outlook first)
Contact me via private e-mail if you are interested in a copy of the beta

Dmitry Streblechenko (MVP
http://www.dimastr.com
OutlookSpy - Outlook, CD
and MAPI Developer Too
 
Well, we are currently working on a solution where we are enabling a Drag 'n Drop feature from MS Outlook to a Document Library in Windows SharePoint Services/SharePoint Portal Server v.2. The MemoryStream is only used to read the message from the machines memory, and import it into the document library by using the SPS API. (Or it can be done by the Web Services included in SPS v.2

However, we also need some of the properties from the MSG file/stream, like To, From, ID ++ to enable duplicate checking and import this meta-data together with the e-mail for later search purposes. These properties are much easier to retrieve from a MailItem, then reading from a Stream. However, as you can read from this posting, there are some issues with importing the Stream into a MailItem

It sounds like your API can solve many of the issues, but still (if I read you correct) we need to save the MSG file to disk

Well, that's the short version of our case

-Aril

----- Tymer wrote: ----

I am deeply interested in the C# code you posted
Can you tell me more about what you are doing with an Outlook Message File in a MemoryStream

FY
An Outlook Message file is an OLE 2.0 Structured Storage Document, also known as a Compound Document
I wrote an an entire API in C# to access all the MAPI properties of an Outlook Message File (.msg) on disk

The API uses a 100% native C# component from http://www.jisys.com/ (Jisys Storage 1.0)
There is also another component, which can also read OLE 2.0 Structured Storage documents at:
http://www.desaware.com/StorageToolsL2.ht
The component is a COM object called StorageTools
I like the Jisys component best, as it is native C#

I thought maybe I could enhance my project, by using your idea with an Outlook Message File in a memory stream

Can you elaborate a bit on what you are doing
Thank
Russell Mange
Las Vegas, NV
 
My nickname is Tymer, but this message is posted using my real name, cuz I
am using Outlook Express to send message on this newsgroup.

Okay, you say that you have some type of stream in memory, which Dimitry,
claims is an Outlook Message file.
An Outlook Message file is an Ole2 Structured Storage file.Using the JiSys
dll, you can absolutely read the properties, write new properites, and save
the Message File or document to disk.

As I mentioned I wrote an entire API in C#, using this control. This
control, will accept as input, a stream object from .NET.
You will not need the API I wrote, you will only need the JiSys control and
do your own thing.
Once, we have an Ole2Document object, in memory from your stream, the world
is at your command, you can do anything with the message you need.

However, I have no idea how you actually got the Outlook Message File in a
stream.
It sounds like you might have created the stream from a File Drop using
Drag-n-Drop. I am pretty savy with Messaging, and .NET so don't worry about
comments or incomplete code. If you are using Interop to get at the stream,
I would also need that portion of your code.

Can you post, or send me a copy of the C# source code, which gets the
Message File in a stream?

I will figure out if the stream does in fact have the Entire Outlook Message
file.

If the stream contains what Dimitry claims, we should have no trouble
getting this to work.
This is why your post, was so interesting to me, and maybe, we can both help
each other.

Arild said:
Well, we are currently working on a solution where we are enabling a Drag
'n Drop feature from MS Outlook to a Document Library in Windows SharePoint
Services/SharePoint Portal Server v.2. The MemoryStream is only used to read
the message from the machines memory, and import it into the document
library by using the SPS API. (Or it can be done by the Web Services
included in SPS v.2)
However, we also need some of the properties from the MSG file/stream,
like To, From, ID ++ to enable duplicate checking and import this meta-data
together with the e-mail for later search purposes. These properties are
much easier to retrieve from a MailItem, then reading from a Stream.
However, as you can read from this posting, there are some issues with
importing the Stream into a MailItem.
It sounds like your API can solve many of the issues, but still (if I read
you correct) we need to save the MSG file to disk.
Well, that's the short version of our case.

-Arild

----- Tymer wrote: -----

I am deeply interested in the C# code you posted.
Can you tell me more about what you are doing with an Outlook Message File in a MemoryStream?

FYI
An Outlook Message file is an OLE 2.0 Structured Storage Document,
also known as a Compound Document.
I wrote an an entire API in C# to access all the MAPI properties of
an Outlook Message File (.msg) on disk.
The API uses a 100% native C# component from http://www.jisys.com/ (Jisys Storage 1.0).
There is also another component, which can also read OLE 2.0
Structured Storage documents at:
http://www.desaware.com/StorageToolsL2.htm
The component is a COM object called StorageTools.
I like the Jisys component best, as it is native C#.

I thought maybe I could enhance my project, by using your idea with
an Outlook Message File in a memory stream.
 
Back
Top