0

can anyone suggest me how to save email attachment using Java? It is encrypted file (PGP). I need to save it and use for decryption. I use this code:

String host = "mail.foxbox.lt";
String user = "user";
String password = "pass";
Properties properties = System.getProperties();

Session session = Session.getDefaultInstance(properties);
Store store = session.getStore("pop3");
store.connect(host, user, password);
Folder folder = store.getFolder("inbox");
folder.open(Folder.READ_WRITE);

Message[] message = folder.getMessages();
for (int a = message.length-1; a < message.length; a++) {
     Multipart multipart = (Multipart) message[a].getContent();
     for (int i = 0; i < multipart.getCount(); i++) {
          BodyPart bodyPart = multipart.getBodyPart(i);
          InputStream stream = bodyPart.getInputStream();
          if (Part.ATTACHMENT.equals(bodyPart.getDisposition())) {
               BufferedReader br = new BufferedReader(new InputStreamReader(stream));
               FileWriter fstream = new FileWriter("AAA001.txt.pgp");
               BufferedWriter out = new BufferedWriter(fstream);

               int y;
               while ((y = stream.read()) != -1) {
                    out.write(y);
               }
               stream.close();
               out.close();
          }
     }
     System.out.println();
}
folder.close(true);
store.close();

But it works only for text files (strings). in my case it changes .pgp file and gives me decryption error. How do I save file without any stream? thanks.

buc
  • 6,268
  • 1
  • 34
  • 51
user1164545
  • 129
  • 3
  • 5
  • 10

2 Answers2

4

The problem is that you use BufferedReader and BufferedWriter. These are intended to use with character streams. The will convert the byte sequence that you read from the attachment to Unicode character codepoints (char type) using the default platform encoding and vice versa. During the encoding or decoding process, unmappable characters may be replaced or left out from the stream.

You have to use plain InputStream for reading the attachment and FileOutputStream for writing to the file:

InputStream stream = bodyPart.getInputStream();
if (Part.ATTACHMENT.equals(bodyPart.getDisposition())) {
    FileOutputStream fstream = new FileOutputStream("AAA001.txt.pgp");

    byte[] buffer = new byte[1024];
    int len;
    while ((len = stream.read(buffer)) != -1) {
        fstream.write(buffer, 0, len);
    }

    fstream.close();
}
stream.close();

There are other methods for copying the contents of an InputStream to a OutputStream, see this question and the answers.

Community
  • 1
  • 1
buc
  • 6,268
  • 1
  • 34
  • 51
0

Try this:

String host = "host";
String user = "user";
String password = "pass";
Properties properties = System.getProperties();
Session session = Session.getDefaultInstance(properties);
Store store = session.getStore("pop3");
store.connect(host, user, password);
Folder folder = store.getFolder("inbox");
folder.open(Folder.READ_WRITE);
Message[] message = folder.getMessages();
for (int a = message.length-1; a < message.length; a++) {
        Multipart multipart = (Multipart) message[a].getContent();    
  for (int i = 0; i < multipart.getCount(); i++) {
      BodyPart bodyPart = multipart.getBodyPart(i);           
      InputStream is = bodyPart.getInputStream();
      File f = new File("/tmp/" + bodyPart.getFileName());
      FileOutputStream fos = new FileOutputStream(f);
      byte[] buf = new byte[4096];
      int bytesRead;
      while((bytesRead = is.read(buf))!=-1) {
          fos.write(buf, 0, bytesRead);
      }
      fos.close();
      attachments.add(f);
    }
  }
  folder.close(true);
  store.close();
 }
Korhan Ozturk
  • 11,148
  • 6
  • 36
  • 49