I separate Message msg into Multipart multi1 = (Multipart) msg.getContent(). And a mail attachment is in one BodyPart, Part part = multi1.getBodyPart(i); Then I want to save the attachment. private void saveFile(String fileName, InputStream in) throws IOException { File file = new File(fileName); if (!file.exists()) { OutputStream out = null; try { out = new BufferedOutputStream(new FileOutputStream(file)); in = new BufferedInputStream(in); byte[] buf = new byte[BUFFSIZE]; int len; while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); } } catch (FileNotFoundException e) { LOG.error(e.toString()); } finally { // close streams if (in != null) { in.close(); } if (out != null) { out.close(); } } }
But it cost too much time on reading IO Stream. For example,a 2.7M file needs almost 160 seconds to save on the disk. I have already tried Channel and some other IO Stream, but nothing changed. Any solution for saving attachment using Java Mail? I have tried props.put("mail.imap.partialfetch", false); props.put("mail.imaps.partialfetch", false); props.put("mail.imap.fetchsize", "1048576"); but it improved a little. For more code information https://github.com/cainzhong/java-mail-demo/blob/master/src/main/java/com/java/mail/impl/ReceiveMailImpl.java |