我正在尝试使用 ews-java API 连接到我的收件箱并监听新电子邮件。
我似乎能够很好地连接,并且我正在从此处复制 github 上的示例代码:
// 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
很难解决这个问题,因为我找不到该库所有方法的手册 - 而且该示例不起作用。
完整代码为:
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)
最佳答案
有两件事:
1) 要从 WellKnownFolderName
创建 FolderId
,您必须使用 relevant constructor 。所以改变:
新的FolderId[] { WellKnownFolderName.Inbox }
到:
新FolderId[] { 新FolderId(WellKnownFolderName.Inbox) }
注意:new FolderId[] {..}
仅创建一个数组。然后数组中的每个项目都必须是 FolderId
类型,因此我们使用构造函数 new FolderId(...)
并将 WellKnownFolderName
传递为论证。
2) 您导入了错误的 EventType
(可能是 IDE 自动导入功能的错误),
因此请更改:
导入 javax.swing.event.DocumentEvent.EventType;
到:
导入microsoft.exchange.webservices.data.core.enumeration.notification.EventType;
关于java - 监听 Exchange 上的新电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39133847/