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.