Click here to Skip to main content
15,608,157 members
Articles / Productivity Apps and Services / Microsoft Office
Posted 28 Jul 2008


114 bookmarked

Outlook Drag and Drop in C#

Rate me:
Please Sign up or sign in to vote.
4.93/5 (89 votes)
28 Jul 2008CPOL6 min read
How to drag and drop multiple Outlook mail messages or message attachments to a C# WinForm.

Demo application form


A project came up at work the other day requiring the ability to drag and drop any number of mail messages or mail message attachments from Otlook into a WinForms application... Easy I thought, this has to be a common problem. I will just jump on CodeProject, find an example, and with a bit of tweaking, be running in no time. Now, if you are here, I am sure you now know how little information there is available on this topic, so I thought I would add to the very small pool with a complete example of how to drag and drop mail items or attachments from Outlook into a WinForms application without using the Outlook object model.

I am going to skip over explaining all the creating a form, allowing drag and drop, etc., stuff that you can find a million places on the Internet, and just focus on the code to make drag and drop from Outlook work. If you feel lost, go find another article with more basic drag and drop information, get a fully functional drag and drop app, then come back here and work from that code.

Using the Code

When I started writing the code, I decided the easiest way to work the functionality into the existing application was to create a new class that implemented the IDataObject interface that is normally provided when dragging and dropping onto a WinForm. The new class was to catch any calls to Outlook specific data formats, and pass all other calls through to the original IDataObject. Below, you can see how easy it is to use the class in a DragDrop event handler. The FileGroupDescriptor format returns a string array containing the names of each file dropped instead of the usual MemoryStream you would be used to if you have tackled this yourself, and the FileContents returns a MemoryStream array containing the binary contents of each file dropped.

private void Form1_DragDrop(object sender, DragEventArgs e)
    //wrap standard IDataObject in OutlookDataObject
    OutlookDataObject dataObject = new OutlookDataObject(e.Data);
    //get the names and data streams of the files dropped
    string[] filenames = (string[])dataObject.GetData("FileGroupDescriptor");
    MemoryStream[] filestreams = (MemoryStream[])dataObject.GetData("FileContents");

    for (int fileIndex = 0; fileIndex < filenames.Length; fileIndex++)
        //use the fileindex to get the name and data stream
        string filename = filenames[fileIndex];
        MemoryStream filestream = filestreams[fileIndex];

        //save the file stream using its name to the application path
        FileStream outputStream = File.Create(filename);

Understanding the Code

To understand what the OutlookDataObject class above is doing to get the file information, there are two things to take note of. The first is that information for the file names is returned from Outlook in a MemoryStream, which is actually a representation of the FILEGROUPDESCRIPTORA or FILEGROUPDESCRIPTORW structures. The second is that to get the file contents, you need the ability to specify an index to get anything past the first file, and the standard IDataObject does not expose this ability. All this is explained in detail below.

Getting the File Names

There are two versions of the file details returned from the IDataObject in the FileGroupDescriptor and FileGroupDescriptorW formats which map to the FILEGROUPDESCRIPTORA and FILEGROUPDESCRIPTORW structures, respectively. In this article, I will focus on the FileGroupDescriptor format which is the ASCII version; FileGroupDescriptorW (W for wide) is the Unicode version, and you will need to use it when working with non-ASCII file names, but they are handled in the same way.

//use the IDataObject to get the FileGroupDescriptor as a MemoryStream
MemoryStream fileGroupDescriptorStream = (MemoryStream)e.Data.GetData("FileGroupDescriptor");

Most examples you will see involve taking the MemoryStream above and converting each non-null byte from index 76 onwards to a char and appending that to a string. While this works adequately for one file drop, it gets a bit tricky when dropping more than that. The correct way is to take the returned bytes and cast it to a FILEGROUPDESCRIPTORA structure, which holds a count of items and an array of FILEDESCRIPTORA structures, which holds the file details. The definitions of these structures can be seen below.

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public sealed class FILEGROUPDESCRIPTORA
    public uint cItems;
    public FILEDESCRIPTORA[] fgd;

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public sealed class FILEDESCRIPTORA
    public uint dwFlags;
    public Guid clsid;
    public SIZEL sizel;
    public POINTL pointl;
    public uint dwFileAttributes;
    public System.Runtime.InteropServices.ComTypes.FILETIME ftCreationTime;
    public System.Runtime.InteropServices.ComTypes.FILETIME ftLastAccessTime;
    public System.Runtime.InteropServices.ComTypes.FILETIME ftLastWriteTime;
    public uint nFileSizeHigh;
    public uint nFileSizeLow;
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
    public string cFileName;

Now that the foundations are set, let's get into the code to actually convert the returned MemoryStream into something usable like a string array of file names. This involves putting the raw bytes into unmanaged memory and using Marshal.PtrToStructure to bring it back in as a structure. There is some extra marshalling in the code because the fgd array of the FILEGROUPDESCRIPTORA structure doesn't get populated as the PtrToStructure method doesn't work with variable length arrays.

//use IDataObject to get FileGroupDescriptor
//as a MemoryStream and copy into a byte array
MemoryStream fgdStream = 
byte[] fgdBytes = new byte[fgdStream.Length];
fgdStream.Read(fgdBytes, 0, fgdBytes.Length);

//copy the file group descriptor into unmanaged memory
IntPtr fgdaPtr = Marshal.AllocHGlobal(fgdBytes.Length);
Marshal.Copy(fgdBytes, 0, fgdaPtr, fgdBytes.Length);

//marshal the unmanaged memory to a FILEGROUPDESCRIPTORA struct
object fgdObj = Marshal.PtrToStructure(fgdaPtr, 

//create a array to store file names in
string[] fileNames = new string[fgd.cItems];

//get the pointer to the first file descriptor
IntPtr fdPtr = (IntPtr)((int)fgdaPointer + Marshal.SizeOf(fgdaPointer));

//loop for the number of files acording to the file group descriptor
for(int fdIndex = 0;fdIndex < fgd.cItems;fdIndex++)
    //marshal the pointer to the file descriptor as a FILEDESCRIPTORA struct
    object fdObj = Marshal.PtrToStructure(fdPtr, 
    NativeMethods.FILEDESCRIPTORA fd = (NativeMethods.FILEDESCRIPTORA)fdObj;
    //get file name of file descriptor and put in array
    fileNames[fdIndex] = fd.cFileName;

    //move the file descriptor pointer to the next file descriptor
    fdPtr = (IntPtr)((int)fdPtr + Marshal.SizeOf(fd));

At this point, we have now converted the MemoryStream into a string array that contains the name of each file dropped, which is a lot easier to work with. Outlook messages get the name of their subject with ".msg" on the end, and for Outlook message attachments, the file name of the attachment.

Getting the File Contents

The file contents sit behind the FileContents format. If you drag and drop a single attachment, then the default IDataObject works as expected and will return a MemoryStream containing that file's data. Things get more complex when dragging multiple attachments or Outlook email messages for different reasons. Multiple attachments pose an issue because the OS calls for drop data allow for an index to be specified, but the C# implementation of the IDataObject doesn't expose this directly. Mail messages are an issue because the OS call returns an IStorage which is a compound file type, and again, the C# implementation of the IDataObject lets us down by not handling this type of return, so you get a null.

Specifying an Index

To get at the content of multiple dropped files, an index needs to be specified to indicate which file contents are required. The default IDataObject doesn't allow this, but it can be cast to a COM IDataObject which will accept a FORMATETC structure that has an index property that can be set to indicate the file contents required.

//cast the default IDataObject to a com IDataObject
System.Runtime.InteropServices.ComTypes.IDataObject comDataObject;
comDataObject = (System.Runtime.InteropServices.ComTypes.IDataObject)e.Data;

//create a FORMATETC struct to request the data with from the com IDataObject
FORMATETC formatetc = new FORMATETC();
formatetc.cfFormat = (short)DataFormats.GetFormat(format).Id;
formatetc.lindex = 0; //zero based index to retrieve
formatetc.ptd = new IntPtr(0);

//create STGMEDIUM to output request results into

//using the com IDataObject interface get the data using the defined FORMATETC
comDataObject.GetData(ref formatetc, out medium);

As you can see in the example above, by changing the value of the lindex property of the FORMATETC structure, we can change the index of the file contents to retrieve. The result of the call is sitting in the STGMEDIUM structure; this contains a pointer to the actual result in the unionmember property, and the type of result at the pointer in the tymed property. There are three types of returns available to the STGMEDIUM, and each one is explained below.

The Stream Result (TYMED_ISTREAM)

If the tymed property of the STGMEDIUM is TYMED_ISTREAM, then the result is a stream. This is normally handled by the default IDataObject, but when working with the COM IDataObject, the handling code needs to be written again.

//marshal the returned pointer to a IStream object
IStream iStream = (IStream)Marshal.GetObjectForIUnknown(medium.unionmember);

//get the STATSTG of the IStream to determine how many bytes are in it
iStreamStat = new System.Runtime.InteropServices.ComTypes.STATSTG();
iStream.Stat(out iStreamStat, 0);
int iStreamSize = (int)iStreamStat.cbSize;

//read the data from the IStream into a managed byte array
byte[] iStreamContent = new byte[iStreamSize];
iStream.Read(iStreamContent, iStreamContent.Length, IntPtr.Zero);

//wrapped the managed byte array into a memory stream
Stream filestream = new MemoryStream(iStreamContent);

The Storage Result (TYMED_ISTORAGE)

If the tymed property of the STGMEDIUM is TYMED_ISTORAGE, then the result is a storage which is a compound file type. This is a little more complex to process than the stream as it needs to be copied into a memory backed IStorage so its data can then be read from the backing memory store.

NativeMethods.IStorage iStorage = null;
NativeMethods.IStorage iStorage2 = null;
NativeMethods.ILockBytes iLockBytes = null;
System.Runtime.InteropServices.ComTypes.STATSTG iLockBytesStat;
    //marshal the returned pointer to a IStorage object
    iStorage = (NativeMethods.IStorage)

    //create a ILockBytes (unmanaged byte array)
    iLockBytes = NativeMethods.CreateILockBytesOnHGlobal(IntPtr.Zero, true);
    //create a IStorage using the ILockBytes 
    //(unmanaged byte array) as a backing store
    iStorage2 = NativeMethods.StgCreateDocfileOnILockBytes(iLockBytes, 
                                                       0x00001012, 0);

    //copy the returned IStorage into the new memory backed IStorage
    iStorage.CopyTo(0, null, IntPtr.Zero, iStorage2);

    //get the STATSTG of the ILockBytes to determine 
    //how many bytes were written to it
    iLockBytesStat = new System.Runtime.InteropServices.ComTypes.STATSTG();
    iLockBytes.Stat(out iLockBytesStat, 1);
    int iLockBytesSize = (int)iLockBytesStat.cbSize;

    //read the data from the ILockBytes 
    //(unmanaged byte array) into a managed byte array
    byte[] iLockBytesContent = new byte[iLockBytesSize];
    iLockBytes.ReadAt(0, iLockBytesContent, iLockBytesContent.Length, null);

    //wrapped the managed byte array into a memory stream
    Stream filestream = new MemoryStream(iStreamContent);
    //release all unmanaged objects

The HGlobal Result (TYMED_HGLOBAL)

If the tymed property of the STGMEDIUM is TYMED_HGLOBAL, then the result is stored in a HGlobal. For the purposes of Outlook drag and drop, this type should never be returned, but for completeness, I use a little bit of Reflection on the original IDataObject to have that class handle it.

//get the internal ole dataobject and its GetDataFromHGLOBLAL method
BindingFlags bindingFlags = BindingFlags.NonPublic | BindingFlags.Instance;
FieldInfo innerDataField = 
  e.Data.GetType().GetField("innerData", bindingFlags);
IDataObject oleDataObject = 
MethodInfo getDataFromHGLOBLALMethod = 
  oleDataObject.GetType().GetMethod("GetDataFromHGLOBLAL", bindingFlags);

  new object[] { format, medium.unionmember });


Well, hopefully, that all helps someone. I have a few other Outlook tricks that I will be doing articles for; one is how to extract and save a message attachment without using the object model, another is how to use the code in this article to enable drag and drop of Outlook messages and attachments into IE (with appropriate security, so only good for the intranet).


  • 1 July 2008:
    • Original article.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
Founder Guava Development
Australia Australia
I am the Founder of Guava Development a Software Services Company located in Perth, Western Australia dedicated to improving productivity and reducing costs through the targeted and innovative application of software assisted workflows and packages.

I have been working in the industry for 10 years. My day job usually involves programming with C# but I have been known to mess around with just about everything.

Comments and Discussions

GeneralRe: Modified for Remote Desktop file transfer Pin
pm812-Jan-20 23:25
pm812-Jan-20 23:25 
QuestionHow to use the MAPI Tags to retrieve the emails date ? Pin
Liran Friedman17-Nov-14 1:18
Liran Friedman17-Nov-14 1:18 
QuestionGreat Code - Thanks! Pin
allendotnet31-Oct-14 5:13
allendotnet31-Oct-14 5:13 
QuestionMSG attachments do not appear Pin
franky_vij14-Sep-14 3:06
franky_vij14-Sep-14 3:06 
QuestionHow to make this cool software work in Windows 8 Pin
Jesper F. Nielsen3-Jul-14 8:20
Jesper F. Nielsen3-Jul-14 8:20 
AnswerRe: How to make this cool software work in Windows 8 Pin
mm-major10-Jul-14 2:00
mm-major10-Jul-14 2:00 
GeneralRe: How to make this cool software work in Windows 8 Pin
ivi70723-Jun-15 0:59
ivi70723-Jun-15 0:59 
GeneralRe: How to make this cool software work in Windows 8 Pin
George Hendrickson9-Jul-18 9:42
professionalGeorge Hendrickson9-Jul-18 9:42 
I still get an "Arithmetic overflow" error when I try to read the FileContents in the demo app...
MemoryStream[] filestreams = (MemoryStream[])dataObject.GetData("FileContents");

QuestionCuts of the first 4 letters of the file name Pin
Member 98896071-Jul-14 9:51
Member 98896071-Jul-14 9:51 
AnswerRe: Cuts of the first 4 letters of the file name Pin
David Ewen1-Jul-14 18:45
professionalDavid Ewen1-Jul-14 18:45 
GeneralCuts of the first X letters of the file name (the # of characters cut after the first filename varies) Pin
Avalonsoft25-Sep-14 13:06
Avalonsoft25-Sep-14 13:06 
GeneralRe: Cuts of the first X letters of the file name (the # of characters cut after the first filename varies) Pin
David Ewen25-Sep-14 15:45
professionalDavid Ewen25-Sep-14 15:45 
GeneralRe: Cuts of the first X letters of the file name (the # of characters cut after the first filename varies) Pin
Member 1003241330-Jan-17 20:28
Member 1003241330-Jan-17 20:28 
QuestionLicence other than CPOL? Pin
Sin Jeong-hun23-Jun-14 20:20
Sin Jeong-hun23-Jun-14 20:20 
QuestionDrag and Drop with outlook object is not working Pin
d.bock2-Jun-14 0:15
d.bock2-Jun-14 0:15 
AnswerRe: Drag and Drop with outlook object is not working Pin
manupraveenmanu9-Sep-14 23:45
manupraveenmanu9-Sep-14 23:45 
GeneralMy vote of 5 Pin
Andy Roberts31-May-14 9:07
Andy Roberts31-May-14 9:07 
QuestionDrag-drop MSG attachment not working under Outlook 2010 64 bit Pin
JennyWang29-May-14 8:42
JennyWang29-May-14 8:42 
QuestionRe: Drag-drop MSG attachment not working under Outlook 2010 64 bit Pin
jeanBa5-Sep-14 3:08
jeanBa5-Sep-14 3:08 
AnswerRe: Drag-drop MSG attachment not working under Outlook 2010 64 bit Pin
ivi70723-Jun-15 1:00
ivi70723-Jun-15 1:00 
BugMemory not released in OUTLOOK.exe Pin
Member 106488206-Mar-14 23:44
Member 106488206-Mar-14 23:44 
QuestionEquivalent in WPF Pin
David Brunelle13-Jan-14 10:07
David Brunelle13-Jan-14 10:07 
AnswerRe: Equivalent in WPF Pin
David Ewen13-Jan-14 13:58
professionalDavid Ewen13-Jan-14 13:58 
GeneralRe: Equivalent in WPF Pin
David Brunelle14-Jan-14 1:51
David Brunelle14-Jan-14 1:51 
QuestionDatetime of Message Pin
Member 76544939-Dec-13 5:21
Member 76544939-Dec-13 5:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.