Dragging Outlook message to .NET application

Hello everybody,

I'm sure this has been asked several times before, but I just can't accept that there isn't a 'proper' solution for this. I know it's a long question, but no worries, there's a lot of code in here that may (or may not) be irrelevant!

I need to be able to drag a message from Outlook to my .NET app (vb.net, c#, whatever), where I have to disassemble it. I need to read subject, contents, and if possible attachments as well. I've been working with this (edited for clarity):

Private Sub tbReport_DragDrop(sender,e) Handles tbVerslag.DragDrop
        Dim iFormat As Integer
        Dim formats() As String = e.Data.GetFormats
        For iFormat = 0 To UBound(formats)
            Dim formatType As String = formats(iFormat).ToString
            Select Case formatType
                Case "Object Descriptor"

                Case "System.String", "Text"
                Case "UnicodeText"
                Case "Text"
                Case "RenPrivateSourceFolder"

                Case "RenPrivateMessages"

                Case "FileGroupDescriptor"

                Case "FileContents"

            End Select

So I know what kind of 'objects' (?) are present in the object that is passed to my app, but all I've been able to extract is the message's from/subject data, in a badly formatted way (from the FileGroupDescriptor I believe). I have no idea what the other types are, particularly the Ren objects, but I must admit I haven't google'd for it much either...

I've found this on MSDN, which lets you look at memory streams, but the best content I can get out of that (apart from empty strings) is a U with dots on it, like: ë

Private Sub CheckOutMemoryStream(ByVal ms As System.IO.MemoryStream)
        Dim byteArray As Byte()
        Dim count As Integer
        Dim charArray As Char()
        Dim uniEncoding As New UnicodeEncoding

        ' Write the stream properties to the console.
        Dim cap, len, pos As String
        cap = ms.Capacity.ToString()
        len = ms.Length.ToString()
        pos = ms.Position.ToString()

        ' Set the stream position to the beginning of the stream.
        ms.Seek(0, System.IO.SeekOrigin.Begin)

        ' Read the first 20 bytes from the stream.
        byteArray = New Byte(CType(ms.Length, Integer)) {}
        count = ms.Read(byteArray, 0, ms.Length)

        ' Read the remaining Bytes, Byte by Byte.
        While (count < ms.Length)
            byteArray(count) = Convert.ToByte(ms.ReadByte())
            count += 1
        End While

        ' Decode the Byte array into a Char array
        ' and write it to the console.
        charArray = New Char(uniEncoding.GetCharCount(byteArray, 0, count)) {}
        uniEncoding.GetDecoder().GetChars(byteArray, 0, count, charArray, 0)
        Dim finalString As String = charArray
        Dim i As Boolean = True
    End Sub

I've also found this on the net, but I have no idea what he/she means:

I have probably tried the same things that you have with the same
results. I do have a potential workaround. You have been trying to
get to the MailItem by looking at the DragEventArgs. What you should
be able to do is use the DragEventArgs to determine if the dropped
item is an Outlook item. (I think that the objectdescriptor format
will have the text "Outlook".) If it is Outlook then use the Outlook
object model to get the message with code similar to:

Outlook._Application myOlApp=new Outlook.ApplicationClass();
Outlook._Explorer myExp=myOlApp.ActiveExplorer();
Outlook._MailItem myMailItem=(Outlook.MailItem)myExp.Selection.Item(1);
string txtBody=myMailItem.Body;

Now you have the MailItem and you can do whatever you like. Since
this is on the drop event the ActiveExplorer should be the outlook
instance that you dragged from. Likewise the item you are dragging
would still be the active selection. Sorry I gave you the snippet in
C#, but you get the point. Hope that helps.

I have access to the Outlook mail store, so if someone can explain the solution above I should be able to go on. But a nice (free) component that transforms the dragged message to a nice instance of a message object with member fields and all would be even better. I've seen them for Delphi, and Java, but not for vb.net / c#.

Thanks for any and all help!
Who is Participating?
Bob LearnedConnect With a Mentor Commented:
Also, did you think about Outlook automation where you can extract the message without drag and drop?

Alex_van_der_PeetAuthor Commented:
Thanks for the tips guys!

Armoghan: I looked at the url, and it looks like a great sample for the attachment part of my problem. I'll try to get the from/subject etc. from the other fields that I found earlier, which might allow me to create a great complete solution.

TheLearnedOne: I thought about automation, which I also use to send mail from the same application, but I'm not sure I'd be able to reliably find the message I'm looking for. One of the comments I posted in my question seems to suggest the same, but I just don't really get how the code he shows is supposed to help.

All in all: I'll be going with drag and drop for now.
Bob LearnedCommented:
Ok, looking at the code for CheckOutMemoryStream, there is a more efficient way, using a StreamReader than all that code there:

Dim sr As StreamReader = New StreamReader(ms)

Dim finalString As String = sr.ReadToEnd()

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.