2016-08-24 79 views
0

我正尝试使用ews-java API连接到我的收件箱并收听新电子邮件。在交换机上收听新电子邮件

我似乎能够连接蛮好的,我从例子在github这里复制代码:

https://github.com/OfficeDev/ews-java-api/wiki/Getting-Started-Guide#beginsubscribetopushnotifications

// Subscribe to push notifications on the Inbox folder, and only listen 
// to "new mail" events. 
PushSubscription pushSubscription = service.SubscribeToPushNotifications(
    new FolderId[] { WellKnownFolderName.Inbox }, 
    new Uri("https://...") /* The endpoint of the listener. */, 
    5 /* Get a status event every 5 minutes if no new events are available. */, 
    null /* watermark: null to start a new subscription. */, 
    EventType.NewMail); 

然而,这是Eclipse中的错误:

new FolderId[] { WellKnownFolderName.Inbox }, // <---TYPE MISMATCH - CANNOT CONVERT FRM 
WELLKNOWNFOLDERNAME TO FOLDERID 

还有

EventType.NewMail); // <---- NEWMAIL CANNOT BE RESOLVED OR IS NOT A FIELD 

它很难解决这个问题,因为我无法找到关于这个lib的所有方法的手册 - 而且这个例子不起作用。

完整的代码是:

package com.geekhelp.quickstart; 

import javax.swing.event.DocumentEvent.EventType; 

import microsoft.exchange.webservices.data.autodiscover.IAutodiscoverRedirectionUrl; 
import microsoft.exchange.webservices.data.core.ExchangeService; 
import microsoft.exchange.webservices.data.core.enumeration.misc.ExchangeVersion; 
import microsoft.exchange.webservices.data.core.enumeration.property.WellKnownFolderName; 
import microsoft.exchange.webservices.data.core.service.item.EmailMessage; 
import microsoft.exchange.webservices.data.core.service.item.Item; 
import microsoft.exchange.webservices.data.credential.ExchangeCredentials; 
import microsoft.exchange.webservices.data.credential.WebCredentials; 
import microsoft.exchange.webservices.data.notification.PushSubscription; 
import microsoft.exchange.webservices.data.property.complex.FolderId; 
import microsoft.exchange.webservices.data.property.complex.MessageBody; 
import microsoft.exchange.webservices.data.search.FindItemsResults; 
import microsoft.exchange.webservices.data.search.ItemView; 

public class App { 
    public static void main(String[] args) { 
     System.out.println("Running"); 
     ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP2); 
     ExchangeCredentials credentials = new WebCredentials("[email protected]", "test"); 
     service.setCredentials(credentials); 
     try { 
      service.autodiscoverUrl("[email protected]"); 
     } catch (Exception e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     System.out.println("Hello World"); 

     EmailMessage message; 
     try { 
      message = new EmailMessage(service); 

      message.getToRecipients().add("[email protected]"); 
      message.setSubject("attachements"); 
      message.setBody(MessageBody.getMessageBodyFromText("Email attachements")); 
      message.send(); 
     } catch (Exception e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 


     // Subscribe to push notifications on the Inbox folder, and only listen 
     // to "new mail" events. 
     PushSubscription pushSubscription = service.SubscribeToPushNotifications(
      new FolderId[] { WellKnownFolderName.Inbox }, // <------------ TYPE MISMATCH - CANNOT CONVERT FRM 
WELLKNOWNFOLDERNAME TO FOLDERID 
      new java.net.URI("https://mail.test.com//EWS//Exchange.asmx") /* The endpoint of the listener. */, 
      5 /* Get a status event every 5 minutes if no new events are available. */, 
      null /* watermark: null to start a new subscription. */, 
      EventType.NewMail); // <----------- NEWMAIL CANNOT BE RESOLVED OR IS NOT A FIELD 
    } 

感谢。

UPDATE

谢谢,但我仍然得到一个错误:

FolderId[] folderId = { new FolderId(WellKnownFolderName.Inbox)}; 
PushSubscription pushSubscription = service.subscribeToPushNotifications(folderId , service.getUrl(), 5, null, EventType.NewMail); 

subscribeToPushNotifications的红色下划线和IDE说:

的方法subscribeToPushNotifications(可迭代,URI,INT,字符串,EventType ...)在ExchangeService类型中不适用于参数(FolderId [],URI,int,null,EventType)

回答

1

Tw东西:

1)要创建FolderIdWellKnownFolderName,您必须使用relevant constructor。所以改变: new FolderId[] { WellKnownFolderName.Inbox }到:

new FolderId[] { new FolderId(WellKnownFolderName.Inbox) }

注:new FolderId[] {..}只创建一个数组。然后数组中的每个项目必须是FolderId类型,所以我们使用构造函数new FolderId(...)并传递WellKnownFolderName作为参数。

2)要导入的错误EventType(可能是在IDE的功能AUTOIMPORT故障),
因此改变: import javax.swing.event.DocumentEvent.EventType;到:

import microsoft.exchange.webservices.data.core.enumeration.notification.EventType;