在尝试从 gmail 下载带有附件的邮件时,我的测试邮件包含一个文本文件作为附件。
附件部分正确返回文本附件内容类型,甚至文件名。但是附件 InputStream 上的循环条件永远不会非零。
经过一番反复试验,结果证明 text/plain 的内容可以使用部件的 getContent 方法获得(在下面介绍调用的情况下
att_mbp.getContent()
返回附加文本文件中的内容)
if (BodyPart.ATTACHMENT.equalsIgnoreCase(att_mbp.getDisposition())) {
att_mbp.getContentType();
// process each attachment
// read the filename
file = att_mbp.getFileName();
InputStream stream = att_mbp.getInputStream();
BufferedInputStream br = new BufferedInputStream(stream);
BufferedOutputStream bout = new BufferedOutputStream(new FileOutputStream(file));
while (br.available() > 0) {
// this loop is never executed for text/plain
bout.write(br.read());
}
bout.flush();
bout.close();
}
我的问题是 - 为什么文本/纯文本附件正文只能从 getContent() 获得,而不能从附加的 InputStream 实例获得?