VB.NET和EWS未读邮件计数缓存异常

时间:2016-03-02 12:05:55

标签: vb.net exchangewebservices

我遇到一些问题,一些VB.Net代码使用EWS(Exchange Web服务)访问邮箱并计算未读邮件的数量。 它的工作原理,但我的问题是,我想在错误的登录信息中出现错误。 当我用错误的用户名填充我的代码时,我在此代码中获得了ServiceRequestException

Dim emails = service.FindItems(WellKnownFolderName.Inbox, sf, iv)

我尝试了一些Try Cach等等,但它似乎听到了错误。

希望有人可以帮助我如何捕获这样的错误,以便我的代码说明停止并崩溃。

Try

            Dim service = New ExchangeService(ExchangeVersion.Exchange2010_SP1)
            Dim credential As New NetworkCredential("username", "password", "localdomain")
            Dim Url = New Uri("https://mail.mydomain.com/EWS/Exchange.asmx")
            Dim prop As New PropertySet(BasePropertySet.FirstClassProperties)

            service.Url = Url
            service.Credentials = credential

            Dim arrSearchFilter(1) As SearchFilter
            arrSearchFilter(0) = New SearchFilter.IsEqualTo(EmailMessageSchema.IsRead, True)
            arrSearchFilter(1) = New SearchFilter.IsEqualTo(EmailMessageSchema.IsRead, False)

            Dim oSearchFilter As SearchFilter = Nothing
            oSearchFilter = New SearchFilter.SearchFilterCollection(LogicalOperator.Or, arrSearchFilter)

            Dim oView As ItemView = New ItemView(20)

            Dim iv = New ItemView(5) ' get last 5 emails

            iv.OrderBy.Add(ItemSchema.DateTimeReceived, SortDirection.Descending)

            Dim sf As SearchFilter = New SearchFilter.SearchFilterCollection(LogicalOperator.And, New SearchFilter.IsEqualTo(EmailMessageSchema.IsRead, False))

            Dim emails = service.FindItems(WellKnownFolderName.Inbox, sf, iv)

            tAntal.Text = emails.Count ' Writs the number of unread Mails

Catch
            MsgBox("Error4")
End Try

有关错误消息,请参阅图像。 enter image description here

1 个答案:

答案 0 :(得分:0)

尝试指定要捕获的异常类型,并且只留下在"尝试"内部抛出异常的行。块:

Dim service = New ExchangeService(ExchangeVersion.Exchange2010_SP1)
Dim credential As New NetworkCredential("username", "password", "localdomain")
Dim Url = New Uri("https://mail.mydomain.com/EWS/Exchange.asmx")
Dim prop As New PropertySet(BasePropertySet.FirstClassProperties)

service.Url = Url
service.Credentials = credential

Dim arrSearchFilter(1) As SearchFilter
arrSearchFilter(0) = New SearchFilter.IsEqualTo(EmailMessageSchema.IsRead, True)
arrSearchFilter(1) = New SearchFilter.IsEqualTo(EmailMessageSchema.IsRead, False)

Dim oSearchFilter As SearchFilter = Nothing
oSearchFilter = New SearchFilter.SearchFilterCollection(LogicalOperator.Or, arrSearchFilter)

Dim oView As ItemView = New ItemView(20)

Dim iv = New ItemView(5) ' get last 5 emails

iv.OrderBy.Add(ItemSchema.DateTimeReceived, SortDirection.Descending)

Dim sf As SearchFilter = New SearchFilter.SearchFilterCollection(LogicalOperator.And, New SearchFilter.IsEqualTo(EmailMessageSchema.IsRead, False))

Try
    Dim emails = service.FindItems(WellKnownFolderName.Inbox, sf, iv)
Catch Ex As ServiceRequestException
    MsgBox(Ex.Message)
End Try

tAntal.Text = emails.Count ' Writs the number of unread Mails