Dragging Outlook message to .NET application

Posted on 2004-11-02
Last Modified: 2010-05-18
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 (, 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 / c#.

Thanks for any and all help!
Question by:Alex_van_der_Peet
    LVL 18

    Assisted Solution

    LVL 96

    Accepted Solution

    Also, did you think about Outlook automation where you can extract the message without drag and drop?


    Author Comment

    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.
    LVL 96

    Expert Comment

    by:Bob Learned
    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()

    LVL 18

    Expert Comment


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    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

    Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
    This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
    This video Micro Tutorial is the first in a two-part series that shows how to create and use custom scanning profiles in Nuance's PaperPort 14.5 ( But the ability to create custom scanning profiles al…
    Want to pick and choose which updates you receive? Feel free to check out this quick video on how to manage your email notifications.

    934 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

    18 Experts available now in Live!

    Get 1:1 Help Now