title: VB.NET - Microsoft.Office.Interop.Outlook - doesn't work when workstation is locked?
body: <p>My <a href="http://en.wikipedia.org/wiki/Visual_Basic_.NET" rel="nofollow">VB.NET</a> application is designed to work side by side with Outlook to perform the following.</p>
  
  <ol>
  <li>Check for new unread emails every 10 seconds (Timer),</li>
  <li>Capture data from each field (To, From, Date, Subject, Body, etc.) and store within a SQL table,</li>
  <li>Delete the email from inbox.</li>
  </ol>
  
  <p>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 <a href="http://en.wikipedia.org/wiki/Remote_Desktop_Services#Remote_Desktop_Connection" rel="nofollow">Remote Desktop Connection</a> using a shared user account so this isn't possible.</p>
  
  <p>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.</p>
  
  <p>Here is a snippet of code from the application, it's basic Microsoft.Office.Interop.Outlook VB.NET code:</p>
  
  <pre><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() &lt;&gt; -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, "&lt;BR&gt;")
          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 &amp; oMsg.Subject &amp; vbNewLine
              sMessage = sMessage &amp; oMsg.SenderName &amp; vbNewLine
              sMessage = sMessage &amp; strSenderEmailAddress &amp; vbNewLine
              sMessage = sMessage &amp; oMsg.CC &amp; vbNewLine
              sMessage = sMessage &amp; oMsg.ReceivedTime &amp; vbNewLine
              sMessage = sMessage &amp; oMsg.Body &amp; vbNewLine
              sMessage = sMessage &amp; "---------------------------" &amp; vbNewLine &amp; vbNewLine
              MsgLog.Text = sMessage &amp; 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
  </code></pre>