我正在使用EWS-java-api访问Outlook电子邮件和阅读电子邮件。 我遇到了以下错误:
microsoft.exchange.webservices.data.core.exception.service.remote.ServiceRequestException: The request failed. null
这是我的代码:
import microsoft.exchange.webservices.data.core.ExchangeService;
import microsoft.exchange.webservices.data.core.enumeration.misc.ExchangeVersion;
import microsoft.exchange.webservices.data.core.enumeration.property.WellKnownFolderName;
import microsoft.exchange.webservices.data.core.service.item.Item;
import microsoft.exchange.webservices.data.credential.WebCredentials;
import microsoft.exchange.webservices.data.search.FindItemsResults;
import microsoft.exchange.webservices.data.search.ItemView;
import microsoft.exchange.webservices.data.search.filter.SearchFilter;
import java.util.Calendar;
import java.util.Date;
import java.util.Properties;
Properties properties = new Properties();
properties.put("javax.net.ssl.trustStore", "C:/Users/<path to java security>/cacerts" );
properties.put("javax.net.ssl.trustStorePassword","changeit");
System.setProperties(properties);
service = new ExchangeService(ExchangeVersion.Exchange2010_SP2);
def usr = "abc@mycompany.ca";
def pwd = "password";
def url = "https://mycompanydomain/ews/exchange.asmx";
service = new ExchangeService(ExchangeVersion.Exchange2010_SP2);
credentials = new WebCredentials(usr, pwd);
service.setCredentials(credentials);
try {
service.setUrl(new URI(url));
service.setTraceEnabled(true);
Calendar cal = Calendar.getInstance();
cal.roll(Calendar.MONTH, false);
view = new ItemView(50);
findResults = service.findItems(WellKnownFolderName.Inbox, view);
for(item in findResults.getItems()) {
println("\n + email is: "+item+ "\n") ;
}
} catch (Exception e) {
e.printStackTrace();
}
这是完整的堆栈跟踪:
15:32:13 [INFO] ScriptProcessor starts processing...
microsoft.exchange.webservices.data.core.exception.service.remote.ServiceRequestException: The request failed. null
15:32:14 [INFO] ScriptProcessor processor executed in 889ms.
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 microsoft.exchange.webservices.data.core.ExchangeService.findItems(ExchangeService.java:985)
at microsoft.exchange.webservices.data.core.ExchangeService.findItems(ExchangeService.java:1028)
at microsoft.exchange.webservices.data.core.ExchangeService.findItems(ExchangeService.java:1104)
at microsoft.exchange.webservices.data.core.ExchangeService$findItems$0.call(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:133)
at Script1.run(Script1.groovy:54)
at groovy.lang.GroovyShell.evaluate(GroovyShell.java:585)
at groovy.lang.GroovyShell.evaluate(GroovyShell.java:623)
at groovy.lang.GroovyShell.evaluate(GroovyShell.java:594)
at org.webharvest.runtime.scripting.GroovyScriptEngine.eval(GroovyScriptEngine.java:136)
at org.webharvest.runtime.processors.ScriptProcessor.execute(ScriptProcessor.java:74)
at org.webharvest.runtime.processors.BaseProcessor.run(BaseProcessor.java:127)
at org.webharvest.runtime.Scraper.execute(Scraper.java:169)
at org.webharvest.runtime.Scraper.execute(Scraper.java:182)
at com.freedomoss.crowdcontrol.webharvest.executor.LocalWebharvestTaskExecutor.executeWebHarvestTask(LocalWebharvestTaskExecutor.java:182)
at com.workfusion.studio.launch.SingleThreadWebHarvestProcess.processTaskInputs(SingleThreadWebHarvestProcess.java:77)
at com.workfusion.studio.launch.SingleThreadWebHarvestProcess.start(SingleThreadWebHarvestProcess.java:46)
at com.workfusion.studio.launch.WebHarvestMainLauncher.launch(WebHarvestMainLauncher.java:79)
at com.workfusion.studio.launch.WebHarvestMainLauncher.main(WebHarvestMainLauncher.java:121)
Caused by: java.lang.NullPointerException
at com.ctc.wstx.sw.BaseStreamWriter.writeCharacters(BaseStreamWriter.java:447)
at microsoft.exchange.webservices.data.core.EwsUtilities.formatLogMessage(EwsUtilities.java:558)
at microsoft.exchange.webservices.data.core.ExchangeServiceBase.traceHttpRequestHeaders(ExchangeServiceBase.java:515)
at microsoft.exchange.webservices.data.core.request.ServiceRequestBase.buildEwsHttpWebRequest(ServiceRequestBase.java:686)
at microsoft.exchange.webservices.data.core.request.ServiceRequestBase.buildEwsHttpWebRequest(ServiceRequestBase.java:665)
at microsoft.exchange.webservices.data.core.request.ServiceRequestBase.validateAndEmitRequest(ServiceRequestBase.java:635)
at microsoft.exchange.webservices.data.core.request.SimpleServiceRequestBase.internalExecute(SimpleServiceRequestBase.java:62)
... 22 more
非常感谢任何帮助。
答案 0 :(得分:0)
我在设置服务网址时遇到了同样的问题,因此我首先通过设置凭据和设置自动发现来完成
发生这种情况时,可以提示用户接受或不接受重定向,而不是失败。该功能需要在autodiscoverRedirectionUrlValidationCallback方法内实现。在下面的示例中,它仅检查重定向URL以“ https://”开头。为此
static class RedirectionUrlCallback implements IAutodiscoverRedirectionUrl {
public boolean autodiscoverRedirectionUrlValidationCallback(
String redirectionUrl) {
return redirectionUrl.toLowerCase().startsWith("https://");
}
}
现在
service.autodiscoverUrl("<your_email_address>", new RedirectionUrlCallback());
可以调用以安全的方式处理重定向。