VB.NET - Microsoft.Office.Interop.Outlook - 工作站被锁定时不起作用?

时间:2012-07-02 13:49:00

标签: vb.net interop outlook windows-xp

我的VB.NET应用程序旨在与Outlook并行工作以执行以下操作。

  1. 每10秒(计时器)检查新的未读电子邮件,
  2. 从每个字段(To,From,Date,Subject,Body等)捕获数据并存储在SQL表中,
  3. 从收件箱中删除电子邮件。
  4. 这个应用程序完美无缺地工作,直到工作站从屏幕保护程序超时锁定自己的那一刻。当PC解锁时,它恢复正常操作并经过积压的电子邮件。您可能会说要禁用屏幕保护程序,但是这台PC只能由Microsoft 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
    

1 个答案:

答案 0 :(得分:2)

我不能高度推荐您查看Outlook Redemption(http://www.dimastr.com/redemption/home.htm)。这将允许您与Exchange进行交互,而无需使用Outlook进行清理。我有几个应用程序基本上是你试图做的;其中一些是作为服务运行的,另一些是在定时作业上调用或以交互方式运行,但是没有一个在用户是否登录时有任何问题。