ari*_*ayu 6 gmail multipart jakarta-mail
我目前正在开发一个从gmail帐户下载附件的应用程序.现在,每当下载压缩附件时我都会收到错误.但是,并非所有,我可以毫无错误地检索它.这是Exception消息:
Exception in thread "main" com.sun.mail.util.DecodingException: BASE64Decoder: Error in encoded stream: needed 4 valid base64 characters but only got 1 before EOF, the 10 most recent characters were: "Q3w5ilxj2P"
Run Code Online (Sandbox Code Playgroud)
仅供参考:我可以通过gmail web界面下载附件.
这是片段:
Multipart multipart = (Multipart) message.getContent();
for (int i = 0; i < multipart.getCount(); i++) {
BodyPart bodyPart = multipart.getBodyPart(i);
if (bodyPart.getFileName().toLowerCase().endsWith("zip") ||
bodyPart.getFileName().toLowerCase().endsWith("rar")) {
InputStream is = bodyPart.getInputStream();
File f = new File("/tmp/" + bodyPart.getFileName());
FileOutputStream fos = new FileOutputStream(f);
byte[] buf = new byte[bodyPart.getSize()];
int bytesRead;
while ((bytesRead = is.read(buf)) != -1) {
fos.write(buf, 0, bytesRead);
}
fos.close();
}
}
}
Run Code Online (Sandbox Code Playgroud)
任何人都有想法,如何解决这个问题?
小智 10
从JavaMail的已知限制,错误和问题列表中:
某些IMAP服务器未正确实现IMAP部分FETCH功能.从IMAP服务器下载大邮件时,此问题通常表现为损坏的电子邮件附件.要解决此服务器错误,请将"mail.imap.partialfetch"属性设置为false.您必须在您提供给Session的Properties对象中设置此属性.
所以你应该关闭 imap会话中的部分提取.例如:
Properties props = System.getProperties();
props.setProperty("mail.store.protocol", "imap");
props.setProperty("mail.imap.partialfetch", "false");
Session session = Session.getDefaultInstance(props, null);
Store store = session.getStore("imaps");
store.connect("imap.gmail.com", "<username>","<password>");
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5384 次 |
| 最近记录: |