Using the EWS protocol for the Java API

I am trying to send an email using the EWS

protocol.Code of the snippet used for this:

private String username = "xxx@xxx.com";
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("test@test.com");
        msg.send();
    } catch (Exception e) {
        e.printStackTrace();
    }

      

When I execute the above code, I get an exception like this:

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 permission to connect the URL? Can I check using the command line? How can I solve this?

Also, if there is any domain that uses the EWS protocol to test pupose.I googled and found out that gmail is not using it. Please also include an example for testing purposes.

thank

+3


source to share


1 answer


I know it too late, but nobody answered it, so I am posting my answer. I also faced similar issues 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("milidFromWhichMailToBeSent@Host.com ", "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("testMail@hOST.com");
                        msg.send();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    System.out.println("Hello");
}
}

      



If you want to try it locally, you can try with Outlook. Outlook also uses the 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

+1


source







All Articles