Problem reading "APPLEFILE" attachments
0 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Per_Jensen
Posted On:   Thursday, March 19, 2009 11:11 AM

At my work, we use JavaMail to autoprocess mails with attached files from customers. Mostly TIFF images, but also PDF files. Sometimes, we have a problem, until now only seen on with PDF files. The files get saved, but is of size 0 bytes. If I forward the original message, however, (using an Outlook client), the forwarded item can be read by our JavaMail program, and files saved OK. If I copy/move the original mail between mailfolders, it's still not OK. I have tried to forward it using JavaMail, but I cannot compose the mail parts, that fails, however - of the same reasons: I cannot read the them in:-) It looks like it's when the mail is send from a Mac. Anyway, the part to r   More>>

At my work, we use JavaMail to autoprocess mails with attached files from customers.
Mostly TIFF images, but also PDF files.
Sometimes, we have a problem, until now only seen on with PDF files.
The files get saved, but is of size 0 bytes. If I forward the original
message, however, (using an Outlook client), the forwarded item can be read by
our JavaMail program, and files saved OK.
If I copy/move the original mail between mailfolders, it's still not OK.

I have tried to forward it using JavaMail, but I cannot
compose the mail parts, that fails, however - of the same reasons: I cannot read the them in:-)

It looks like it's when the mail is send from a Mac.
Anyway, the part to read is of content type "APPLICATION/APPLEFILE" whereas when it's
forwarded and becomes OK, it's content type is "APPLICATION/OCTET-STREAM"

The way, I read the part, is to check if the part is an
instance of an InputStream, and then saving into a byte array, like this:

			
InputStream inStream = part.getInputStream();
byte[] buffer = new byte[BUFFER_SIZE];
int bytesRead;
BufferedOutputStream outBufStream = new BufferedOutputStream(new FileOutputStream(attFileName));
while ((bytesRead = inStream.read(buffer)) != -1) {
outBufStream.write(buffer, 0, bytesRead);
}
inStream.close();
outBufStream.close();

The code is the same for OK attachments, off course.
Problem is that .read() method always returns -1 when content type Is APPLICATION/APPLEFILE,
so a file of 0 bytes are created.


Interesting is perhaps, that debugging shows me, that the
part instance of the InputStream actually is a
com.sun.mail.util.BASE64DecoderStream when it fails, and a
com.sun.mail.util.QPDecoderStream when it works OK.


I can save the WHOLE message to a file, via a ByteArrayOutputStream, using
message.getInputStream(), and when I opens the file I can find the
individual attachments - as well as the bodytext, if any - as they are separated
with the disposition, mimetype and filename fields, showed as plain text.
And the attachments are showed as blocks of characters; I guess it's the
ascii character representation of the bytes. Here's a snippet, note the
first two sentences. They are added, somehow, when I dump the message to file:

			

This message is in MIME format. Since your mail reader does not understand
this format, some or all of this message may not be legible.

------_=_NextPart_000_01C9A8A2.19479FE4
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

(The body text here)

------_=_NextPart_000_01C9A8A2.19479FE4
Content-Type: application/applefile;
name="first_attachment.pdf"
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
filename="first_attachment.pdf"

AAUWAAACAAAAAAAAAAAAAAAAAAAAAAAAAAQAAAAIAAAASgAAABAAAAAJAAAAWgAAACAAAAADAAAA
egAAABAAAAABAAAAigAAI6IAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAA1NzE2NSBCTkhhdWcucGRmJVBERi0xLjMKJbe+raoKMSAwIG9iago8PAovVHlwZSAv
Q2F0YWxvZwovUGFnZXMgMiAwIFIKPj4KZW5kb2JqCjIgMCBvYmoKPDwKL1R5cGUgL1BhZ2VzCi9L
aWRzIFsgNCAwIFIgXQovQ291bnQgMQo+PgplbmRvYmoKMyAwIG9iago8PAovUHJvZHVjZXIgKEhh
...(and so on)
...
DMgMDAwMDAgbg0KMDAwMDAwNTM1NSAwMDAwMCBuDQowMDAwMDA2NTA5IDAwMDAwIG4NCjAwMDAw
MDc2NjUgMDAwMDAgbg0KdHJhaWxlcgo8PAovUm9vdCAxIDAgUgovSW5mbyAzIDAgUgovU2l6ZSAx
MQo+PgpzdGFydHhyZWYKODgyNAolJUVPRgo=

------_=_NextPart_000_01C9A8A2.19479FE4
Content-Type: application/applefile;
name="second_attachment.pdf"
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
filename="second_attachment.pdf"

AAUWAAACAAAAAAAAAAAAAAAAAAAAAAAAAAQAAAAIAAAASgAAABAAAAAJAAAAWgAAACAAAAADAAAA
egAAABAAAAABAAAAigAAI9wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAA1NzE4MSBCTkhhdWcucGRmJVBERi0xLjMKJbe+raoKMSAwIG9iago8PAovVHlwZSAv
Q2F0YWxvZwovUGFnZXMgMiAwIFIKPj4KZW5kb2JqCjIgMCBvYmoKPDwKL1R5cGUgL1BhZ2VzCi9L
...(and so on)


I understand that it's base64 encoded and must be decoded, and I have tried
to copy/paste the blocks separately, run them through
the com.sun.mail.util.BASE64DecoderStream, and saving them as PDF
files, which works fine.


We use Java 1.6.0 update 10 and JavaMail 1.4.2. and since we use
java 1.6 we use the JAF framework from that one.


Server is (from trace log):

Microsoft Exchange IMAP4rev1-server version 5.5.2650.23
but I have tried:

Microsoft Exchange Server 2003 IMAP4rev1 server version 6.5.7226.0 too.


It would be painfull to parse the mail to find where the attachments
starts and stops and saving them as files, so I hope you guys can help?


Kind regards, Per

   <<Less
About | Sitemap | Contact