OfficeDev / ews-java-api

A java client library to access Exchange web services. The API works against Office 365 Exchange Online as well as on premises Exchange.
MIT License
869 stars 560 forks source link

The server cannot service this request right now. Try again later. #644

Open lliaoliao opened 6 years ago

lliaoliao commented 6 years ago

package com.mtuskerbase.app.widget.mailExchange;

import java.net.URI; import java.util.ArrayList; import java.util.Iterator; import java.util.List;

import microsoft.exchange.webservices.data.core.ExchangeService; import microsoft.exchange.webservices.data.core.PropertySet; import microsoft.exchange.webservices.data.core.enumeration.misc.ExchangeVersion; import microsoft.exchange.webservices.data.core.enumeration.notification.EventType; import microsoft.exchange.webservices.data.core.enumeration.property.WellKnownFolderName; import microsoft.exchange.webservices.data.core.enumeration.service.DeleteMode; import microsoft.exchange.webservices.data.core.response.GetItemResponse; import microsoft.exchange.webservices.data.core.response.ServiceResponseCollection; import microsoft.exchange.webservices.data.core.service.item.EmailMessage; import microsoft.exchange.webservices.data.core.service.schema.ItemSchema; import microsoft.exchange.webservices.data.credential.ExchangeCredentials; import microsoft.exchange.webservices.data.credential.WebCredentials; import microsoft.exchange.webservices.data.notification.ItemEvent; import microsoft.exchange.webservices.data.notification.NotificationEvent; import microsoft.exchange.webservices.data.notification.NotificationEventArgs; import microsoft.exchange.webservices.data.notification.StreamingSubscription; import microsoft.exchange.webservices.data.notification.StreamingSubscriptionConnection; import microsoft.exchange.webservices.data.notification.StreamingSubscriptionConnection.INotificationEventDelegate; import microsoft.exchange.webservices.data.notification.StreamingSubscriptionConnection.ISubscriptionErrorDelegate; import microsoft.exchange.webservices.data.notification.SubscriptionErrorEventArgs; import microsoft.exchange.webservices.data.property.complex.FolderId; import microsoft.exchange.webservices.data.property.complex.ItemId; import microsoft.exchange.webservices.data.property.complex.MessageBody;

public class EmailRunner2 implements INotificationEventDelegate, ISubscriptionErrorDelegate{

public static ExchangeService exchangeService = new ExchangeService(ExchangeVersion.Exchange2010_SP2);
private static StreamingSubscriptionConnection conn;
public static StreamingSubscription subscription;

public static void main(String[] args) {

    EmailRunner2 runner = new EmailRunner2();
    runner.connect();
}

public void connect() {

    ExchangeCredentials credentials = new WebCredentials("***", "%**@s5!");
    exchangeService.setCredentials(credentials);
    try {
        exchangeService.setUrl(new URI("###"));
        WellKnownFolderName sd = WellKnownFolderName.Inbox;
        FolderId folderId = new FolderId(sd);

        ArrayList<FolderId> folder = new ArrayList<FolderId>();
        folder.add(folderId);

        subscription = exchangeService.subscribeToStreamingNotifications(folder, EventType.NewMail);

        conn = new StreamingSubscriptionConnection(exchangeService, 30);
        conn.addSubscription(subscription);
        conn.addOnNotificationEvent(this);
        conn.addOnDisconnect(this);
        conn.open();

        EmailMessage msg = new EmailMessage(exchangeService);
        msg.setSubject("Testing Streaming Notification on 16 Aug 2016");
        msg.setBody(MessageBody.getMessageBodyFromText("Streaming Notification "));
        msg.getToRecipients().add("gary.zhang@hatchsolution.com");
        msg.send();

        boolean loop = true;
        int i = 0;
        while (loop) {
            Thread.sleep(20000);
            msg.send();
            if (i++ > 100) {
                loop = false;
            }
        }
        System.out.println("end........");
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

@Override
public void subscriptionErrorDelegate(Object sender, SubscriptionErrorEventArgs args) {
    System.out.println("error...." + args.getException());

}

@Override
public void notificationEventDelegate(Object sender, NotificationEventArgs args) {
    System.out.println("Notification event....");
    // First retrieve the IDs of all the new emails
    List<ItemId> newMailsIds = new ArrayList<ItemId>();

    Iterator<NotificationEvent> it = args.getEvents().iterator();
    while (it.hasNext()) {
        ItemEvent itemEvent = (ItemEvent) it.next();
        if (itemEvent != null) {
            newMailsIds.add(itemEvent.getItemId());
        }
    }

    if (newMailsIds.size() > 0) {
        // Now retrieve the Subject property of all the new emails in one
        // call to EWS.
        ServiceResponseCollection<GetItemResponse> responses;
        try {
            responses = exchangeService.bindToItems(newMailsIds, new PropertySet(ItemSchema.Subject));

            System.out.println("count=======" + responses.getCount());

            for (GetItemResponse response : responses) {
                System.out.println("count=======" + responses.getClass().getName());
                System.out.println("subject=======" + response.getItem().getSubject());
                response.getItem().delete(DeleteMode.MoveToDeletedItems);
                // Console.WriteLine("subject====" + response.Item.Subject);
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

}

The server cannot service this request right now. Try again later. help!!!!

SelaO commented 6 years ago

Is it also really slow for you too?

OS-JaR commented 6 years ago

Hi, can you provide some more information pls? Did you have a stacktrace? What's the EWS response?

lliaoliao commented 6 years ago

@OS-JaR thank you response that. when i invoke conn.open() .

my stacktrace below microsoft.exchange.webservices.data.core.exception.service.remote.ServiceResponseException: The server cannot service this request right now. Try again later. at microsoft.exchange.webservices.data.core.request.ServiceRequestBase.processWebException(ServiceRequestBase.java:548) at microsoft.exchange.webservices.data.core.request.ServiceRequestBase.validateAndEmitRequest(ServiceRequestBase.java:641) at microsoft.exchange.webservices.data.core.request.HangingServiceRequestBase.internalExecute(HangingServiceRequestBase.java:175) at microsoft.exchange.webservices.data.notification.StreamingSubscriptionConnection.open(StreamingSubscriptionConnection.java:316) at com.mtuskerbase.app.widget.mailExchange.EmailRunner2.connect(EmailRunner2.java:67) at com.mtuskerbase.app.widget.mailExchange.aaaaa.main(aaaaa.java:9)

lliaoliao commented 6 years ago

@SelaO no ,my client can not open connection.

ravipatel01 commented 4 years ago

I am facing the same issue.