Dropping Emails onto a .NET Application

Overview

There a a number of articles on the net about how to build a .NET app that will accept emails dropped from Outlook. This article walks through a simple (if kludgy) approach.

Walkthrough

1. Create a new C# Windows Application in Visual Studio

2. Add a COM reference to Microsoft Outlook 10.0 Object Library.

3. Drop a textbox onto Form1 and set its AllowDrop property to True.

4. Add the following code to Form1 and you'll be able to mail items from Outlook onto your textbox.

{copytext|div1}
using System.Windows.Forms;

namespace DragDropTest
{
    public partial class Form1 : Form
    {
        //=========================================================================================
        public Form1()
        {
            InitializeComponent();
        }
        //=========================================================================================
        private void textBox1_DragOver(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent("RenPrivateMessages"))
                e.Effect = DragDropEffects.Copy;
        }
        //=========================================================================================
        private void textBox1_DragDrop(object sender, DragEventArgs e)
        {
            Outlook._Application app = new Outlook.Application();
            Outlook.Selection item = app.ActiveExplorer().Selection;

            for (int i = 1; i <= item.Count; i++)
            {
                Outlook.MailItem email = item.Item(1) as Outlook.MailItem;
                string from = email.SenderName;
            }
        }
    }
}