Hi,
Firstly we use aspose.email 5.7.0 with a supported license.
We use Aspose library in order to clean email attachment, in several Java run.
We use Aspose library in order to clean email attachment, in several Java run.
But we encounter a problem with attachment add/remove.
In a first run, we remove an attachment with:
{code}
mapiMessage.getAttachments().removeItem(myMapiAttachment);
{code}
No error.
Then in a second run on the same email, we replace another attachment with:
(We remove and readd all elements because replacement feature appeared after the 5.7.0).
{code}
//messageFile is the source MSG file, attachmentIndex is the attachment index we want to replace and attachmentFile is the new attachment we want to inject
public void replaceAttachment(File messageFile, int attachmentIndex, File attachmentFile) throws FileNotFoundException, IOException {
try (FileInputStream fis = new FileInputStream(messageFile);
FileInputStream attchmentFis = new FileInputStream(attachmentFile); ){
MapiMessage mapiMessage = MapiMessage.fromStream(fis);
List mapiAttachments = new ArrayList();
for (MapiAttachment mapiAttachment : mapiMessage.getAttachments()) {
mapiAttachments.add(mapiAttachment);
}
for (MapiAttachment mapiAttachment : mapiAttachments) {
mapiMessage.getAttachments().removeItem(mapiAttachment);
}
int currentIndex = 0;
for (MapiAttachment mapiAttachment : mapiAttachments) {
if (currentIndex == attachmentIndex) {
mapiMessage.getAttachments().add(attachmentFile.getName(), Files.readAllBytes(attachmentFile.toPath()));
} else {
mapiMessage.getAttachments().addMapiAttachment(mapiAttachment);
}
currentIndex++;
}
mapiMessage.save(messageFile.getAbsolutePath());
}
}
{code}
This time, we encounter this error on MapiMessage.save call (we replace the attachment with index 5):
{error}
com.aspose.email.system.exceptions.ArgumentException: Key ‘_attach_version1.0#00000005’ already exists in list.
at com.aspose.email.system.collections.SortedList.a(Unknown Source) ~[aspose-email-5.7.0-jdk16.jar:5.7.0.0]
at com.aspose.email.system.collections.SortedList.addItem(Unknown Source) ~[aspose-email-5.7.0-jdk16.jar:5.7.0.0]
at com.aspose.email.MapiMessage.e(SourceFile:1943) ~[aspose-email-5.7.0-jdk16.jar:5.7.0.0]
at com.aspose.email.MapiMessage.save(SourceFile:1785) ~[aspose-email-5.7.0-jdk16.jar:5.7.0.0]
{error}
So we decided to upgrade aspose.email to 6.4.0, since it does not require any code evolution to compile.
With no modification, the attachment removing is still working, but the attachment replacement launch another error:
{error}
com.aspose.email.system.exceptions.ArgumentException: Key ‘_attach_version1.0#00000000’ already exists in list.
at com.aspose.email.system.collections.SortedList.a(Unknown Source) ~[aspose-email-6.4.0-jdk16.jar:6.4.0.0]
at com.aspose.email.system.collections.SortedList.addItem(Unknown Source) ~[aspose-email-6.4.0-jdk16.jar:6.4.0.0]
at com.aspose.email.MapiMessage.a(SourceFile:2109) ~[aspose-email-6.4.0-jdk16.jar:6.4.0.0]
at com.aspose.email.MapiMessage.e(SourceFile:1916) ~[aspose-email-6.4.0-jdk16.jar:6.4.0.0]
at com.aspose.email.MapiMessage.save(SourceFile:1865) ~[aspose-email-6.4.0-jdk16.jar:6.4.0.0]
{error}
I already tried with the 6.5.0 with the same result.
However, if I mix the versions, it is working:
I remove my attachment with aspose.email 6.4.0
I replace my attachment with aspose.email 5.7.0
I did also a modification test which is working:
In the replacement program, I use the removeAt(int) method instead of removeItem(MapiAttachment) to still remove all items, and it is working.
The weird thing is the removeItem method return “true”, that means it correctly finds the item to remove, but it must encounter problem inside.
Do you know why the removeItem does not work anymore on 6.4.0 ?
Or, why the remove method misses the attachment tag on 5.7.0 ?
Or, why the remove method misses the attachment tag on 5.7.0 ?
Regards.