如何使用带有ColdFusion的EWS托管API搜索收件箱?

时间:2017-04-28 21:23:44

标签: java coldfusion exchange-server exchangewebservices

我们正在使用ColdFusion 2016并使用EWS API连接到Exchange。我已设法从以下帖子中获取代码示例,但无法弄清楚如何将SearchFilter与findItems()方法一起使用。 Why is the get action of cfexchangemail never returning?

service = createObject("Java", "microsoft.exchange.webservices.data.ExchangeService");
service.init();

version = createObject("Java", "microsoft.exchange.webservices.data.ExchangeVersion");
service.init(version.Exchange2010);

credentials = createObject("Java", "microsoft.exchange.webservices.data.WebCredentials");

credentials.init(yourusername, yourpassword);
service.setCredentials(credentials);

uri = createObject("Java", "java.net.URI");
uri.init("outlook webservices url");
service.setUrl(uri);
WellKnownFolderName=createObject("Java","microsoft.exchange.webservices.data.WellKnownFolderName");
result = service.FindItems(service.WellKnownFolderName.Inbox, CreateObject("java", "microsoft.exchange.webservices.data.ItemView").init(100));

for(item in result.getItems(){
    // ..loop through each field and store their value 
    // in query object or something...
}

所以基本上可以将这个Java代码翻译成Coldfusion吗?

ItemView view = new ItemView(10);
view.getOrderBy().add(ItemSchema.DateTimeReceived, SortDirection.Ascending);
view.setPropertySet(new PropertySet(BasePropertySet.IdOnly, ItemSchema.Subject, ItemSchema.DateTimeReceived));

FindItemsResults<Item> findResults =
    service.findItems(WellKnownFolderName.Inbox,
        new SearchFilter.SearchFilterCollection(
            LogicalOperator.Or, new SearchFilter.ContainsSubstring(ItemSchema.Subject, "EWS"),
        new SearchFilter.ContainsSubstring(ItemSchema.Subject, "API")), view);

service.loadPropertiesForItems(findResults, PropertySet.FirstClassProperties);
System.out.println("Total number of items found: " + findResults.getTotalCount());

for (Item item : findResults) {
    System.out.println(item.getSubject());
    System.out.println(item.getBody());
    // Do something with the item.
}

从评论中更新:

我无法弄清楚如何实例化SearchFilterSearchFilterCollection。我尝试创建一个searchFilter对象,但它没有可以使用的属性。我一定是要错过一些东西。

CreateObject("java","microsoft.exchange.webservices.data.Sea‌​rchFilter");

2 个答案:

答案 0 :(得分:2)

最终的工作结果是:

folderName = createObject("java", "microsoft.exchange.webservices.data.WellKnownFolderName");
itemView = CreateObject("java","microsoft.exchange.webservices.data.ItemView").init(10,0);
folderID = CreateObject("java","microsoft.exchange.webservices.data.FolderId").init(
        folderName.Inbox,
        mailBox
        );
sortDirection = CreateObject("java","microsoft.exchange.webservices.data.SortDirection");
basePropertySet = createObject("java", "microsoft.exchange.webservices.data.BasePropertySet");
propertyset = createObject("java", "microsoft.exchange.webservices.data.PropertySet");
itemSchema = createObject("java", "microsoft.exchange.webservices.data.ItemSchema");
searchFilterCollection = CreateObject("java","microsoft.exchange.webservices.data.SearchFilter$SearchFilterCollection");
logicalOperator = createObject("java", "microsoft.exchange.webservices.data.LogicalOperator");
subString1 = CreateObject("java","microsoft.exchange.webservices.data.SearchFilter$ContainsSubstring").init(
        itemSchema.Subject,
        "EWS"
        );
subString2 = CreateObject("java","microsoft.exchange.webservices.data.SearchFilter$ContainsSubstring").init(
        itemSchema.Subject,
        "API"
        );
searchFilterCollection.add(SubString1);
searchFilterCollection.add(SubString2);
searchFilterCollection.setLogicalOperator(LogicalOperator.Or);
bodyType = createObject("java", "microsoft.exchange.webservices.data.BodyType");
propertyset.add(itemSchema.Subject);
propertyset.add(itemSchema.DateTimeReceived);
propertyset.setBasePropertySet(basePropertySet.IdOnly);
propertyset.setRequestedBodyType(bodyType.HTML);
itemView.getOrderBy().add(itemSchema.DateTimeReceived, sortDirection.Ascending);
itemView.setPropertySet(propertyset);
viewResults = service.findItems(folderID, searchFilterCollection, itemView);

答案 1 :(得分:1)

(不是完整答案,但评论时间太长)

难怪......这是一个很多的嵌套。我没有安装lib,但如果是我的话,我首先将其分解为单独的语句,每行一个new个对象。从最里面的构造函数开始:

new SearchFilter.ContainsSubstring(
          ItemSchema.Subject
        , "API"
    )

您需要先创建ItemSchema的实例,才能访问Subject属性。查看C# docs(没有可用的javadoc),路径应该是这样的:

ItemSchema = createObject("java", "microsoft.exchange.webservices.data.ItemSchema");

之后继续创建SearchFilter。注意班级名称中的句点.?关键字new后跟一个包含句点的类名,通常表示它是inner class。内部类需要特殊的语法,即ParentClass$InnerClass。这样的事情应该有效:

APISubString = CreateObject("java","microsoft.exchange.webservices.data.SearchFilter$ContainsSubstring");
writeDump(APISubString );

如果有效,请添加init()以初始化对象:

  APISubString = CreateObject("java","microsoft.exchange.webservices.data.SearchFilter.ContainsSubstring").init(ItemSchema.Subject, "API");

然后移动到下一个最里面的对象:

new SearchFilter.ContainsSubstring(
           ItemSchema.Subject
          , "EWS"
    )

并起泡,冲洗,重复剩余的物体:

// ItemSchema.Subject is a static value and can be reused
EWSSubString = CreateObject("java","microsoft.exchange.webservices.data.SearchFilter.ContainsSubstring").init(ItemSchema.Subject, "EWS");