[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now


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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

649 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