我试图通过Spring 3的MailSender使用JavaMail发送附件,但我一直收到此错误:
org.springframework.mail.MailPreparationException: Could not prepare mail; nested exception is java.lang.IllegalArgumentException: Passed-in Resource contains an open stream: invalid argument. JavaMail requires an InputStreamSource that creates a fresh stream for every call.
我发现并关注其他人'关于此的建议(例如,how to use spring send email with attachment use InputStream?),但无法弄清楚我InputStream
开放的原因。我尝试了几种不同的变体来为附件创建InputStreamSource
(这是String
)并且两种方法都存在同样的问题。
接近1:
message.addAttachment(
filenameBase + "_" + DATE_FORMAT.format(new Date()) + ".csv",
new InputStreamSource() {
@Override
public InputStream getInputStream() throws IOException {
return IOUtils.toInputStream(content);
}
});
方法2:
message.addAttachment(
filenameBase + "_" + DATE_FORMAT.format(new Date()) + ".csv",
new ByteArrayResource(content.getBytes()));
答案 0 :(得分:0)
最后看起来这不是问题 - 有另一个附件调用没有正确使用InputStreamSource(这看起来有点奇怪,因为行号与这一行排成一行)......