【问题标题】:EWS SubscribeToPullNotifications does not get new emailsEWS SubscribeToPullNotifications 未收到新电子邮件
【发布时间】:2013-10-07 21:06:26
【问题描述】:

我使用 EWS 编写了以下代码来订阅拉取通知并阅读新电子邮件。一切正常。突然之间,它不再阅读新电子邮件。任何想法可能是什么原因?以及如何解决?

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 以来已经有新电子邮件。

【问题讨论】:

    标签: vb.net exchange-server exchangewebservices


    【解决方案1】:

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

    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
    

    【讨论】:

    • 谢谢!稍后我会试试这个并告诉你。
    猜你喜欢
    • 1970-01-01
    • 2015-01-16
    • 2019-09-13
    • 2019-06-02
    • 2017-08-11
    • 2010-12-09
    • 2013-01-21
    • 2018-09-18
    • 1970-01-01
    相关资源
    最近更新 更多