2008-11-25 43 views
30

我正在使用.Net 3.5 Sp1上的C#編寫的WPF中編寫的Windows客戶端上,其中要求是客戶端收到的電子郵件中的數據可以存儲在數據庫中。現在最簡單的方法就是複製和粘貼文本,主題,聯繫信息和手動收到的時間使用關節炎誘導量的ctrl-c/ctrl-v。將一個或多個郵件從Outlook拖放到C#WPF應用程序

我認爲一個簡單的方法來處理這個問題將允許用戶從Outlook中拖動一個或多個電子郵件(他們都使用Outlook 2007當前)到窗口中,允許我的應用程序提取必要的信息併發送它到後端系統進行存儲。

然而,幾個小時的谷歌搜索有關這方面的信息似乎表明,有關這個看似基本的任務的信息缺乏驚人的缺乏。我認爲像這樣的東西在許多不同的設置中會很有用,但到目前爲止我所能找到的所有東西都是非解決方案的一半。

有沒有人有任何建議如何做到這一點?由於我只是要閱讀郵件,不要發送任何內容或做任何惡意的事情,對於不涉及被憎恨的安全彈出窗口的解決方案而言,這會很好,但任何事情都不可能完成。

基本上,如果我可以得到所有被選中的郵件列表,拖放到Outlook中,我將可以自己處理其他郵件!

謝謝!

符文

+0

符文你還在尋找答案嗎? – cgreeno 2008-12-23 10:25:43

+0

嗨克里斯 - 是的,我在上個月剛剛完成了一個完全不同的項目。我會盡快跟進,只要我的頭在水面之上。 – 2009-02-05 08:09:19

+0

不應該綁定到您的Exchange服務器,並以這種方式訪問​​Outlook郵箱?爲什麼不考慮CDO對​​象?這使您可以從Exchange服務器獲取消息收集。 http://msdn.microsoft.com/en-us/library/ms978698.aspx – D3vtr0n 2009-06-10 16:46:25

回答

32

我發現一個很好的article應該做你需要的。

UPDATE

我能得到該文章中WPF工作稍加調整中的代碼,下面是你需要做的改變。

變化從System.Windows.Forms.IDataObject所有引用System.Windows.IDataObject

在OutlookDataObject構造,改變

FieldInfo innerDataField = this.underlyingDataObject.GetType().GetField("innerData", BindingFlags.NonPublic | BindingFlags.Instance); 

FieldInfo innerDataField = this.underlyingDataObject.GetType().GetField("_innerData", BindingFlags.NonPublic | BindingFlags.Instance); 

更改所有DataFormats。 GetFormat調用DataFormats.GetDataFormat

public void SetData(string format, bool autoConvert, object data) 
{ 
    this.underlyingDataObject.SetData(format, autoConvert, data); 
} 

更改SetData的實施,

public void SetData(string format, object data, bool autoConvert) 
{ 
    this.underlyingDataObject.SetData(format, data, autoConvert); 
} 

這些變化,我能得到它的郵件保存到文件作爲文章一樣。對不起格式化,但編號/項目符號列表不適用於代碼片段。

3

在XAML中,你需要設置你的事件:

<TextBlock 
     Name="myTextBlock" 
     Text="Drag something into here" 
     AllowDrop="True" 
     DragDrop.Drop="myTextBlock_Drop" 
     /> 

一旦你已經設置的AllowDrop = True和設置你放棄那麼事件去後面的代碼,並建立您的活動:

private void myTextBlock_Drop(object sender, DragEventArgs e) 
{ 
     // Mark the event as handled, so TextBox's native Drop handler is not called. 
     e.Handled = true; 
     Stream sr; 

      //Explorer 
      if (e.Data.GetDataPresent(DataFormats.FileDrop, true)) 
       //Do somthing 

     //Email Message Subject 
     if (e.Data.GetDataPresent("FileGroupDescriptor")) 
     { 
      sr = e.Data.GetData("FileGroupDescriptor") as Stream; 
       StreamReader sr = new StreamReader(sr2);//new StreamReader(strPath, Encoding.Default); 
      //Message Subject 
        string strFullString = sr.ReadToEnd(); 
     } 


} 

如果你想打破它進一步下降,你可以使用: 文件描述符或FILECONTENTS作爲輪廓在以下article

你OT她的選擇是與展望MS Office Primary Interop Assemblies聯繫在一起,並以這種方式打破信息。

5

我發現了很多解決方案,建議您對所有文件名使用「FileGroupDescriptor」,並使用DragEventArgs對象上的「FileContents」來檢索每個文件的數據。 「FileGroupDescriptor」適用於電子郵件名稱,但「FileContents」返回null,因爲.Net中的IDataObject實現無法處理由COM返回的IStorage對象。

David Ewen有一個很好的解釋,優秀的樣本和代碼下載,在http://www.codeproject.com/KB/office/outlook_drag_drop_in_cs.aspx偉大的作品。

1

我假設你有一個在Outlook後面運行的Exchange服務器。

您可以執行的操作是從Exchange服務器檢索郵件,並根據郵件的EntryIDStoreID將其位置存儲在數據庫中。這裏有一個VB.Net片段:

Imports Microsoft.Office.Interop 

Public Class OutlookClientHandler 

Private _application As Outlook.Application 
Private _namespace As Outlook.NameSpace 

Public Sub New() 
    If Process.GetProcessesByName("outlook".ToLower).Length > 0 Then 
     _application = New Outlook.Application 
    Else 
     Dim startInfo As ProcessStartInfo = New ProcessStartInfo("outlook.exe") 
     startInfo.WindowStyle = ProcessWindowStyle.Minimized 
     Process.Start(startInfo) 

     _application = New Outlook.Application 
    End If 
End Sub 

' Retrieves the specified e-mail from Outlook/Exchange via the MAPI 
Public Function GetMailItem(ByVal entryID as String, ByVal storeID as String) As Outlook.MailItem 
    _namespace = _application.GetNamespace("MAPI") 
    Dim item As Outlook.MailItem 
    Try 
     item = _namespace.GetItemFromID(entryID, storeID) 
    Catch comex As COMException 
     item = Nothing ' Fugly, e-mail wasn't found! 
    End Try 

    Return item 
End Function 
End Class 

我猜你是舒適的使用MAPI,否則,你可以在這裏讀到:

http://msdn.microsoft.com/en-us/library/cc765775(v=office.12).aspx

從Outlook檢索選定的電子郵件

Public Function GetSelectedItems() As List(Of Object) 
    Dim items As List(Of Object) = New List(Of Object) 

    For Each item As Object In _application.ActiveExplorer().Selection 
     items.Add(item) 
    Next 

    Return items 
End Function 

從Outlook中檢索電子郵件後,您可以將它們推送到您的數據庫中!保存他們的EntryIDStoreID(你可能想要保存父母的(文件夾的)EntryIDStoreID)。

相關問題