writing email attachment file to disk  
Author Message
seller





PostPosted: 2005-9-7 4:57:00 Top

java-programmer, writing email attachment file to disk I'm trying to capture an email attachment and write it to disk.
I've coded the following lines in my attempt. The program seems
to run and creates the local file alex99.pdf. However, when I
tried to open it, it says it's corrupted and/or wasn't properly
decoded. Any ideas?

Properties props = new Properties();
Session session = Session.getDefaultInstance(props, null);
Store store = session.getStore("pop3");
store.connect(host, username, password);
Folder folder = store.getFolder("inbox");
folder.open(Folder.READ_ONLY);
Message message[] = folder.getMessages();
for (int i = 0, n = message.length; i < n; i++) {
System.out.println(i + ": " + message[i].getFrom() + "\t"
+ message[i].getSubject());
Object obj = message[i].getContent();
if (obj instanceof String) {
System.out.println(">>>plain text");
} else if (obj instanceof Multipart) {
System.out.println("Multipart");
Multipart mp = (Multipart) obj;
System.out.println("mp count = " + mp.getCount());
for (int j = 0, nn = mp.getCount(); j < nn; j++) {
Part part = mp.getBodyPart(j);
String disposition = part.getDisposition();
if ((disposition != null)&& ((disposition.equals(Part.ATTACHMENT) ||
(disposition.equals(Part.INLINE))))) {
System.out.println("attachment");
FileOutputStream fos = new FileOutputStream("alex99.pdf");
part.writeTo(fos);
} else if (disposition==null) {
MimeBodyPart mbp = (MimeBodyPart)part;
if (mbp.isMimeType("text/plain")) {
System.out.println("text/plain");
} else {
System.out.println("other mime type");
}................................

 
Oliver Wong





PostPosted: 2005-9-7 5:11:00 Top

java-programmer >> writing email attachment file to disk
"seller" <email***@***.com> wrote in message
news:email***@***.com...
> I'm trying to capture an email attachment and write it to disk.
> I've coded the following lines in my attempt. The program seems
> to run and creates the local file alex99.pdf. However, when I
> tried to open it, it says it's corrupted and/or wasn't properly
> decoded. Any ideas?

Did you try comparing the original pdf file you sent yourself with the
pdf file created by your program in a hex editor?

- Oliver


 
seller





PostPosted: 2005-9-7 9:05:00 Top

java-programmer >> writing email attachment file to disk
Oliver Wong wrote:
> "seller" <email***@***.com> wrote in message
> news:email***@***.com...
> > I'm trying to capture an email attachment and write it to disk.
> > I've coded the following lines in my attempt. The program seems
> > to run and creates the local file alex99.pdf. However, when I
> > tried to open it, it says it's corrupted and/or wasn't properly
> > decoded. Any ideas?
>
> Did you try comparing the original pdf file you sent yourself with the
> pdf file created by your program in a hex editor?
>
> - Oliver

no i didn't. but the file properties are different. the size for one
thing........

 
 
seller





PostPosted: 2005-9-7 9:07:00 Top

java-programmer >> writing email attachment file to disk
Oliver Wong wrote:
> "seller" <email***@***.com> wrote in message
> news:email***@***.com...
> > I'm trying to capture an email attachment and write it to disk.
> > I've coded the following lines in my attempt. The program seems
> > to run and creates the local file alex99.pdf. However, when I
> > tried to open it, it says it's corrupted and/or wasn't properly
> > decoded. Any ideas?
>
> Did you try comparing the original pdf file you sent yourself with the
> pdf file created by your program in a hex editor?
>
> - Oliver

also tried different file types, jpeg, bmp, gif. same thing....

 
 
seller





PostPosted: 2005-9-7 9:34:00 Top

java-programmer >> writing email attachment file to disk
Oliver Wong wrote:
> "seller" <email***@***.com> wrote in message
> news:email***@***.com...
> > I'm trying to capture an email attachment and write it to disk.
> > I've coded the following lines in my attempt. The program seems
> > to run and creates the local file alex99.pdf. However, when I
> > tried to open it, it says it's corrupted and/or wasn't properly
> > decoded. Any ideas?
>
> Did you try comparing the original pdf file you sent yourself with the
> pdf file created by your program in a hex editor?
>
> - Oliver

also tried different file types, jpeg, bmp, gif. same thing....

 
 
Roedy Green





PostPosted: 2005-9-7 12:49:00 Top

java-programmer >> writing email attachment file to disk On 6 Sep 2005 13:56:40 -0700, "seller" <email***@***.com> wrote or
quoted :

>I'm trying to capture an email attachment and write it to disk.
>I've coded the following lines in my attempt. The program seems
>to run and creates the local file alex99.pdf. However, when I
>tried to open it, it says it's corrupted and/or wasn't properly
>decoded. Any ideas?

The key is some code that looks in essence like this :

MimeMessage rm = folder.getMessages()[0];
Multipart rmp = (Multipart )rm.getContent();
Part attach = rmp.getBodyPart( attachmentIndex );
String recipientList = ( String )attach.getContent();

JMF/Javamail must have some giant list of MIME types in it telling it
what sort of Object to create with getContent. You had better be sure
your MIME and content-encoding are in its list. What you may need to
do is wrap your PDF file as something very vanilla rather than an
application/pdf, e.g. application/octet-stream.

In any case, turn on debugging so you get a better feel for what is
coming down the wire. You might also snoop with a packet sniffer. See
http://mindprod.com/jgloss/packetsniffer.html
Watch what Eudora does and then compare that with what you are doing.

You are trying to debug in the dark.
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.
 
 
Roedy Green





PostPosted: 2005-9-7 15:08:00 Top

java-programmer >> writing email attachment file to disk On Wed, 07 Sep 2005 04:49:02 GMT, Roedy Green
<email***@***.com> wrote or quoted :

>JMF/Javamail must have some giant list of MIME types in it telling it
>what sort of Object to create with getContent.

I found this list in activation.jar in mimetypes.default

#
# A simple, old format, mime.types file
#
text/html html htm HTML HTM
text/plain txt text TXT TEXT
image/gif gif GIF
image/ief ief
image/jpeg jpeg jpg jpe JPG
image/tiff tiff tif
image/x-xwindowdump xwd
application/postscript ai eps ps
application/rtf rtf
application/x-tex tex
application/x-texinfo texinfo texi
application/x-troff t tr roff
audio/basic au
audio/midi midi mid
audio/x-aifc aifc
audio/x-aiff aif aiff
audio/x-mpeg mpeg mpg
audio/x-wav wav
video/mpeg mpeg mpg mpe
video/quicktime qt mov
video/x-msvideo avi

I found application/octet-stream in MimeTypesFileTypeMap.class

The docs at jaf-1.0.2/docs/javadocs/index.html talk about the various
places JAF goes looking for MIME types. That just gets you
extension-MIME maps. There is still the business of which DataHandlers
for each MIME type are implemented.

see http://mindprod.com/jgloss/mime.html#JAF
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.
 
 
Oliver Wong





PostPosted: 2005-9-7 23:42:00 Top

java-programmer >> writing email attachment file to disk
"seller" <email***@***.com> wrote in message
news:email***@***.com...
>
> Oliver Wong wrote:
>> Did you try comparing the original pdf file you sent yourself with
>> the
>> pdf file created by your program in a hex editor?
>>
>> - Oliver
>
> no i didn't. but the file properties are different. the size for one
> thing........
>

All the more reason to open it up in a hex editor. See what extra bytes
are getting added.

- Oliver


 
 
Roedy Green





PostPosted: 2005-9-10 17:04:00 Top

java-programmer >> writing email attachment file to disk On Wed, 07 Sep 2005 15:42:26 GMT, "Oliver Wong" <email***@***.com>
wrote or quoted :

> All the more reason to open it up in a hex editor. See what extra bytes
>are getting added.

see http://mindprod.com/jgloss/hex.html for some hex editors.
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.