简体   繁体   中英

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 = "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 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

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("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 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM