如何使用Spring发送电子邮件附件一起使用的InputStream?

问题描述 投票:24回答:5

情况是这样的:

首先,我们生成的存储器中的文件,我们可以得到一个InputStream对象第二,InputStream对象必须为发送电子邮件的附件...的语言是Java,我们使用Spring来发送电子邮件。

我找了很多,但我不能找到如何发送电子邮件附件使用的InputStream ...我试着这样做:

InputStreamSource iss = null;
                    iss = new InputStreamResource(new FileInputStream("c:\\a.txt"));
MimeMessageHelper message = new MimeMessageHelper(mimeMessage,
                        true, "UTF-8");
message.addAttachment("attachment", iss);

但我们例外:

传入资源包含一个打开的流:无效的参数。 JavaMail的要求,致力于为每个调用一个新的流的InputStreamSource。

java spring email inputstream
5个回答
52
投票

在内存生成的文件,你可以使用使用ByteArrayResource。只需使用IOUtils Apache的共享转换您的InputStream对象。这是很简单的:

    helper.addAttachment("attachement",
    new ByteArrayResource(IOUtils.toByteArray(inputStream)));

6
投票

看看春季参考章节24.3 Using the JavaMail MimeMessageHelper

这个例子就是从那里,我认为这确实想你想做的事:

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);

1
投票

你可以简单的实现InputStreamSource的,并通过新的InputStream在它的要求:

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);

0
投票

// inlineFileObjectCreated - 您可以为例如创建一个StringBuilder对象

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());

/////////////////////////////////////////////

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();
        }
      }

0
投票

工作的例子是:

1)附件是InputStreamSource接口

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);
}

2)附件是DataSource接口

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);
    }

说明:

传入资源包含一个打开的流:无效的参数。 JavaMail的要求,致力于为每个调用一个新的流的InputStreamSource。

可能出现此消息如果开发者使用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,使得不可能使用这种实现作为附件:

public class InputStreamResource extends AbstractResource {
   //...
   @Override
   public boolean isOpen() {
      return true;
   }
   //...
}
© www.soinside.com 2019 - 2024. All rights reserved.