How do I extract EMBEDDED and OLE attachments?

75 views
Skip to first unread message

Ben Reid

unread,
Jul 3, 2012, 2:59:49 AM7/3/12
to java-libp...@googlegroups.com
I am having trouble extracting attachments where the AttachMethod is EMBEDDED or OLE. I have successfully extracted attachments where the AttachMethod is BY VALUE using the example code.

The example code (see below) says that I can extract attachments using getFileInputStream() - but this doesnt seem to work for EMBEDDED or OLE attachments.


Attachments can be read through PSTAttachment.getFileInputStream like so:

int numberOfAttachments = email.getNumberOfAttachments();
for (int x = 0; x < numberOfAttachments; x++) {
       
PSTAttachment attach = email.getAttachment(x);
       
InputStream attachmentStream = attach.getFileInputStream();
       
// both long and short filenames can be used for attachments
       
String filename = attach.getLongFilename();
       
if (filename.isEmpty()) {
                filename
= attach.getFilename();
       
}
       
FileOutputStream out = new FileOutputStream(filename);
       
// 8176 is the block size used internally and should give the best performance
       
int bufferSize = 8176;
       
byte[] buffer = new byte[bufferSize];
       
int count = attachmentStream.read(buffer);
       
while (count == bufferSize) {
               
out.write(buffer);
                count
= attachmentStream.read(buffer);
       
}
       
byte[] endBuffer = new byte[count];
       
System.arraycopy(buffer, 0, endBuffer, 0, count);
       
out.write(endBuffer);
       
out.close();
        attachmentStream
.close();
}


Dimitry Erastov

unread,
Jul 9, 2012, 2:28:40 AM7/9/12
to java-libp...@googlegroups.com
What exactly is the problem? Does getFileInputStream() fail? I have a few messages with embedded attachments that my application extracted and they look fine. I don't do anything special for them, just set content disposition and content-id, if available from the source message.
Reply all
Reply to author
Forward
0 new messages