OfficeDev / ews-java-api

A java client library to access Exchange web services. The API works against Office 365 Exchange Online as well as on premises Exchange.
MIT License
869 stars 560 forks source link

Exception when calling "send()" method #318

Open themartin78 opened 9 years ago

themartin78 commented 9 years ago

Dear all,

I currently encounter problems when calling "send()" on a message. Although the message properties (recipients, attachments, etc.) have been properly set, I always get an exception when calling send:

 microsoft.exchange.webservices.data.exception.ServiceRequestException: The request failed. null
at microsoft.exchange.webservices.data.core.request.SimpleServiceRequestBase.internalExecute(SimpleServiceRequestBase.java:80)
at microsoft.exchange.webservices.data.core.request.MultiResponseServiceRequest.execute(MultiResponseServiceRequest.java:158)
at microsoft.exchange.webservices.data.core.ExchangeService.internalCreateItems(ExchangeService.java:594)
at microsoft.exchange.webservices.data.core.ExchangeService.createItem(ExchangeService.java:653)
at microsoft.exchange.webservices.data.core.service.item.Item.internalCreate(Item.java:247)
at microsoft.exchange.webservices.data.core.service.item.EmailMessage.internalSend(EmailMessage.java:147)
at microsoft.exchange.webservices.data.core.service.item.EmailMessage.send(EmailMessage.java:258)
at Serienmailer$1.run(Serienmailer.java:106)
at java.lang.Thread.run(Unknown Source)

Caused by: java.lang.NullPointerException
at microsoft.exchange.webservices.data.core.ExchangeServerInfo.parse(ExchangeServerInfo.java:80)
at microsoft.exchange.webservices.data.core.request.ServiceRequestBase.readSoapHeader(ServiceRequestBase.java:530)
at microsoft.exchange.webservices.data.core.request.ServiceRequestBase.readResponse(ServiceRequestBase.java:485)
at microsoft.exchange.webservices.data.core.request.ServiceRequestBase.readResponse(ServiceRequestBase.java:453)
at microsoft.exchange.webservices.data.core.request.SimpleServiceRequestBase.internalExecute(SimpleServiceRequestBase.java:69)
... 8 more

What is really strange is that the message does get sent despite the exception. That is, unless I add file attachments to the message. Then the message gets stuck in my drafts folder and nothing else happens.

Any idea on that I might be doing wrong or how I could tackle the problem?

Best Martin

serious6 commented 9 years ago

@themartin78 can you provide a small code example that triggers the exception? Are you using the latest snapshot version?

themartin78 commented 9 years ago

Yes, I am using the latest version available.

I'm afraid there's nothing really special about my code though:

try
{       
  EmailMessage msg;     
  msg = new EmailMessage(service);
  msg.setFrom(new EmailAddress(mailSender));
  msg.setSubject(mailSubject);

  msg.setBody(MessageBody.getMessageBodyFromText(mailText));
                            BodyType type = BodyType.Text;
                            msg.getBody().setBodyType(type);

for(int i=0;i<attachments.length;i++)
  if(!attachments[i].equals(""))
    msg.getAttachments().addFileAttachment(attachments[i]);

msg.getToRecipients().add(mailAdressen[recipient]);

msg.send();
Thread.sleep(timetowait);

}
catch (Exception e)
{
  e.printStackTrace();
}

This slice of code is embedded in a for-loop which increases 'recipient' in each iteration. The variable values are all valid.

Best Martin

serious6 commented 9 years ago

I am a little bit concerned about that Thread.sleep stuff. Are you doing anything concurrent here?

themartin78 commented 9 years ago

Well, the piece of software I'm writing is a kind of mass mailer intended for governmental use. The delay caused by Thread.sleep() prevents messages from being incorrectly classified as spam by the receiving server. Nothing concurrent going on here.

What's the exact problem you see with using Thread.sleep?

themartin78 commented 9 years ago

Just commented out the Thread.sleep stuff. Still no success though.

serious6 commented 9 years ago

currently didnt have time to reproduce. Did you do any debugging on it with evaluation of the possible cause? Does this exception occur for every E-Mail will it be thrown after some of them where send successfully?

themartin78 commented 9 years ago

Did you do any debugging on it with evaluation of the possible cause?

Tried to but posted here since I was unable to identify the cause.

Does this exception occur for every E-Mail?

Yes.

Will it be thrown after some of them where send successfully?

Yes, it gets thrown for those as well. But sending itself does succeed for any email without attachments.

serious6 commented 9 years ago

hmm works with my server. Maybe enable trace to look at the apis-request for further debugging.

themartin78 commented 9 years ago

Ok, I now added "service.setTraceEnabled(true)".

What do I do next (i.e. to look at the API's request)?

serious6 commented 9 years ago

@themartin78 I created a wiki-page for this.

themartin78 commented 9 years ago

Here's what I get trying to send a message with an attachment. Note that the output is mixed with the exception output because I did all logging via console.

Please note that I edited out any domain-/url-related information to keep that information private.

Type:EwsRequestHttpHeaders Message:<Trace Tag="EwsRequestHttpHeaders" Tid="34" Time="2015-05-22 07:44:34Z">
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>

Type:EwsRequest Message:<Trace Tag="EwsRequest" Tid="34" Time="2015-05-22 07:44:34Z">
<?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_SP2"></t:RequestServerVersion></soap:Header><soap:Body><m:CreateItem MessageDisposition="SaveOnly"><m:Items><t:Message><t:Subject>Hallo</t:Subject><t:Body BodyType="Text">Super</t:Body><t:ToRecipients><t:Mailbox><t:EmailAddress>XYZ@XYZ.de</t:EmailAddress></t:Mailbox></t:ToRecipients><t:From><t:Mailbox><t:EmailAddress>XYZ@XYZ.de</t:EmailAddress></t:Mailbox></t:From></t:Message></m:Items></m:CreateItem></soap:Body></soap:Envelope>
</Trace>

Type:EwsResponseHttpHeaders Message:<Trace Tag="EwsResponseHttpHeaders" Tid="34" Time="2015-05-22 07:44:35Z">
200 text/xml; charset=utf-8
X-BEServer : EXCHMBX2
Persistent-Auth : true
request-id : 745b3f49-4768-482a-b3e4-e22893dc92b5
Set-Cookie : exchangecookie=a939f04b1c174e9cb35910c4977c335e; expires=Sun, 22-May-2016 07:44:35 GMT; path=/; HttpOnly,X-BackEndCookie=S-1-5-21-4148307-1437163908-1275988791-16652=u56Lnp2ejJqBxsuZmsnJyZvSms/LztLLmsrP0p7NmprSycvJzMeamsbMzc6bgYHNz87K0s/J0s3Oq8/IxcvLxczK; expires=Sun, 21-Jun-2015 05:44:35 GMT; path=/EWS; secure; HttpOnly
X-Powered-By : ASP.NET
Server : Microsoft-IIS/8.5
Cache-Control : private
X-AspNet-Version : 4.0.30319
X-FEServer : EXCHCAS1
Date : Fri, 22 May 2015 07:44:35 GMT
Transfer-Encoding : chunked
Vary : Accept-Encoding
X-CalculatedBETarget : ABC.de
X-DiagInfo : EXCHMBX2
Content-Type : text/xml; charset=utf-8

</Trace>

Type:EwsResponseHttpHeaders Message:<Trace Tag="EwsResponseHttpHeaders" Tid="34" Time="2015-05-22 07:44:36Z">
200 text/xml; charset=utf-8
X-BEServer : EXCHMBX2
Persistent-Auth : true
request-id : 745b3f49-4768-482a-b3e4-e22893dc92b5
Set-Cookie : exchangecookie=a939f04b1c174e9cb35910c4977c335e; expires=Sun, 22-May-2016 07:44:35 GMT; path=/; HttpOnly,X-BackEndCookie=S-1-5-21-4148307-1437163908-1275988791-16652=u56Lnp2ejJqBxsuZmsnJyZvSms/LztLLmsrP0p7NmprSycvJzMeamsbMzc6bgYHNz87K0s/J0s3Oq8/IxcvLxczK; expires=Sun, 21-Jun-2015 05:44:35 GMT; path=/EWS; secure; HttpOnly
X-Powered-By : ASP.NET
Server : Microsoft-IIS/8.5
Cache-Control : private
X-AspNet-Version : 4.0.30319
X-FEServer : EXCHCAS1
Date : Fri, 22 May 2015 07:44:35 GMT
Transfer-Encoding : chunked
Vary : Accept-Encoding
X-CalculatedBETarget : ABC.de
X-DiagInfo : EXCHMBX2
Content-Type : text/xml; charset=utf-8

</Trace>

Type:EwsResponse Message:<Trace Tag="EwsResponse" Tid="34" Time="2015-05-22 07:44:36Z">
<?xml version="1.0" encoding="utf-8"?><s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"><s:Header><h:ServerVersionInfo MajorVersion="15" MinorVersion="0" MajorBuildNumber="1044" MinorBuildNumber="21" Version="V2_22" xmlns:h="http://schemas.microsoft.com/exchange/services/2006/types" xmlns="http://schemas.microsoft.com/exchange/services/2006/types" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/></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><t:Message><t:ItemId Id="AAMkAGJkNzI3NjFkLTAwZTktNDc1Ny04ZGNhLTE4NWZkZmE3ZGI1MgBGAAAAAAAVOAIj1ddtSqSqAI5oMVdKBwDjhlUsMPSbSr6/PYLLeTujAAAAAAEQAADjhlUsMPSbSr6/PYLLeTujAAC/7fOeAAA=" ChangeKey="CQAAABYAAADjhlUsMPSbSr6/PYLLeTujAAC/81CZ"/></t:Message></m:Items></m:CreateItemResponseMessage></m:ResponseMessages></m:CreateItemResponse></s:Body></s:Envelope>
</Trace>

Type:EwsResponseHttpHeaders Message:<Trace Tag="EwsResponseHttpHeaders" Tid="34" Time="2015-05-22 07:44:38Z">
200 text/xml; charset=utf-8
X-BEServer : EXCHMBX2
Persistent-Auth : true
request-id : 745b3f49-4768-482a-b3e4-e22893dc92b5
Set-Cookie : exchangecookie=a939f04b1c174e9cb35910c4977c335e; expires=Sun, 22-May-2016 07:44:35 GMT; path=/; HttpOnly,X-BackEndCookie=S-1-5-21-4148307-1437163908-1275988791-16652=u56Lnp2ejJqBxsuZmsnJyZvSms/LztLLmsrP0p7NmprSycvJzMeamsbMzc6bgYHNz87K0s/J0s3Oq8/IxcvLxczK; expires=Sun, 21-Jun-2015 05:44:35 GMT; path=/EWS; secure; HttpOnly
X-Powered-By : ASP.NET
Server : Microsoft-IIS/8.5
Cache-Control : private
X-AspNet-Version : 4.0.30319
X-FEServer : EXCHCAS1
Date : Fri, 22 May 2015 07:44:35 GMT
Transfermicrosoft.exchange.webservices.data.exception.ServiceRequestException: The request failed. null
    at microsoft.exchange.webservices.data.core.request.SimpleServiceRequestBase.internalExecute(SimpleServiceRequestBase.java:80)
    at microsoft.exchange.webservices.data.core.request.MultiResponseServiceRequest.execute(MultiResponseServiceRequest.java:158)
    at microsoft.exchange.webservices.data.core.ExchangeService.internalCreateItems(ExchangeService.java:594)
    at microsoft.exchange.webservices.data.core.ExchangeService.createItem(ExchangeService.java:653)
    at microsoft.exchange.webservices.data.core.service.item.Item.internalCreate(Item.java:247)
    at microsoft.exchange.webservices.data.core.service.item.EmailMessage.internalSend(EmailMessage.java:151)
    at microsoft.exchange.webservices.data.core.service.item.EmailMessage.send(EmailMessage.java:258)
    at Serienmailer$2.run(Serienmailer.java:119)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException
    at microsoft.exchange.webser-Encoding : chunked
Vary : Accept-Encoding
X-CalculatedBETarget : ABC.de
X-DiagInfo : EXCHMBX2
Content-Type : text/xml; charset=utf-8

</Trace>

vices.data.core.ExchangeServerInfo.parse(ExchangeServerInfo.java:80)
    at microsoft.exchange.webservices.data.core.request.ServiceRequestBase.readSoapHeader(ServiceRequestBase.java:530)
    at microsoft.exchange.webservices.data.core.request.ServiceRequestBase.readResponse(ServiceRequestBase.java:485)
    at microsoft.exchange.webservices.data.core.request.ServiceRequestBase.readResponse(ServiceRequestBase.java:445)
    at microsoft.exchange.webservices.data.core.request.SimpleServiceRequestBase.internalExecute(SimpleServiceRequestBase.java:69)
    ... 8 more
themartin78 commented 9 years ago

Any news on this one? It 's been a while...

themartin78 commented 9 years ago

Still n-o-t-h-i-n-g?

serious6 commented 9 years ago

please use your debugger yourself. And have a look on what is causing the problem. Everything you need to do has been deployed. Maybe also using the latest version might help. If the error still occures and you encountered the cause of this failure you can report back. I think the reason why nobody answered so far is that you will not be served with debugging here. I also mentioned that the code you provided works for me. So maybe you also should check the exchange server configuration or the surroundings in that you use the code-snippet. I gave you hints about: