2012-07-02 100 views
0

我的VB.NET應用程序旨在與Outlook並行工作以執行以下操作。VB.NET - Microsoft.Office.Interop.Outlook - 在工作站被鎖定時不起作用?

  1. 檢查新的未讀電子郵件,每10秒(定時器),從每場
  2. 捕獲數據(收件人,發件人,日期,主題,正文等),並存儲在SQL表中,
  3. 從收件箱中刪除電子郵件。

此應用程序完美地工作,直到工作站從屏幕保護程序超時時間鎖定自己的時刻。當PC解鎖後,它恢復正常操作並處理積壓的電子郵件。你可能會說只是禁用屏幕保護程序,但這臺PC只能通過微軟Remote Desktop Connection使用共享用戶帳戶訪問,所以這是不可能的。

這在Windows XP SP3和Outlook 2003下運行。我的問題不是真的找到解決方案,它更多的是找到原因。但顯然一個解決方案會很好。

下面是從應用程序代碼片段,它的基本Microsoft.Office.Interop.Outlook VB.NET代碼:

Public Sub Outlook_GetMail() 

    Dim sFileName As String = "senderexcludedlist.txt" 
    Dim ssenderexcludedlist As String = "" 

    If System.IO.File.Exists(sFileName) = True Then 
     Dim objReader As New System.IO.StreamReader(sFileName) 

     Do While objReader.Peek() <> -1 
      ssenderexcludedlist = objReader.ReadLine() 
     Loop 

     objReader.Dispose() 
    End If 

    Dim sMessage As String = "" 

    ' Create Outlook application. 
    Dim oApp As Microsoft.Office.Interop.Outlook.Application = New Microsoft.Office.Interop.Outlook.Application 

    ' Get Mapi NameSpace. 
    Dim oNS As Microsoft.Office.Interop.Outlook.NameSpace = oApp.GetNamespace("mapi") 
    oNS.Logon("Mailbox", Missing.Value, False, True) 

    ' Get Messages collection of Inbox. 
    Dim oInbox As Microsoft.Office.Interop.Outlook.MAPIFolder = oNS.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox) 
    Dim oItems As Microsoft.Office.Interop.Outlook.Items = oInbox.Items 

    ' Get unread e-mail messages. 
    oItems = oItems.Restrict("[Unread] = true") 

    ' Loop each unread message. 
    Dim oMsg As Microsoft.Office.Interop.Outlook.MailItem 
    Dim i As Integer 

    For i = 1 To oItems.Count 
     On Error Resume Next 

     oMsg = oItems.Item(i) 
     Dim strBody = oMsg.Body() 
     strBody = Replace(strBody, vbNewLine, "<BR>") 
     strBody = Replace(strBody, "'", "´")   

     Dim strSenderEmailAddress As String = oMsg.SenderEmailAddress 

     If InStr(ssenderexcludedlist, strSenderEmailAddress) = 0 And Not oMsg.SenderName.Contains("Leeds Tech Support") Then 

      sMessage = sMessage & oMsg.Subject & vbNewLine 
      sMessage = sMessage & oMsg.SenderName & vbNewLine 
      sMessage = sMessage & strSenderEmailAddress & vbNewLine 
      sMessage = sMessage & oMsg.CC & vbNewLine 
      sMessage = sMessage & oMsg.ReceivedTime & vbNewLine 
      sMessage = sMessage & oMsg.Body & vbNewLine 
      sMessage = sMessage & "---------------------------" & vbNewLine & vbNewLine 
      MsgLog.Text = sMessage & MsgLog.Text 

      'INSERT SQL HERE 
     End If 
     oMsg.UnRead = False 
     oMsg.Delete() 
    Next 

    ' Log off. 
    oNS.Logoff() 

    ' Clean up. 
    oApp = Nothing 
    oNS = Nothing 
    oItems = Nothing 
    oMsg = Nothing 
End Sub 
+0

我想它與工作站鎖定時從交換服務器斷開連接有關。你記錄錯誤/例外嗎?如果是這樣,當工作站鎖定時會發生什麼?另外,請檢查Windows事件日誌。 –

回答

2

我不能建議高度不夠,你簽出的Outlook贖回(HTTP ://www.dimastr.com/redemption/home.htm)。這將允許您與Exchange進行交互,而無需使用Outlook進行混淆。我有幾個應用程序基本上做你想做的事情;其中一些作爲服務運行,另一些作爲定時作業調用或交互式運行,但其中沒有一個與用戶是否登錄有關。

+0

您好,感謝您提供這方面的建議。你能幫助提供任何編碼示例嗎? – Andy