保存电子邮件附件

时间:2012-01-23 08:37:25

标签: java email attachment pgp

有人可以建议我如何使用Java保存电子邮件附件吗? 它是加密文件(PGP)。我需要保存它并用于解密。我使用这段代码:

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

但它仅适用于文本文件(字符串)。在我的情况下,它更改.pgp文件,并给我解密错误。 如何在没有任何流的情况下保存文件? 感谢。

2 个答案:

答案 0 :(得分:3)

问题是您使用BufferedReaderBufferedWriter。这些用于字符流。将使用默认平台编码将您从附件读取的字节序列转换为Unicode字符代码点(char类型),反之亦然。在编码或解码过程中,可以从流中替换或省略不可映射的字符。

您必须使用普通InputStream来阅读附件,并使用FileOutputStream来写入文件:

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

还有其他方法可以将InputStream的内容复制到OutputStream,请参阅this question and the answers

答案 1 :(得分:0)

试试这个:

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