如何捕获电子邮件

11
我已经在 Outlook 中创建了一个基本的自定义任务窗格。
我想将邮件拖放到任务窗格中。当拖放时,它应该允许我捕获邮件作为对象,从而可以对其进行处理,例如保存到 SharePoint 位置。
这可能吗?如果可以,有什么提示?
我正在使用 VS2013 C# .NET 4.0,Add-in 是针对 Outlook 2010/2013 的。

“对它进行操作”是什么意思?仅通过访问邮件消息作为原始.msg文件(文件名和内容为原始字节)就足够了吗? - Dávid Molnár
3个回答

6

前提和设置

  • Windows 10专业版
  • 带有Office开发的Visual Studio 2013终极版
  • Outlook 2013与一个电子邮件账户

项目

  • In Visual Studio select New Project -> Visual C# -> Office/SharePoint -> Office Add-ins -> Outlook 2013 Add-in
  • Right click on the project -> Add -> User Control
  • 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;
    

Outlook 2013 Custom Pane

拖放邮件

  • Double click UserControl1 in the Solution Explorer. This opens the designer window.
  • 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", https://dev59.com/KVcP5IYBdhLWcg3w-Otz#43577490
        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);
                }
            }
        }
    }
    
你可以从CodeProject获取 "iwantedue.Windows.Forms.OutlookDataObject",或者使用这个GitHub gist

演示

Outlook 2013 Custom Pane Drag and drop email message


1
应该归功于David Ewen :)。我们的答案基本上是他在代码项目上的工作。我以前使用过它,没有任何问题,不知道OP是否真的有任何问题... - George Vovos

0

您可以通过检查Explorer类的Selection属性来获取已删除的项目或多个项目(如果允许)。 在以下文章中了解更多信息:

  • {{link2:如何:获取拖放到.NET表单上的Outlook电子邮件项目的属性}}
  • {{link3:Outlook,自定义任务窗格和拖放问题}}

0
尝试使用类似这样的代码。
        public static string[] GetDropedFiles(DragEventArgs e)
        {
            string[] files = null;
            var outlookFile = e.Data.GetData("FileGroupDescriptor", true) as MemoryStream;
            if (outlookFile != null)
            {
                OutlookEmailObject dataObject = new OutlookEmailObject(e.Data);

                var filenames = (string[])dataObject.GetData("FileGroupDescriptorW");
                var filestreams = (MemoryStream[])dataObject.GetData("FileContents");

                files = new string[filenames.Length];
                for (int fileIndex = 0; fileIndex < filenames.Length; fileIndex++)
                {
                    string filename = filenames[fileIndex];
                    MemoryStream filestream = filestreams[fileIndex];

                    string path = Path.GetTempPath();
                    string fullFileName = path + filename;

                    FileStream outputStream = File.Create(fullFileName);
                    filestream.WriteTo(outputStream);
                    outputStream.Close();

                    files[fileIndex] = fullFileName;
                }
            }
            else
                files = (string[])e.Data.GetData(DataFormats.FileDrop);

            return files;
        }

您可以在此处获取OutlookEmailObject类(下载代码示例):
http://www.codeproject.com/Articles/28209/Outlook-Drag-and-Drop-in-C

(当然,在完成操作后,您应该删除所有临时文件)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接