我使用EWS编写了以下代码来订阅Pull通知并阅读新电子邮件。它工作得很好。突然间它没有读取新的电子邮件。任何想法可能是什么原因?以及如何解决它?
Imports Microsoft.Exchange.WebServices.Data
Imports System.Threading
Public Class FormTest
Dim subscription As PullSubscription
Dim service As ExchangeService
Private Sub ButtonStart_Click(sender As Object, e As EventArgs) Handles ButtonStart.Click
service = New ExchangeService
service.Credentials = New WebCredentials("myusername", "mypassword", "mydomain")
service.Url = New Uri("https://webmail.mydomain.com/EWS/exchange.asmx")
subscription = service.SubscribeToPullNotifications(New FolderId() {WellKnownFolderName.Inbox}, 1440, Nothing, EventType.NewMail)
End Sub
Private Sub ButtonPoll_Click(sender As Object, e As EventArgs) Handles ButtonPoll.Click
PollEmails()
End Sub
Private Sub PollEmails()
Dim events As GetEventsResults = subscription.GetEvents()
For Each itemEvent As ItemEvent In events.ItemEvents
Dim message As EmailMessage = EmailMessage.Bind(service, itemEvent.ItemId)
message.Load()
' Do something with 'message'
Next
End Sub
End Class
基本上当我按下ButtonPoll时,事件不包含任何新的evenet,即使自按下ButtonStart以来还有新的电子邮件。
答案 0 :(得分:0)
Heyo !!我做的事非常相似。看起来你已经在一个半月前发布了这个问题,所以你现在可能已经解决了这个问题,但我想把我最初的经历放在这里给其他可能正在寻找的人。我在下面发布的方式是一种无需按下轮询按钮即可获得通知的方法。这可能有所帮助!我把所有花哨的东西都删掉了,因为学习总是很重要,但最初的部分是最难以理解的部分。
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