0

我使用 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 时,事件不包含任何新的事件,即使自按下 ButtonStart 以来已经有新的电子邮件。

4

1 回答 1

0

喂!!我正在做一些非常相似的事情。看起来你在一个半月前发布了这个问题,所以你现在可能已经解决了,但我想把我的初步经验放在这里,以供其他可能正在寻找的人使用。我在下面发布的方式是一种无需按下投票按钮即可获得通知的方式。这可能会有所帮助!我把所有花哨的东西都去掉了,因为学习总是很重要的,但它的最初部分是最令人讨厌的部分。

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
于 2013-11-18T15:16:07.487 回答