情况是这样的:
首先,我们在内存中生成一个文件,我们可以获得一个InputStream
对象。其次,InputStream对象必须作为电子邮件的附件发送。语言是Java,我们使用Spring发送电子邮件。
我已经找到了很多信息,但是我找不到如何使用InputStream
发送电子邮件附件。我尝试这样做:
InputStreamSource iss= new InputStreamResource(new FileInputStream("c:\\a.txt"));
MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true, "UTF-8");
message.addAttachment("attachment", iss);
但是我得到一个例外:
传入资源包含一个开放流:无效参数。 Java邮件需要一个InputStreamSource,它为每个流创建一个新的流致电。
请参阅弹簧参考章节helper.addAttachment("attachement",
new ByteArrayResource(IOUtils.toByteArray(inputStream)));
示例来自那里,我想它确实要您做:
24.3 Using the JavaMail MimeMessageHelper
您可以根据需要简单地实现InputStreamSource,并在其中传递新鲜的InputStream:
JavaMailSenderImpl sender = new JavaMailSenderImpl();
sender.setHost("mail.host.com");
MimeMessage message = sender.createMimeMessage();
// use the true flag to indicate you need a multipart message
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setTo("[email protected]");
helper.setText("Check out this image!");
// let's attach the infamous windows Sample file (this time copied to c:/)
FileSystemResource file = new FileSystemResource(new File("c:/Sample.jpg"));
helper.addAttachment("CoolImage.jpg", file);
sender.send(message);
// inlineFileObjectCreated-您可以为示例创建StringBuilder对象
InputStreamSource iss = new InputStreamSource() {
@Override
public InputStream getInputStream() throws IOException {
// provide fresh InputStream
return new FileInputStream("c:\\a.txt");
}
}
MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true, "UTF-8");
message.addAttachment("attachment", iss);
//////////////////////////////////////////////
ByteArrayDataSource source = new ByteArrayDataSource("file name", "contentType", inlineFileObjectCreated.getBytes() );
JavaMailSender mailSender = (JavaMailSender) ServicesHome.getService("javaMailSender");
MimeMessage mimeMessage = mailSender.createMimeMessage();
MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);
mimeMessageHelper.setTo(toArray);
mimeMessageHelper.setSubject("");
mimeMessageHelper.setText("");
mimeMessageHelper.addAttachment("filename", source);
mailSender.send(mimeMessageHelper.getMimeMessage());
工作示例是:
1)附件是import javax.activation.DataSource;
public class ByteArrayDataSource implements DataSource {
byte[] bytes;
String contentType;
String name;
public ByteArrayDataSource( String name, String contentType, byte[] bytes ) {
this.name = name;
this.bytes = bytes;
this.contentType = contentType;
}
public String getContentType() {
return contentType;
}
public InputStream getInputStream() {
return new ByteArrayInputStream(bytes);
}
public String getName() {
return name;
}
public OutputStream getOutputStream() throws IOException {
throw new FileNotFoundException();
}
}
接口
InputStreamSource
2)附件是public void send() throws IOException, MessagingException {
final ByteArrayOutputStream stream = createInMemoryDocument("body");
final InputStreamSource attachment = new ByteArrayResource(stream.toByteArray());
final MimeMessage message = javaMailSender.createMimeMessage();
final MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setSubject("subject");
helper.setFrom("[email protected]");
helper.setTo("[email protected]");
helper.setReplyTo("[email protected]");
helper.setText("stub", false);
helper.addAttachment("document.txt", attachment);
javaMailSender.send(message);
}
接口
DataSource
解释:
传入资源包含一个开放流:无效参数。JavaMail需要一个InputStreamSource来为它创建一个新的流每个电话。
如果开发人员使用在public void send() throws IOException, MessagingException {
final ByteArrayOutputStream document = createInMemoryDocument("body");
final InputStream inputStream = new ByteArrayInputStream(document.toByteArray());
final DataSource attachment = new ByteArrayDataSource(inputStream, "application/octet-stream");
final MimeMessage message = javaMailSender.createMimeMessage();
final MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setSubject("subject");
helper.setFrom("[email protected]");
helper.setTo("[email protected]");
helper.setReplyTo("[email protected]");
helper.setText("stub", false);
helper.addAttachment("document.txt", attachment);
javaMailSender.send(message);
}
方法中返回InputStreamSource
的true
实现,则可能会出现此消息。
方法isOpen()
中有一个特殊检查:
MimeMessageHelper#addAttacment()
[public void addAttachment(String attachmentFilename, InputStreamSource inputStreamSource, String contentType) {
//...
if (inputStreamSource instanceof Resource && ((Resource) inputStreamSource).isOpen()) {
throw new IllegalArgumentException(
"Passed-in Resource contains an open stream: invalid argument. " +
"JavaMail requires an InputStreamSource that creates a fresh stream for every call.");
}
//...
}
总是返回InputStreamResource#isOpen()
,这使得无法将此实现用作附件:
true