喂!!我正在做一些非常相似的事情。看起来你在一个半月前发布了这个问题,所以你现在可能已经解决了,但我想把我的初步经验放在这里,以供其他可能正在寻找的人使用。我在下面发布的方式是一种无需按下投票按钮即可获得通知的方式。这可能会有所帮助!我把所有花哨的东西都去掉了,因为学习总是很重要的,但它的最初部分是最令人讨厌的部分。
Imports System.Net
Imports System.Net.Security
Imports Microsoft.Exchange.WebServices.Data
Imports System.Security.Cryptography.X509Certificates
Public Class MainForm
Private Sub ExecuteButton_Click(sender As System.Object, e As System.EventArgs) Handles ExecuteButton.Click
Console.Clear()
Dim exch As ExchangeService = New ExchangeService(ExchangeVersion.Exchange2010_SP2)
exch.Url = New Uri("https://" + ExchangeURLTB.Text + "/EWS/Exchange.asmx")
exch.UseDefaultCredentials = False
exch.Credentials = New System.Net.NetworkCredential(UsernameTB.Text, PasswordTB.Text, DomainTB.Text)
ServicePointManager.ServerCertificateValidationCallback = New RemoteCertificateValidationCallback(AddressOf ValidateCertificate)
Dim subscribeStreaming As StreamingSubscription = exch.SubscribeToStreamingNotifications(New FolderId() {WellKnownFolderName.Inbox}, EventType.NewMail, EventType.Created, EventType.Deleted)
Dim subscribeStreamingConnection = New StreamingSubscriptionConnection(exch, 30)
subscribeStreamingConnection.AddSubscription(subscribeStreaming)
AddHandler subscribeStreamingConnection.OnNotificationEvent, AddressOf OnNotificationEvent
AddHandler subscribeStreamingConnection.OnSubscriptionError, AddressOf OnSubscriptionError
AddHandler subscribeStreamingConnection.OnDisconnect, AddressOf OnDisconnect
subscribeStreamingConnection.Open()
Console.Write("Steaming subscription open")
End Sub
Private Sub OnNotificationEvent(sender As Object, args As NotificationEventArgs)
Dim subscription As StreamingSubscription = args.Subscription
For Each notification As NotificationEvent In args.Events
Select Case notification.EventType
Case EventType.NewMail
Console.WriteLine(vbNewLine)
Console.WriteLine("-------------Mail created:-------------")
End Select
Next
End Sub
Private Sub OnSubscriptionError(sender As Object, args As SubscriptionErrorEventArgs)
End Sub
Private Sub OnDisconnect(sender As Object, args As SubscriptionErrorEventArgs)
End Sub
Private Function ValidateCertificate(ByVal sender As Object, ByVal certificate As X509Certificate, ByVal chain As X509Chain, ByVal sslPolicyErrors As SslPolicyErrors) As Boolean
'Return True to force the certificate to be accepted.
Return True
End Function
End Class