2017-04-21 98 views
10

我在Outlook中創建了一個基本的自定義任務窗格。如何捕獲電子郵件

我想拖動電子郵件並將其放入任務窗格中。丟棄時,它應該允許我將電子郵件作爲一個對象進行捕獲,這讓我可以用它做任何事情,例如保存到分享點位置。

這可能嗎?如果是這樣,任何指針?

我正在使用VS2013 C#.NET 4.0和加載項用於Outlook 2010/2013。

+0

你說的 「做的東西與它」 是什麼意思?以原始.msg文件訪問郵件消息是否足夠? (文件名和內容作爲原始字節) –

回答

5

先決條件和設置

  • 的Windows 10 Pro的
  • 的Visual Studio 2013旗艦版與Office開發
  • 展望2013用電子郵件帳戶

項目

  • 在Visual Studio中選擇新建項目 - >的Visual C# - >辦公/的SharePoint - > Office插件 - > Outlook 2013中加載
  • 右鍵單擊項目 - >添加 - >用戶控制
  • 打開 「ThisAddIn.cs」 和下面的代碼添加到 「ThisAddIn_Startup」 的方法:

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

Outlook 2013 Custom Pane

拖放郵件

  • 雙擊解決方案資源管理器中的UserControl1。這會打開設計器窗口。
  • 在屬性設置的AllowDrop =真和掛鉤兩個事件處理程序的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

演示

Outlook 2013 Custom Pane Drag and drop email message

+1

信貸應該去大衛Ewen :)。我們的答案基本上是他的代碼項目的工作。我以前用過它沒有任何問題,不知道OP是否真的有任何問題... –

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

(當然,你應該刪除所有臨時文件完成後會將它們之後)

相關問題