如何使用弹簧发送附件使用InputStream的电子邮件?

时间:2011-04-15 13:27:15

标签: java spring email inputstream

情况是这样的:

首先,我们在内存中生成一个文件,我们可以得到一个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);

但我们例外:

  

Passed-in Resource包含一个开放流:无效参数。 JavaMail需要一个InputStreamSource,为每次调用创建一个新流。

5 个答案:

答案 0 :(得分:50)

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

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

答案 1 :(得分: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("test@host.com");

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

答案 2 :(得分: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);

答案 3 :(得分: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();
        }
      }

答案 4 :(得分: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("from@from.com");
    helper.setTo("to@to.com");
    helper.setReplyTo("replyTo@replyTo.com");
    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("from@from.com");
        helper.setTo("to@to.com");
        helper.setReplyTo("replyTo@replyTo.com");
        helper.setText("stub", false);
        helper.addAttachment("document.txt", attachment);
        javaMailSender.send(message);
    }

说明:

  

传入的资源包含一个开放的流:无效的参数。   JavaMail需要一个InputStreamSource来为它创建一个新的流   每个电话。

如果开发人员使用InputStreamSource方法中返回true的{​​{1}}实现,则会出现此消息。

方法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
相关问题