在交换时收听新电子邮件

时间:2016-08-24 22:16:19

标签: java exchangewebservices ewsjavaapi

我正在尝试使用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("test@test.com0", "test");
        service.setCredentials(credentials);
        try {
            service.autodiscoverUrl("test@test.com");
        } 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("test@test.com");
            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
    }

感谢。

更新

谢谢,但我仍然收到错误:

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

subscribeToPushNotifications下划线为红色,IDE说:

ExchangeService类型中的方法subscribeToPushNotifications(Iterable,URI,int,String,EventType ...)不适用于参数(FolderId [],URI,int,null,EventType)

2 个答案:

答案 0 :(得分:2)

两件事:

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

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

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

2)您导入了错误的EventType(可能是IDE的自动导入功能的错误),所以更改: import javax.swing.event.DocumentEvent.EventType;来:

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

答案 1 :(得分:0)

迟来的响应,但是显然,更新的问题并不将数组视为Iterable对象。但是,列表 do 被视为可迭代对象。您需要做的就是使用Arrays.asList()方法将数组转换为列表,并提供数组作为参数。