我的VB.NET应用程序旨在与 Outlook 一起工作以执行以下操作。
- 每 10 秒检查一次新的未读电子邮件(计时器),
- 从每个字段(To、From、Date、Subject、Body 等)捕获数据并存储在 SQL 表中,
- 从收件箱中删除电子邮件。
此应用程序可以完美运行,直到工作站因屏幕保护程序超时而锁定自身。解锁 PC 后,它会恢复正常操作并处理积压的电子邮件。您可能会说只是禁用屏幕保护程序,但是这台 PC 只能通过 Microsoft远程桌面连接使用共享用户帐户访问,因此这是不可能的。
这在 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