简体   繁体   English

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

[英]How do I use Exchange Web Services to get a list of message IDs from an Exchange Online 2013 inbox

I am converting some code that was written by a former employee using the Exchange Web Services API from Exchange 2007. When it was written, we had an on-premise Exchange 2007 Server. 我正在转换一些以前的员工使用Exchange 2007中的Exchange Web Services API编写的代码。编写这些代码时,我们有一个内部Exchange 2007 Server。 Now we have moved our email to Exchange Online and I want to use the Exchange Web Services for Exchange 2013 (API 2.1). 现在,我们已将电子邮件移至Exchange Online,我想将Exchange Web Services用于Exchange 2013(API 2.1)。

The old code used the ExchangeWebServices namespace and I want to use the Microsoft.Exchange.Webservices namespace in the new v2.1 API 旧代码使用了ExchangeWebServices命名空间,而我想在新的v2.1 API中使用Microsoft.Exchange.Webservices命名空间

None of the references from the old ExchangeWebServices namespace are defined in the new Microsoft.Exchange.Webservices so I need to refactor the code to match equivalent items in the new API. 在新的Microsoft.Exchange.Webservices中,没有定义来自旧ExchangeWebServices命名空间的引用,因此我需要重构代码以匹配新API中的等效项。

One of the things I need to do is create a list of all of the Message IDs of the items in the Inbox for a specific user account. 我需要做的一件事情是在收件箱中为特定用户帐户创建所有邮件ID的列表。 In the old code, this was used; 在旧代码中,使用了它。

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 公共函数GetInboxMessageIDs()为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

The items in the old API namespace that are not referenced in the new API namespace are; 新API名称空间中未引用的旧API名称空间中的项目是:

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

Does anyone have any recommendations for achieving this result using the new API code? 有没有人建议使用新的API代码达到此结果?

The API that was used in your earlier application was based off the generated proxy from EWS. 早期应用程序中使用的API基于EWS生成的代理。 The new API you are referring to is the EWS Managed API which is a wrapper around EWS and is the recommended way to use EWS. 您要使用的新API是EWS托管API,它是EWS的包装,并且是使用EWS的推荐方法。

To answer your first question about getting the message id for each message in the Inbox, here is a link to a 'How to' topic that should help you get started. 要回答有关获取“收件箱”中每封邮件的邮件ID的第一个问题,这里是指向“如何”主题的链接,该主题应该可以帮助您入门。 It talks about getting items in batches because if you are like me then there are a lot of messages in the Inbox at any given time. 它谈论批量获取物品,因为如果您像我一样,那么在任何给定时间收件箱中都会有很多消息。 You can set the number of items to return, but the example shows doing only five at a time. 您可以设置要返回的项目数,但是该示例显示一次仅执行五项。 And I know it's in C#, but it should easily convert to VB. 而且我知道它在C#中,但是应该可以轻松转换为VB。

Example: Perform a paged search by using the EWS Managed API 示例:使用EWS托管API执行分页搜索

Regarding items in the namespace that are missing. 关于名称空间中缺少的项目。 They aren't actually missing, they have been renamed during the development of the managed API. 它们实际上并没有丢失,它们在托管API的开发过程中已被重命名。 Here is the list (with links) to the new topics that match what you were using in the older code: 以下是与您在旧代码中使用的主题相匹配的新主题的列表(带有链接):

There are a lot of 'How to' topics in the Exchange Online and Exchange 2013 development documentation in the Develop web service clients for Exchange node. 开发ExchangeWeb服务客户端”节点中的Exchange Online和Exchange 2013开发文档中有很多“如何做”主题。 These focus on EWS Managed API and also give examples of EWS with SOAP. 这些关注于EWS托管API,还提供了带SOAP的EWS示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM