如何使用Exchange Web服务从Exchange Online 2013收件箱中获取邮件ID列表

时间:2014-04-16 16:51:50

标签: vb.net web service exchange-server ews-managed-api

我正在转换一些由前员工使用Exchange 2007中的Exchange Web Services API编写的代码。编写代码时,我们有一个内部部署的Exchange 2007 Server。现在我们已将电子邮件移至Exchange Online,并且我希望使用Exchange Web Services for Exchange 2013(API 2.1)。

旧代码使用ExchangeWebServices命名空间,我想在新的v2.1 API中使用Microsoft.Exchange.Webservices命名空间

新的Microsoft.Exchange.Webservices中没有定义旧ExchangeWebServices命名空间的引用,因此我需要重构代码以匹配新API中的等效项。

我需要做的一件事是为特定用户帐户创建收件箱中项目的所有邮件ID列表。在旧代码中,使用了它;

Private ExchangeBinding As New ExchangeServiceBinding
Public Sub New(ByVal UserName As String, ByVal Password As String, ByVal Domain As String, ByVal URL As String)
    ExchangeBinding.Credentials = New NetworkCredential(UserName, Password, Domain)
    ExchangeBinding.Url = URL
End Sub

Public Function GetInboxMessageIDs()As ArrayOfRealItemsType

    Dim returnInboxMessageIds As ArrayOfRealItemsType = Nothing
    Dim errMsg As String = String.Empty

    'Create the request and specify the travesal type.
    Dim FindItemRequest As FindItemType
    FindItemRequest = New FindItemType
    FindItemRequest.Traversal = ItemQueryTraversalType.Shallow

    'Define which item properties are returned in the response.
    Dim ItemProperties As ItemResponseShapeType
    ItemProperties = New ItemResponseShapeType
    ItemProperties.BaseShape = DefaultShapeNamesType.IdOnly

    'Add properties shape to the request.
    FindItemRequest.ItemShape = ItemProperties

    'Identify which folders to search to find items.
    Dim FolderIDArray(0) As DistinguishedFolderIdType
    FolderIDArray(0) = New DistinguishedFolderIdType
    FolderIDArray(0).Id = DistinguishedFolderIdNameType.inbox

    'Add folders to the request.
    FindItemRequest.ParentFolderIds = FolderIDArray

    Try
        'Send the request and get the response.
        Dim FindItemResponse As FindItemResponseType
        FindItemResponse = ExchangeBinding.FindItem(FindItemRequest)

        'Get the response messages.
        Dim ResponseMessage As ResponseMessageType()
        ResponseMessage = FindItemResponse.ResponseMessages.Items

        Dim FindItemResponseMessage As FindItemResponseMessageType

        If ResponseMessage(0).ResponseClass = ResponseClassType.Success Then

            FindItemResponseMessage = ResponseMessage(0)
            returnInboxMessageIds = FindItemResponseMessage.RootFolder.Item

        Else

            '' Server error
            Dim responseClassStr As String = [Enum].GetName(GetType(ExchangeWebServices.ResponseClassType), ResponseMessage(0).ResponseClass).ToString
            Dim responseCodeStr As String = [Enum].GetName(GetType(ExchangeWebServices.ResponseCodeType), ResponseMessage(0).ResponseCode).ToString
            Dim messageTextStr As String = ResponseMessage(0).MessageText.ToString
            Dim thisErrMsg As String = String.Format("ExchangeWebServices Inbox Error: {0}, {1}, {2}", responseClassStr, responseCodeStr, messageTextStr)
            errMsg = If(errMsg.Equals(String.Empty), String.Empty, errMsg & "; ") & thisErrMsg

        End If

    Catch ex As Exception
        errMsg = If(errMsg.Equals(String.Empty), String.Empty, errMsg & "; ") & ex.Message
    End Try

    If Not errMsg.Equals(String.Empty) Then
        returnInboxMessageIds = Nothing
        Throw New System.Exception(errMsg)
    End If

    Return returnInboxMessageIds

End Function

旧API命名空间中未在新API命名空间中引用的项目是;

-ExchangeServiceBinding
-ArrayOfRealItemsType
-FindItemType
-ItemQueryTraversalType
-ItemResponseShapeType
-DefaultShapeNamesType
-DistinguishedFolderIdType
-DistinguishedFolderIdNameType
-FindItemResponseType
-ResponseMessageType()
-FindItemResponseMessageType
-ResponseClassType

是否有人建议使用新的API代码来实现此结果?

1 个答案:

答案 0 :(得分:0)

您之前的应用程序中使用的API基于EWS生成的代理。您引用的新API是EWS托管API,它是EWS的包装,是推荐使用EWS的方法。

要回答有关在收件箱中获取每封邮件的邮件ID的第一个问题,请点击此链接指向'如何'应该帮助您入门的主题。它谈到批量购买物品,因为如果你像我一样,那么在任何给定时间收件箱中都会有很多消息。您可以设置要返回的项目数,但示例显示一次只执行五项。我知道它在C#中,但它应该很容易转换为VB。

Example: Perform a paged search by using the EWS Managed API

关于命名空间中缺少的项目。它们实际上并没有丢失,它们在托管API的开发过程中被重命名。以下是与旧代码中使用的内容相匹配的新主题的列表(包含链接):

有很多'如何' Develop web service clients for Exchange节点中 Exchange Online和Exchange 2013开发文档中的主题。这些重点关注EWS托管API,并提供带有SOAP的EWS示例。