在 VB.NET 中查找并 return 来自 outlook 的最旧电子邮件的 ReceivedTime
Find and return the ReceivedTime of the oldest email from outlook in VB.NET
好的,到目前为止,我能够显示最旧的未读邮件,但不能显示所有最旧的电子邮件。当我这样做时,它当然会停止程序。
Dim objOutlook As Outlook._Application
objOutlook = New Outlook.Application()
Dim objNS As Outlook._NameSpace = objOutlook.Session
Dim objRecipient As Outlook.Recipient = _
objNS.CreateRecipient("John Smith")
'Dim eMail As Outlook.MailItem
'Dim travelunreadold As String
Dim vItems
Dim vvItems
Dim vOlderItems
Dim vOlderunreadItems
If objRecipient.Resolve Then
Dim objFolder As Outlook.MAPIFolder = _
objNS.GetSharedDefaultFolder(objRecipient, _
Outlook.OlDefaultFolders.olFolderInbox)
txtTravUnread.Text = objFolder.UnReadItemCount
'For Each eMail In objFolder.Items
' If eMail.UnRead = True Then
' travelunreadold = eMail.ReceivedTime
' Txttravundate.Text = travelunreadold
' End If
'Next eMail
vItems = objFolder.Items.Restrict("[Unread] = true")
vItems.Sort("ReceivedTime", False) 'ascending order
vOlderunreadItems = vItems.GetFirst
Txttravundate.Text = vOlderunreadItems
vvItems = objFolder.Items
vvItems.Sort("ReceivedTime", False) 'ascending order
vOlderItems = vvItems.GetFirst
Txttravemdate.Text = vOlderItems
txtTravEmails.Text = objFolder.Items.Count
'Txttravemdate.Text = oldest.recievedtime
Else
Console.Write("Recipient could not be resolved.")
End If
我已经根据提供的更改进行了编辑。似乎它没有抓取日期,但是只要它在循环中,程序就不会挂起。
首先,不要遍历文件夹中的所有项目。这是非常低效的。使用物品。Find/FindNext/Restrict.
对于您的情况(最早的电子邮件),请使用 Items.Sort:
set vItems = objFolder.Items
vItems.Sort "ReceivedTime", false 'ascending order
set vOlderItems = vItems.GetFirst
如果您只想要最早的未读电子邮件,请使用限制:
set vItems = objFolder.Items.Restrict("[Unread] = true ")
vItems.Sort "ReceivedTime", false 'ascending order
set vOlderItems = vItems.GetFirst
感谢德米特里,它正在进行以下更改
Dim objOutlook As Outlook._Application
objOutlook = New Outlook.Application()
Dim objNS As Outlook._NameSpace = objOutlook.Session
Dim objRecipient As Outlook.Recipient = _
objNS.CreateRecipient("John Smith")
' Dim eMail As Outlook.MailItem
' Dim travelunreadold As String
Dim vItems
Dim vvItems
Dim vOlderItems
Dim vOlderunreadItems
If objRecipient.Resolve Then
Dim objFolder As Outlook.MAPIFolder = _
objNS.GetSharedDefaultFolder(objRecipient, _
Outlook.OlDefaultFolders.olFolderInbox)
txtTravUnread.Text = objFolder.UnReadItemCount
txtTravEmails.Text = objFolder.Items.Count
'For Each eMail In objFolder.Items
' If eMail.UnRead = True Then
' travelunreadold = eMail.ReceivedTime
' Txttravundate.Text = travelunreadold
' End If
'Next eMail
vvItems = objFolder.Items
vvItems.Sort("ReceivedTime", False) 'ascending order
vOlderItems = vvItems.GetFirst
Txttravemdate.Text = FormatDateTime(vOlderItems.ReceivedTime, 0)
vItems = objFolder.Items.Restrict("[Unread] = true")
vItems.Sort("ReceivedTime", False) 'ascending order
vOlderunreadItems = vItems.GetFirst
Txttravundate.Text = FormatDateTime(vOlderunreadItems.ReceivedTime, 0)
'Txttravemdate.Text = oldest.recievedtime
Else
Console.Write("Recipient could not be resolved.")
End If
好的,到目前为止,我能够显示最旧的未读邮件,但不能显示所有最旧的电子邮件。当我这样做时,它当然会停止程序。
Dim objOutlook As Outlook._Application
objOutlook = New Outlook.Application()
Dim objNS As Outlook._NameSpace = objOutlook.Session
Dim objRecipient As Outlook.Recipient = _
objNS.CreateRecipient("John Smith")
'Dim eMail As Outlook.MailItem
'Dim travelunreadold As String
Dim vItems
Dim vvItems
Dim vOlderItems
Dim vOlderunreadItems
If objRecipient.Resolve Then
Dim objFolder As Outlook.MAPIFolder = _
objNS.GetSharedDefaultFolder(objRecipient, _
Outlook.OlDefaultFolders.olFolderInbox)
txtTravUnread.Text = objFolder.UnReadItemCount
'For Each eMail In objFolder.Items
' If eMail.UnRead = True Then
' travelunreadold = eMail.ReceivedTime
' Txttravundate.Text = travelunreadold
' End If
'Next eMail
vItems = objFolder.Items.Restrict("[Unread] = true")
vItems.Sort("ReceivedTime", False) 'ascending order
vOlderunreadItems = vItems.GetFirst
Txttravundate.Text = vOlderunreadItems
vvItems = objFolder.Items
vvItems.Sort("ReceivedTime", False) 'ascending order
vOlderItems = vvItems.GetFirst
Txttravemdate.Text = vOlderItems
txtTravEmails.Text = objFolder.Items.Count
'Txttravemdate.Text = oldest.recievedtime
Else
Console.Write("Recipient could not be resolved.")
End If
我已经根据提供的更改进行了编辑。似乎它没有抓取日期,但是只要它在循环中,程序就不会挂起。
首先,不要遍历文件夹中的所有项目。这是非常低效的。使用物品。Find/FindNext/Restrict.
对于您的情况(最早的电子邮件),请使用 Items.Sort:
set vItems = objFolder.Items
vItems.Sort "ReceivedTime", false 'ascending order
set vOlderItems = vItems.GetFirst
如果您只想要最早的未读电子邮件,请使用限制:
set vItems = objFolder.Items.Restrict("[Unread] = true ")
vItems.Sort "ReceivedTime", false 'ascending order
set vOlderItems = vItems.GetFirst
感谢德米特里,它正在进行以下更改
Dim objOutlook As Outlook._Application
objOutlook = New Outlook.Application()
Dim objNS As Outlook._NameSpace = objOutlook.Session
Dim objRecipient As Outlook.Recipient = _
objNS.CreateRecipient("John Smith")
' Dim eMail As Outlook.MailItem
' Dim travelunreadold As String
Dim vItems
Dim vvItems
Dim vOlderItems
Dim vOlderunreadItems
If objRecipient.Resolve Then
Dim objFolder As Outlook.MAPIFolder = _
objNS.GetSharedDefaultFolder(objRecipient, _
Outlook.OlDefaultFolders.olFolderInbox)
txtTravUnread.Text = objFolder.UnReadItemCount
txtTravEmails.Text = objFolder.Items.Count
'For Each eMail In objFolder.Items
' If eMail.UnRead = True Then
' travelunreadold = eMail.ReceivedTime
' Txttravundate.Text = travelunreadold
' End If
'Next eMail
vvItems = objFolder.Items
vvItems.Sort("ReceivedTime", False) 'ascending order
vOlderItems = vvItems.GetFirst
Txttravemdate.Text = FormatDateTime(vOlderItems.ReceivedTime, 0)
vItems = objFolder.Items.Restrict("[Unread] = true")
vItems.Sort("ReceivedTime", False) 'ascending order
vOlderunreadItems = vItems.GetFirst
Txttravundate.Text = FormatDateTime(vOlderunreadItems.ReceivedTime, 0)
'Txttravemdate.Text = oldest.recievedtime
Else
Console.Write("Recipient could not be resolved.")
End If