Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using EWS protocol for Java API

I am trying to send an email using EWS protocol.Code snippet used for the same :

private String username = "[email protected]";
private String password = "*****";

public void testMethod() throws Exception {

    ExchangeService service = new ExchangeService(
            ExchangeVersion.Exchange2010_SP2);
    ExchangeCredentials credentials = new WebCredentials(username, password);
    service.setTraceEnabled(true);
    service.setCredentials(credentials);

    try {
        service.setUrl(new URI("https://someurl/ews/exchange.asmx"));
    } catch (URISyntaxException e) {
        e.printStackTrace();
    }

    EmailMessage msg;
    try {
        msg = new EmailMessage(service);
        msg.setSubject("hello world");
        msg.setBody(MessageBody
                .getMessageBodyFromText("Sent using the EWS API"));
        msg.getToRecipients().add("[email protected]");
        msg.send();
    } catch (Exception e) {
        e.printStackTrace();
    }

When I execute the above code , I am getting exception as follows:

microsoft.exchange.webservices.data.core.exception.service.remote.ServiceRequestException: The request failed. The request failed. The remote server returned an error: (401)Unauthorized
at microsoft.exchange.webservices.data.core.request.SimpleServiceRequestBase.internalExecute(SimpleServiceRequestBase.java:74)
at microsoft.exchange.webservices.data.core.request.MultiResponseServiceRequest.execute(MultiResponseServiceRequest.java:158)
at 

How can I assure that I have rights to connect the URL? Can it be checked via cmd prompt? How shall I resolve this?

Also , if there is any domain which is using EWS protocol for testing pupose.I googled and came to know that gmail is not using it.Please also include some example for testing purpose.

Thanks

like image 255
test Avatar asked Mar 17 '26 14:03

test


1 Answers

I know its too late but nobody answered it yet so I am posting my answer.I have also faced similar problems earlier while trying to send Email using this API.

package testEWS;
import java.net.URI;
import java.net.URISyntaxException;
import microsoft.exchange.webservices.data.EmailMessage;
import microsoft.exchange.webservices.data.ExchangeCredentials;
import microsoft.exchange.webservices.data.ExchangeService;
import microsoft.exchange.webservices.data.ExchangeVersion;
import microsoft.exchange.webservices.data.MessageBody;
import microsoft.exchange.webservices.data.WebCredentials;


public class Sendmail {

                public static void main(String[] args) throws Exception {
                                testMethod();
                                System.out.println("mail sent.. have fun");

                }


                public static  void testMethod() throws Exception {


                          ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP2);
                        ExchangeCredentials credentials = new WebCredentials("[email protected] ", "Password");
                         service.setCredentials(credentials);

                    try {
                        service.setUrl(new URI("https://myexchange.XXXX.com/EWS/Exchange.asmx"));
                    } catch (URISyntaxException e) {
                        e.printStackTrace();
                    }

                    EmailMessage msg;
                    try {
                       msg = new EmailMessage(service);
                        msg.setSubject("hello world");
                        msg.setBody(MessageBody.getMessageBodyFromText("Sent using the EWS API"));
                        msg.getToRecipients().add("[email protected]");
                        msg.send();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    System.out.println("Hello");
}
}

If you want to try it locally, you can try with Microsoft outlook. Outlook also uses EWS API. To get the endpoint from outlook follow this link: http://blogs.msdn.com/b/deva/archive/2011/12/02/how-to-get-the-ews-endpoint-url-from-outlook-2007-2010.aspx

like image 194
Eagle Avatar answered Mar 19 '26 05:03

Eagle



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!