2

I want to send an email with Java and Microsoft exchange server API. I followed the instruction from the documents, but I always get an exception.

All parameter (username, password, domain and uri are correct). I have tested this parameter with EWS Editor from Microsoft, and with the editor I get a connection.

My code is this:

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 Mail {

ExchangeService service = new ExchangeService(
        ExchangeVersion.Exchange2010_SP2);

ExchangeCredentials credentials = new WebCredentials("user",
        "passwort", "domain");

public void sendMail() throws Exception {

    service.setTraceEnabled(true);
    java.net.URI uri = new java.net.URI(
            "https://********/ews/exchange.asmx");

    service.setCredentials(credentials);
    service.setUrl(uri);

    System.out.println(service.getServerInfo());

    EmailMessage msg = new EmailMessage(service);
    msg.setBody(MessageBody
            .getMessageBodyFromText("Sent using the EWS Managed API."));
    msg.getToRecipients().add("send mail to ");
    msg.send();
}

public static void main(String[] args) throws Exception {
    Mail mail = new Mail();
    mail.sendMail();
}

The exception is this:

Exception in thread "main" microsoft.exchange.webservices.data.EWSHttpException: Connection not established
at microsoft.exchange.webservices.data.HttpClientWebRequest.throwIfConnIsNull(HttpClientWebRequest.java:394)
at microsoft.exchange.webservices.data.HttpClientWebRequest.getResponseCode(HttpClientWebRequest.java:370)
at microsoft.exchange.webservices.data.EwsUtilities.formatHttpResponseHeaders(EwsUtilities.java:597)
at microsoft.exchange.webservices.data.ExchangeServiceBase.traceHttpResponseHeaders(ExchangeServiceBase.java:467)
at microsoft.exchange.webservices.data.ExchangeServiceBase.processHttpResponseHeaders(ExchangeServiceBase.java:1043)
at microsoft.exchange.webservices.data.SimpleServiceRequestBase.internalExecute(SimpleServiceRequestBase.java:58)
at microsoft.exchange.webservices.data.MultiResponseServiceRequest.execute(MultiResponseServiceRequest.java:144)
at microsoft.exchange.webservices.data.ExchangeService.internalCreateItems(ExchangeService.java:464)
at microsoft.exchange.webservices.data.ExchangeService.createItem(ExchangeService.java:535)
at microsoft.exchange.webservices.data.Item.internalCreate(Item.java:215)
at microsoft.exchange.webservices.data.EmailMessage.internalSend(EmailMessage.java:125)
at microsoft.exchange.webservices.data.EmailMessage.send(EmailMessage.java:253)
at de.ruv.SendMail.Mail.sendMail(Mail.java:40)
at de.ruv.SendMail.Mail.main(Mail.java:45)
Jota
  • 17,281
  • 7
  • 63
  • 93
java java
  • 405
  • 1
  • 11
  • 25
  • what aboout this? http://stackoverflow.com/questions/14562153/could-not-establish-the-connection-to-the-exchange-web-service-java-api – igo Jun 10 '13 at 08:39

1 Answers1

0

Change

ExchangeCredentials credentials = new WebCredentials("user", "password", "domain");

to

service.Credentials = new NetworkCredential("user", "password", "domain");
Henrik Aasted Sørensen
  • 6,966
  • 11
  • 51
  • 60
Dan W
  • 89
  • 1
  • 6