Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Dragging Outlook message to .NET application

Posted on 2004-11-02
Medium Priority
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 (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!
Question by:Alex_van_der_Peet
  • 2
  • 2
LVL 18

Assisted Solution

armoghan earned 500 total points
ID: 12481206
LVL 96

Accepted Solution

Bob Learned earned 500 total points
ID: 12482779
Also, did you think about Outlook automation where you can extract the message without drag and drop?


Author Comment

ID: 12483013
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
ID: 12483163
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

ID: 12702455

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses
Course of the Month10 days, 10 hours left to enroll

572 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