Timeouts and freezing on loading or binding emails RRS feed

  • Question

  • I have a service that watches 2 email inboxes. One thread is created for each inbox. The new messages are read and marked as Read on startup, then the NewMail event handler handles all new mail after that.

    We have been using EWS version 14. We had no issues retrieving emails.

    After we moved the emails to Office 365, the problems started.

    On startup, the retrieval and handling of existing new messages is still working fine.

    After startup, when the NewMail event handler is triggered, the bind/load method times out, regardless of what we set the Timeout property to.

    We tried to switch to the newest EWS version (15.0).

    With the new version, the startup handling still works fine.

    However, trying to bind one email in the NewMail event handler makes no error, but makes the inbox thread quit.

    When there is a new email message in both inboxes, the entire service freezes on the Bind line (i let it sit for 40 minutes before having to kill the service).

    We tried both EWS versions with the following, with no success:

    The below code is used in the NewEmail event handler AND the startup processing of existing new messages. Why would the same code have issues in one place and not the other? How do i fix this?

    I have also tried to use EmailMessage.Bind using the NotificationEventArgs object, but i have the same issues.

    We tried a SyncLock to get around the lock up and/or timeout, but that did not help.

     Private Sub HaveMail(ByVal sender As Object, ByVal e As Microsoft.Exchange.WebServices.Data.NotificationEventArgs)
            Dim error_occured As Boolean = False
                    SyncLock lock
                        Logger.LogDebug("Just entered the Locked state of the HaveMail event")
                        Dim ir As FindItemsResults(Of Item) = service.FindItems(WellKnownFolderName.Inbox, New SearchFilter.IsEqualTo(EmailMessageSchema.IsRead, False), New ItemView(20))
                        Do While ir.Items.Count > 0
                            For Each i As EmailMessage In ir
                                    Logger.LogDebug("About to load a newly received email message")
                                Catch ex As Exception
                                    Throw New Exception("Error while loading an email message: " & ex.Message)
                                End Try
                                Dim rm As New ReceivedMessage
                                rm.ReceivedDateTime = i.DateTimeReceived
                                rm.ToAddress = i.DisplayTo
                                rm.Subject = i.Subject
                                rm.Body = i.Body
                                rm.FromAddress = i.From.Address
                                Logger.LogDebug("Just loaded a newly received email message from " & rm.FromAddress)
                                Dim rme As New ReceivedMessageEvent
                                rme.msg = rm
                                RaiseEvent GotMail(Me, rme)
                                Logger.LogDebug("Just processed a newly received email message from " & rm.FromAddress)
                                i.IsRead = True
                            ir = service.FindItems(WellKnownFolderName.Inbox, New SearchFilter.IsEqualTo(EmailMessageSchema.IsRead, False), New ItemView(20))
                    End SyncLock

    Please help!


    Tuesday, September 15, 2015 4:11 PM


  • I don't think this is in EWS problem sounds like you haven't set the DefaultConnectionLimit which will be 2 by defaults so whenever your code trying to make more the 2 concurrent connection it will just hang (Streaming Subscription holds a connections open). So the first thing I would try is setting that eg

    System.Net.ServicePointManager.DefaultConnectionLimit = 100

    The other thing to check is your network connection if your connection is being proxied that can break Streaming Subscriptions in that case try using an unproxied connection to see if that fixes the problem.


    • Marked as answer by kaftsmt Friday, September 18, 2015 4:24 PM
    Wednesday, September 16, 2015 5:33 AM