I've created a basic Custom Task Pane in Outlook.
I want to drag an email and drop it into the task pane. When dropped, it should allow me to capture the email as an object I guess, allowing me to do stuff with it, as in save to a sharepoint location for example.
Is that possible? If so, any pointers?
I am using VS2013 C# .NET 4.0 and Add-in is for Outlook 2010/2013.
Email capture is the process of collecting email addresses from people who come to your website. Studies show that an email list can offer as high as 4300% return on investment (ROI).
Open "ThisAddIn.cs" and add the following code to the "ThisAddIn_Startup" method:
var myCustomPane= this.CustomTaskPanes.Add(new UserControl1(), "My Pane");
myCustomPane.Visible = true;
In Properties set AllowDrop = True and hook up two event handlers DragDrop and DragEnter.
private void UserControl1_DragEnter(object sender, DragEventArgs e)
{
// if you want to read the message data as a string use this:
if (e.Data.GetDataPresent(DataFormats.UnicodeText))
{
e.Effect = DragDropEffects.Copy;
}
// if you want to read the whole .msg file use this:
if (e.Data.GetDataPresent("FileGroupDescriptorW") &&
e.Data.GetDataPresent("FileContents"))
{
e.Effect = DragDropEffects.Copy;
}
}
private void UserControl1_DragDrop(object sender, DragEventArgs e)
{
// to read basic info about the mail use this:
var text = e.Data.GetData(DataFormats.UnicodeText).ToString();
var message = text.Split(new string[] { "\r\n" }, StringSplitOptions.None)[1];
var parts = message.Split('\t');
var from = parts[0]; // Email From
var subject = parts[1]; // Email Subject
var time = parts[2]; // Email Time
// to get the .msg file contents use this:
// credits to "George Vovos", http://stackoverflow.com/a/43577490/1093508
var outlookFile = e.Data.GetData("FileGroupDescriptor", true) as MemoryStream;
if (outlookFile != null)
{
var dataObject = new iwantedue.Windows.Forms.OutlookDataObject(e.Data);
var filenames = (string[])dataObject.GetData("FileGroupDescriptorW");
var filestreams = (MemoryStream[])dataObject.GetData("FileContents");
for (int fileIndex = 0; fileIndex < filenames.Length; fileIndex++)
{
string filename = filenames[fileIndex];
MemoryStream filestream = filestreams[fileIndex];
// do whatever you want with filestream, e.g. save to a file:
string path = Path.GetTempPath() + filename;
using (var outputStream = File.Create(path))
{
filestream.WriteTo(outputStream);
}
}
}
}
You can get "iwantedue.Windows.Forms.OutlookDataObject" from CodeProject or you can use this GitHub gist.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With