如何捕获电子邮件

osh*_*nen 10 c# sharepoint outlook-addin outlook-2010

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

我想拖动一封电子邮件并将其放入任务窗格.当被删除时,它应该允许我将电子邮件捕获为我猜的对象,允许我用它来做事情,例如保存到sharepoint位置.

那可能吗?如果是这样,任何指针?

我使用的是VS2013 C#.NET 4.0,加载项适用于Outlook 2010/2013.

Dáv*_*nár 5

先决条件和设置

  • Windows 10专业版
  • Visual Studio 2013 Ultimate与Office开发
  • 带有电子邮件帐户的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;
    
    Run Code Online (Sandbox Code Playgroud)

Outlook 2013自定义窗格

拖放消息

  • 双击解决方案资源管理器中的UserControl1.这将打开设计器窗口.
  • 在Properties中设置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);
                }
            }
        }
    }
    
    Run Code Online (Sandbox Code Playgroud)

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

演示

Outlook 2013自定义窗格拖放电子邮件