1

I am using EWS to send emails on Exchange 2010 as follows:

ExchangeService service = new ExchangeService();
ExchangeCredentials credentials = new WebCredentials(email, password);
service.setCredentials(credentials);
service.setUrl(new java.net.URI("https://" + host
    + "/EWS/Exchange.asmx"));
service.setTraceEnabled(true);

EmailMessage msg = new EmailMessage(service);
msg.setSubject("Hello world!");
msg.setBody(MessageBody.getMessageBodyFromText("Sent using the EWS Managed API."));
msg.getToRecipients().add("email");
msg.send();

The message is not delivered to the inbox and I don't know what is the problem. After I enabled tracing, I get the following:

<Trace Tag="EwsRequestHttpHeaders" Tid="1" Time="2012-10-14 11:13:46Z">
    POST /EWS/Exchange.asmx HTTP/1.1
    Content-type : text/xml; charset=utf-8
    Accept-Encoding : gzip,deflate
    Keep-Alive : 300
    User-Agent : ExchangeServicesClient/0.0.0.0
    Connection : Keep-Alive
    Accept : text/xml
</Trace>

<Trace Tag="EwsRequest" Tid="1" Time="2012-10-14 11:13:47Z">
    <?xml version="1.0" encoding="utf-8"?>
    <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages" xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types">
    <soap:Header><t:RequestServerVersion Version="Exchange2010_SP1"></t:RequestServerVersion></soap:Header>
    <soap:Body><m:CreateItem MessageDisposition="SendOnly"><m:Items><t:Message><t:Subject>Hello world!</t:Subject><t:Body BodyType="HTML">Sent using the EWS Managed API.</t:Body><t:ToRecipients><t:Mailbox><t:EmailAddress>adamb@fabrikam.com</t:EmailAddress></t:Mailbox></t:ToRecipients></t:Message></m:Items></m:CreateItem></soap:Body>
    </soap:Envelope>
</Trace>

14/10/2012 01:13:48 م org.apache.commons.httpclient.auth.AuthChallengeProcessor selectAuthScheme
    INFO: NTLM authentication scheme selected
    <Trace Tag="EwsResponseHttpHeaders" Tid="1" Time="2012-10-14 11:13:48Z">
    200 text/xml; charset=utf-8
    X-AspNet-Version : 2.0.50727
    X-EwsPerformanceData : RpcC=3;RpcL=15;LdapC=0;LdapL=0;
    Persistent-Auth : true
    Date : Sun, 14 Oct 2012 11:13:48 GMT
    Vary : Accept-Encoding
    Transfer-Encoding : chunked
    Content-Encoding : gzip
    Content-Type : text/xml; charset=utf-8
    X-Powered-By : ASP.NET
    Server : Microsoft-IIS/7.5
    Cache-Control : private
</Trace>

<Trace Tag="EwsResponse" Tid="1" Time="2012-10-14 11:13:48Z">
    <?xml version="1.0" encoding="utf-8"?>
    <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
    <s:Header><h:ServerVersionInfo MajorVersion="14" MinorVersion="1" MajorBuildNumber="218" MinorBuildNumber="14" Version="Exchange2010_SP1" xmlns:h="http://schemas.microsoft.com/exchange/services/2006/types" xmlns="http://schemas.microsoft.com/exchange/services/2006/types" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"/></s:Header>
    <s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><m:CreateItemResponse xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages" xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types"><m:ResponseMessages><m:CreateItemResponseMessage ResponseClass="Success"><m:ResponseCode>NoError</m:ResponseCode><m:Items/></m:CreateItemResponseMessage></m:ResponseMessages></m:CreateItemResponse></s:Body>
    </s:Envelope>
</Trace>

Please advise how to solve this issue.

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
Mahmoud Saleh
  • 33,303
  • 119
  • 337
  • 498
  • I have also enabled tracing service.setTraceEnabled(true)- I cant see trace logs from exchange. - How and where I can see tarce logs to java ews api ? Code snippet will be helpful – StackOverFlow Oct 03 '18 at 06:50

2 Answers2

0

The exchange server is responding that all is ok:

<?xml version="1.0" encoding="utf-8"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
  <s:Header>
    <h:ServerVersionInfo MajorVersion="14" MinorVersion="1" MajorBuildNumber="218" MinorBuildNumber="14" Version="Exchange2010_SP1" xmlns:h="http://schemas.microsoft.com/exchange/services/2006/types" xmlns="http://schemas.microsoft.com/exchange/services/2006/types" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"/>
  </s:Header>
  <s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <m:CreateItemResponse xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages" xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types">
      <m:ResponseMessages>
        <m:CreateItemResponseMessage ResponseClass="Success">
          <m:ResponseCode>NoError</m:ResponseCode>
          <m:Items/>
        </m:CreateItemResponseMessage>
      </m:ResponseMessages>
    </m:CreateItemResponse>
  </s:Body>
</s:Envelope>

You should check on the Exchange side, or ask your Exchange server admin about what's going on (for example some spam filtering or whatever else), because from a code perspective and SOAP Request/Response point of view, there's nothing wrong.

Alex
  • 25,147
  • 6
  • 59
  • 55
0
          <t:Message>
            <t:Subject>Hello world!</t:Subject>
            <t:Body BodyType="HTML">Sent using the EWS Managed API.</t:Body>
            <t:ToRecipients>
              <t:Mailbox>
                <t:EmailAddress>adamb@fabrikam.com</t:EmailAddress>
              </t:Mailbox>
            </t:ToRecipients>
          </t:Message>

Please check the inbox of adamb@fabrikam.com or talk to an Exchange administrator to look at the mail queues, filters, many other things etc. that might stop the email from coming. Nothing wrong with your posted code.

sainiuc
  • 1,697
  • 11
  • 13