我在使用EWS下载"ItemAttachment"类型的附件时遇到问题。以下是我用来下载附件的代码:
PropertySet ps = new PropertySet(BasePropertySet.FirstClassProperties);
ps.add(ItemSchema.MimeContent);
ia = (ItemAttachment) attach;
ia.load(ps);
//ia.load();
System.out.println(ia.getItem().getSubject());
MimeContent mc = ia.getItem().getMimeContent();
String itemName = ia.getName().replace(" ","").replace(":","-").trim();
attname.add(itemName);
System.out.println(itemName);
byte[] contentBytes = mc.getContent();
theStream = new FileOutputStream(
"C:\Users\502000317\Desktop\test\"
+ itemName);
ia.getItem().getAttachments();
theStream.write(contentBytes);
theStream.flush();
但是当我尝试执行这个时,我得到了以下错误:
java.lang.ClassCastException:无法将microsoft.exchange.webservices.data.PropertySet强制转换为microsoft.exchange.webservices.da.PropertyDefinitionBase位于microsoft.exchange.webservices.data.PropertySet.writeAdditionalPropertiesToXml(未知源)位于microsoft.exchange.webservices.data.GetAttachmentRequest.writeElementsToXml(未知源)位于microsoft.exchange.webservices.data.ServiceRequestBase.writeBodyToXml(未知源)位于microsoft.exchange.webservices.data.ServiceRequestBase.writeToXml(未知源)位于microsoft.exchange.webservices.data.ServiceRequestBase.buildEwsHttpWebRequest(未知源)位于microsoft.exchange.webservices.data.ServiceRequestBase.validateAndEmitRequest(未知源)位于microsoft.exchange.webservices.data.SimpleServiceRequestBase.internalExecute(未知源)位于microsoft.exchange.webservices.data.MultiResponseServiceRequest.execute(未知来源)位于microsoft.exchange.webservices.data.ExchangeService.internalGetAttachments(未知来源)位于microsoft.exchange.webservices.data.ExchangeService.getAttachment(未知来源)位于microsoft.exchange.webservices.data.Attachment.internalLoad(未知来源)位于microsoft.exchange.webservices.data.ItemAttachment.load(未知来源)网址:com.medpro.roundrobinmailer.RoundRobinMail.sendunreadmailtoRRRecpt(RoundRobinMail.java:173)网址:com.medpro.roundrobinmailer.roundrobinmailer.delegateAccessSearchEmailWithFilter(roundrobinmailer.java:114)网址:com.medpro.roundrobinmailer.roundrobinmailer.main(RoundRobinLoader.java:57)
代码在以下位置中断:ia负荷(ps);
这把我逼疯了。请帮忙。
谢谢Tushar
这是EWS Java中的一个已知错误。幸运的是,它是开源的,可以在Github:上使用
https://github.com/OfficeDev/ews-java-api
参见第12期。问题出在ExchangeService.internalGetAttachments()中。其中有一些非常丑陋的代码,使用了无法解释的泛型和非类型化集合的混合。因此,当它本应复制到各个PropertyDefinitionBase对象上时,它最终会将整个PropertySet添加到列表中,这就是您看到ClassCastException的原因。看一看。如果它还没有被修复,那么你自己应该不难做到。
/*@作者rmanssink
此方法下载与电子邮件相关的所有附件
我认为你会出错,因为在加载之前你需要绑定项目。。以下是运行良好的代码。。。经过测试,目前正在我的项目中使用。。希望能有所帮助。。祝好运
*/
public static void getAttachments(ExchangeService service, ItemId id) throws Exception{
Item item = Item.bind(service, id);
item.load();
if (item.getHasAttachments()){
AttachmentCollection attachmentsCol = item.getAttachments();
int NumberOfAttachments = attachmentsCol.getCount();
System.out.println("Attchments being Downloaded:: "+NumberOfAttachments);
String directoryName = "C:\MSExchangeEmail\WebContent\attachments";
File theDir = new File(directoryName);
// if the directory does not exist, create it
if (!theDir.exists()) {
System.out.println("creating directory: " + directoryName);
boolean result = false;
try{
result = theDir.mkdir();
} catch(SecurityException se){
//handle it
}
if(result) {
System.out.println("DIR created: "+result);
}
}
for (int i = 0; i < NumberOfAttachments; i++) {
FileAttachment attachment = (FileAttachment)attachmentsCol.getPropertyAtIndex(i);
attachment.load(directoryName+"\"+attachment.getName());
}
}
}