如何使用InputStream和Spring发送带有附件的电子邮件?

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

情况是这样的:

首先,我们在内存中生成一个文件,我们可以获得一个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,它为每个流创建一个新的流致电。

java spring email inputstream
5个回答
56
投票

对于在内存中生成的文件,您可以使用ByteArrayResource。只需使用InputStream库中的IOUtils转换IOUtils对象。

非常简单:

Apache Commons IO

6
投票

请参阅弹簧参考章节helper.addAttachment("attachement", new ByteArrayResource(IOUtils.toByteArray(inputStream)));

示例来自那里,我想它确实要您做:

24.3 Using the JavaMail MimeMessageHelper

1
投票

您可以根据需要简单地实现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);

0
投票

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

0
投票

工作示例是:

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); } 方法中返回InputStreamSourcetrue实现,则可能会出现此消息。

方法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
© www.soinside.com 2019 - 2024. All rights reserved.