Public Class Program
Shared outlookApp As New Microsoft.Office.Interop.Outlook.ApplicationClass()
Shared ns As [NameSpace]
Public Shared Sub Main()
msgbox("start to monitor new emails")
ns = outlookApp.GetNamespace("MAPI")
AddHandler outlookApp.NewMailEx , AddressOf outlookApp_NewMailEx
AddHandler outlookApp.NewMail , AddressOf outlookApp_NewMail
While True
End While
End Sub
Private Shared Sub outlookApp_NewMail()
msgbox("a new message comes: new email")
End Sub
Private Shared Sub outlookApp_NewMailEx(ByVal EntryIDCollection As String)
msgbox("a new message comes")
AnalyzeNewItem(EntryIDCollection)
End Sub
Private Shared Sub AnalyzeNewItem(ByVal entry As String)
Dim inbox = ns.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox)
Dim allmails As List(Of Microsoft.Office.Interop.Outlook.MailItem) = New List(Of Microsoft.Office.Interop.Outlook.MailItem)()
For Each item In inbox.Items
If TypeOf item Is Microsoft.Office.Interop.Outlook.MailItem Then
Dim mail = TryCast(item, Microsoft.Office.Interop.Outlook.MailItem)
allmails.Add(mail)
End If
Next
Dim latest = allmails.Max(Function(s) s.ReceivedTime)
Dim latestMailItem = allmails.FirstOrDefault(Function(s) s.ReceivedTime = latest)
If latestMailItem IsNot Nothing Then
msgbox(latestMailItem.Subject)
msgbox(latestMailItem.[To])
msgbox(latestMailItem.SenderName)
msgbox(latestMailItem.ReceivedTime)
msgbox(latestMailItem.Body)
End If
End Sub
End Class