我目前正在开发一个从 gmail 帐户下载附件的应用程序。
现在,每次下载压缩附件时都会出错。但是,并非全部,有些我可以毫无错误地检索它。这是异常消息:
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"
仅供参考:我可以通过 gmail 网络界面下载附件。
这是片段:
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();
}
}
}
任何人都有想法,如何解决这个问题?
从列表中已知的限制、错误、问题JavaMail 的:
某些 IMAP 服务器不实现
IMAP 部分 FETCH 功能
适当地。这个问题通常
表现为损坏的电子邮件附件
从以下位置下载大邮件时
IMAP 服务器。要解决此问题
服务器错误,设置
“mail.imap.partialfetch”属性
错误的。你必须设置这个
Properties 对象中的属性
您提供给您的会话。
所以你应该只是turn offimap 会话中的部分获取。例如:
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>");
source: https://javaee.github.io/javamail/docs/api/com/sun/mail/imap/package-summary.html
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)