wieseljonas / java-libpst

Automatically exported from code.google.com/p/java-libpst
1 stars 1 forks source link

Working with attachments #15

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
I'm sorry to contact you through the issues area, but I could not find any 
other way to get a hold of you.

I am having trouble figuring out how to work with attachments. I would like to 
somehow save the attachments in a message (if it has any) somewhere on my 
computer, but cannot find anything in PSTAttachment to extract an attachment.

My code looks as follows:

PSTAttachment attachment;
if(msg.hasAttachments())
{
  for(int i = 0; i < msg.getNumberOfAttachments(); i++)
  {
    attachment = msg.getAttachment(i);
    //cannot figure out what to do after this...
  }
}

Ideally I would like to get the attachments, one at a time (hence the for 
loop), and save them somewhere.

Any help would be appreciated

(Awesome library by the way!) 

Original issue reported on code.google.com by aneepbin...@gmail.com on 29 Jul 2010 at 6:28

GoogleCodeExporter commented 9 years ago
All looks to me so far!

You should be able to get the contents of the attachment by calling:
byte[] contents = attachment.getFileContents()

You'd probably want to throw it into a file or something (note: untested & 
checked):

FileOutputStream out = new FileOutputStream(attachment.getLongFilename());
out.write(contents);
out.close();

Original comment by rjohnson...@gmail.com on 29 Jul 2010 at 7:00

GoogleCodeExporter commented 9 years ago
Wow with a couple tweaks it works! Just had to put a check in there to make 
sure attachment.getFileSize() != 0 (else it would throw an exception)

Thanks again for the help and an amazing library.

Original comment by aneepbin...@gmail.com on 30 Jul 2010 at 3:48

GoogleCodeExporter commented 9 years ago
Cool, glad it's working for you.  I've just bundled a new release which has 
significant fixes and some new functions.  It's recommended upgrading.

Original comment by rjohnson...@gmail.com on 30 Jul 2010 at 4:01

GoogleCodeExporter commented 9 years ago
Hi,i tried this code :
  public static String parseMailAttachments(XHTMLContentHandler xhtml, PSTMessage  email, EmbeddedDocumentExtractor embeddedExtractor)throws TikaException, IOException, PSTException, Exception {
    int numberOfAttachments = email.getNumberOfAttachments();

    String resulattachement=new String("");

    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();
    }

      return resulattachement;
    }
} 

and i have this error :
Caused by: java.io.FileNotFoundException: Invalid file path

Original comment by amiren...@gmail.com on 20 Aug 2014 at 3:38

GoogleCodeExporter commented 9 years ago
who can help me !!!!!!!!!!!!

Original comment by amiren...@gmail.com on 21 Aug 2014 at 8:05