Drag'n'drop one or more mails from Outlook to C# WPF application Drag'n'drop one or more mails from Outlook to C# WPF application wpf wpf

Drag'n'drop one or more mails from Outlook to C# WPF application


I found a great article that should do exactly what you need to.

UPDATE

I was able to get the code in that article working in WPF with a little tweaking, below are the changes you need to make.

Change all references from System.Windows.Forms.IDataObject to System.Windows.IDataObject

In the OutlookDataObject constructor, change

FieldInfo innerDataField = this.underlyingDataObject.GetType().GetField("innerData", BindingFlags.NonPublic | BindingFlags.Instance);

To

FieldInfo innerDataField = this.underlyingDataObject.GetType().GetField("_innerData", BindingFlags.NonPublic | BindingFlags.Instance);

Change all DataFormats.GetFormat calls to DataFormats.GetDataFormat

Change the SetData implementation from

public void SetData(string format, bool autoConvert, object data){    this.underlyingDataObject.SetData(format, autoConvert, data);}

TO

public void SetData(string format, object data, bool autoConvert){    this.underlyingDataObject.SetData(format, data, autoConvert);}

With those changes, I was able to get it to save the messages to files as the article did. Sorry for the formatting, but numbered/bulleted lists don't work well with code snippets.


I found a lot of solutions suggesting you use the “FileGroupDescriptor” for all the file names and the “FileContents” on the DragEventArgs object to retrieve the data of each file. The “FileGroupDescriptor” works fine for the email message names, but “FileContents” returns a null because the implementation of the IDataObject in .Net cannot handle the IStorage object that is returned by COM.

David Ewen has a great explanation, excellent sample and code download that works great at http://www.codeproject.com/KB/office/outlook_drag_drop_in_cs.aspx.


In your Xaml you need to set up your Event:

<TextBlock        Name="myTextBlock"          Text="Drag something into here"        AllowDrop="True"         DragDrop.Drop="myTextBlock_Drop"        />

Once you have Set AllowDrop = True and Set you drop event then go to the code behind and set up your event:

private void myTextBlock_Drop(object sender, DragEventArgs e){         // Mark the event as handled, so TextBox's native Drop handler is not called.         e.Handled = true;         Stream sr;          //Explorer           if (e.Data.GetDataPresent(DataFormats.FileDrop, true))              //Do somthing        //Email Message Subject         if (e.Data.GetDataPresent("FileGroupDescriptor"))        {            sr = e.Data.GetData("FileGroupDescriptor") as Stream;                StreamReader sr = new StreamReader(sr2);//new StreamReader(strPath, Encoding.Default);            //Message Subject                    string strFullString = sr.ReadToEnd();         }}

If you wish to break it down further you can use:FILEDESCRIPTOR or FILECONTENTS as outline in the following article

your other option is to tie into outlooks MS Office Primary Interop Assemblies and break the message apart that way.