【问题标题】:How to capture an email如何捕获电子邮件
【发布时间】:2017-09-18 13:22:55
【问题描述】:

我在 Outlook 中创建了一个基本的自定义任务窗格。

我想将一封电子邮件拖放到任务窗格中。删除后,它应该允许我将电子邮件捕获为我猜想的对象,让我可以用它做一些事情,例如保存到共享点位置。

这可能吗?如果有,有什么指点?

我使用的是 VS2013 C# .NET 4.0,插件适用于 Outlook 2010/2013。

【问题讨论】:

  • “用它做事”是什么意思?将邮件消息作为原始 .msg 文件访问就足够了吗? (文件名和内容作为原始字节)

标签: c# sharepoint outlook-addin outlook-2010


【解决方案1】:

先决条件和设置

  • Windows 10 专业版
  • 具有 Office 开发功能的 Visual Studio 2013 Ultimate
  • 带有电子邮件帐户的 Outlook 2013

项目

  • 在 Visual Studio 中选择新建项目 -> Visual C# -> Office/SharePoint -> Office 插件 -> Outlook 2013 插件
  • 项目右键->添加->用户控制
  • 打开“ThisAddIn.cs”并将以下代码添加到“ThisAddIn_Startup”方法中:

    var myCustomPane= this.CustomTaskPanes.Add(new UserControl1(), "My Pane");
    myCustomPane.Visible = true;
    

拖放消息

  • 在解决方案资源管理器中双击 UserControl1。这将打开设计器窗口。
  • 在属性中设置 AllowDrop = True 并连接两个事件处理程序 DragDropDragEnter

    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);
                }
            }
        }
    }
    

您可以从CodeProject 获取"iwantedue.Windows.Forms.OutlookDataObject",也可以使用此GitHub gist

演示

【讨论】:

  • 功劳应该归功于 David Ewen :)。我们的答案基本上是他在代码项目上的工作。我以前用过没问题,不知道OP到底有没有问题...
【解决方案2】:

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

【讨论】:

    【解决方案3】:

    试试这样的

            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

    (当然你应该在完成后删除所有临时文件)

    【讨论】:

      猜你喜欢
      • 2011-12-14
      • 1970-01-01
      • 2014-08-02
      • 1970-01-01
      • 2014-01-25
      • 2013-10-17
      • 1970-01-01
      • 1970-01-01
      • 2014-01-26
      相关资源
      最近更新 更多