简体   繁体   中英

VB.NET - Microsoft.Office.Interop.Outlook - doesn't work when workstation is locked?

My VB.NET application is designed to work side by side with Outlook to perform the following.

  1. Check for new unread emails every 10 seconds (Timer),
  2. Capture data from each field (To, From, Date, Subject, Body, etc.) and store within a SQL table,
  3. Delete the email from inbox.

This application works flawlessly right up until the moment where the workstation locks itself from the screensaver timeout. When the PC is unlocked it resumes normal operation and goes through the backlog of emails. You're probably going to say to just disable the screensaver, however this PC is only accessed by Microsoft Remote Desktop Connection using a shared user account so this isn't possible.

This runs under Windows XP SP3 and Outlook 2003. My issue isn't really to find a solution, it's more to just find the cause. But obviously a solution would be nice.

Here is a snippet of code from the application, it's basic Microsoft.Office.Interop.Outlook VB.NET code:

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

I can't recommend highly enough that you check out Outlook Redemption (http://www.dimastr.com/redemption/home.htm). This will allow you to interact with Exchange without having to muck about with Outlook. I have several applications that do essentially what you are attempting to do; some of them run as a service, others get called on a timed job or run interactively, but none of them have any problems with whether the user is logged in or not.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM