我正在尝试使用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);


但这是蚀中的错误:

 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
    }


谢谢。

更新

谢谢,但是我仍然遇到错误:

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


带红色下划线的subscribeToPushNotifications,IDE表示:

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

最佳答案

两件事情:

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;

关于java - 收听交换时的新电子邮件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39133847/

10-08 23:45