Doug Greaney
Doug Greaney

Reputation: 181

listen for new email on exchange

I am trying to use the ews-java API to connect to my inbox and listen for new emails.

I seem to be able to connect just fine, and I am copying code from the examples on github here:

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);

However this is the error in eclipse:

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

And also

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

Its hard to sort this out because I cant find a manual on all the methods for this lib - and the example doesn't work.

The full code is:

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
    }

Thanks.

UPDATE

Thanks, but I am still getting a error:

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

subscribeToPushNotifications is underlined red, and the IDE says:

The method subscribeToPushNotifications(Iterable, URI, int, String, EventType...) in the type ExchangeService is not applicable for the arguments (FolderId[], URI, int, null, EventType)

Upvotes: 1

Views: 2309

Answers (2)

Cameron C.
Cameron C.

Reputation: 1

Late response, but looking at your updated issue, apparently, arrays do not count as Iterable objects. Lists, however, do count as Iterable objects. All you need do is convert your Array into a List using the Arrays.asList() method, providing your array as a parameter.

Upvotes: 0

Usman Shahid Amin
Usman Shahid Amin

Reputation: 261

Two things:

1) To create FolderId from WellKnownFolderName, you have to use the relevant constructor. So change: new FolderId[] { WellKnownFolderName.Inbox } to:

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

Note: new FolderId[] {..} only creates an array. Then each item in the array has to be of FolderId type so we use the constructor new FolderId(...) and pass the WellKnownFolderName as argument.

2) You are importing the wrong EventType (possibly the fault of an autoimport feature of the IDE),
So change: import javax.swing.event.DocumentEvent.EventType; to:

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

Upvotes: 2

Related Questions