Hi,
I've tried to use the MapiMessage class and found that this class solved the invalid sender address issue.
However, I have a problem when I try to remove one or more attachments from the email.
Basically I need to make sure the email is stripped of all attachments and saved with any embedded images in the body. When the MapiMessage class is used embedded images and normal attachments are both found in the attachment collection. So I tried to identify the attachments that aren't embedded images and only remove them. But when I do this and try to open the email I get an error "Could not open one or more attachments".
Here is my code:
MapiMessage origMessage = MapiMessage.fromFile("C:\\Temp\\inputfiles\\test2\\test email with embedded images.msg");
for (int i = 0; i < origMessage.getAttachments().size(); i++)
{
MapiAttachment attachment = (MapiAttachment) origMessage.getAttachments().get(i);
if (attachment.getProperties().contains(0x3712001E) || attachment.getProperties().contains(0x3712001F)) {
// Embedded image leave it in the email
} else {
// Normal attachment, remove from the email
origMessage.getAttachments().remove(i);
i = i - 1;
}
}
origMessage.save("C:\\Temp\\inputfiles\\test2\\out.msg");
The problem appears to be because I am trying to remove an item from the collection when there are other items after it. The problem can also be reproduced by doing the following:
MapiMessage origMessage = MapiMessage.fromFile("C:\\Temp\\inputfiles\\test2\\test email with embedded images.msg");
origMessage.getAttachments().remove(0);
origMessage.save("C:\\Temp\\inputfiles\\test2\\out.msg");
When you open "out.msg" it will show the error I mentioned earlier. I've attached the input file I used for this to the post.
What is the best way to remove items from the attachments collection at different positions?
Regards,