Java 项目附件可以';不能使用EWS下载
我在使用EWS下载“ItemAttachment”类型的附件时遇到问题。 以下是我用来下载附件的代码:Java 项目附件可以';不能使用EWS下载,java,exchangewebservices,Java,Exchangewebservices,我在使用EWS下载“ItemAttachment”类型的附件时遇到问题。 以下是我用来下载附件的代码: PropertySet ps = new PropertySet(BasePropertySet.FirstClassProperties); ps.add(ItemSchema.MimeContent); ia = (ItemAttachment) attach; ia.
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.data.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.ValidateAndItemRequest(未知源)
位于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.roundrobinmailer.sendunadmailtorrecpt上(roundrobinmailer.java:173)
在com.medpro.roundrobinmailer.roundrobinmailer.delegateAccessSearchEmailWithFilter上(roundrobinmailer.java:114)
位于com.medpro.roundrobinmailer.roundrobinmailer.main(roundrobinmailer.java:57)
代码在以下位置中断:
ia.荷载(ps)
这让我快发疯了。
请帮忙
谢谢
图萨/*
@作者:辛克
此方法下载与电子邮件关联的所有附件
我认为您遇到了错误,因为在加载之前,您需要绑定项目。。下面是工作正常的代码。。。已测试并正在我的项目中使用。。希望能有帮助。。祝你好运
*/
公共静态void getAttachments(ExchangeService服务,ItemId)引发异常{
Item=Item.bind(服务,id);
item.load();
如果(item.getHasAttachments()){
AttachmentCollection attachmentsCol=item.getAttachments();
int NumberOfAttachments=attachmentsCol.getCount();
System.out.println(“正在下载的附件::”+NumberOfAttachments);
String directoryName=“C:\\MSExchangeEmail\\WebContent\\attachments”;
File theDir=新文件(目录名);
//如果目录不存在,请创建它
如果(!theDir.exists()){
System.out.println(“创建目录:“+directoryName”);
布尔结果=假;
试一试{
结果=dir.mkdir();
}捕获(安全异常se){
//处理它
}
如果(结果){
System.out.println(“创建的目录:+结果”);
}
}
对于(inti=0;i
这是EWS Java中的一个已知错误。幸运的是,它是开源的,可以在Github上获得:
见第12期。问题出在ExchangeService.internalGetAttachments()中。其中有一些非常丑陋的代码,它使用了一种无法解释的泛型和非类型集合的混合。结果,当它应该复制单个PropertyDefinitionBase对象时,它会将整个PropertySet添加到列表中,这就是为什么会看到ClassCastException。看一看。如果它还没有被修复,那么你自己也不难做到。嗨,拉杰,获取以下错误:正在下载的附件::3 java.lang.ClassCastException:microsoft.exchange.webservices.data.ItemAttachment无法转换为com.medpro.roundrobinmailer.roundrobinmailer.sendunadmailtorrecpt(roundrobinmailer.java:170)上的microsoft.exchange.webservices.data.FileAttachment在com.medpro.roundrobinmailer.roundrobinmailer.delegateAccessSearchEmailWithFilter(roundrobinmailer.java:107)在com.medpro.roundrobinmailer.roundrobinmailer.main(roundrobinmailer.java:50)上,这种情况从昨天起就一直发生在我身上。当我使用itemAttachment对象下载附件时,api突然崩溃,不允许我下载任何东西,当我尝试使用itemAttachment时,load函数要求我以PropertyDefinitionBase的形式提供参数,我不知道如何将PropertSet对象强制转换为PropertyDefinitionBase对象。而不是Item Item=Item.bind(service,id),然后是Item.load()。您可以直接尝试此操作。。。我也面临着同样的负载问题,我尝试了这个,它被解决了。。。item=item.bind(service,id,propertySet)-->例如[item item=item.bind(mailList.get(i).getService(),mailList.get(i).getItemId(),new propertySet(ItemSchema.MimeContent))]由于错误,我从未使用过RoundRobinMailer,我的应用程序是否使用了JavaMailer。。。代码运行良好。。。试着加载一个可能会很好。。。PS:如果可能的话,让我们看看你的代码,也许其他人也能提供帮助
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());
}
}
}