Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 526 Vote(s) - 3.59 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to send email with attachment using InputStream and Spring?

#1
The situation is like this:

First, we generate a file in the memory, we can get a `InputStream` object.
Second the *InputStream* object must be send as a attachment of a email. The language is Java, we use Spring to send email.

I have found a lot of information, but I cannot find how to send an email attachment using `InputStream`. I try to do like this:

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

But I get an exception:


> Passed-in Resource contains an open stream: invalid argument. JavaMail
> requires an InputStreamSource that creates a fresh stream for every
> call.

Reply

#2
You can make simple implementation of InputStreamSource and pass fresh InputStream in it, as requested:

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

#3
//inlineFileObjectCreated -- you can create a StringBuilder Object for a example

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

Reply

#4
The working examples are:

1) Attachment is an `InputStreamSource` interface

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) Attachment is an `DataSource` interface

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

**The explanation:**

> Passed-in Resource contains an open stream: invalid argument.
> JavaMail requires an InputStreamSource that creates a fresh stream for
> every call.

This message could appear if the developer use an implementation of `InputStreamSource` that return `true` in the `isOpen()` method.

There is a special check in the method `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()` always return `true` that makes impossible to use this implementation as an attachment:

public class InputStreamResource extends AbstractResource {
//...
@Override
public boolean isOpen() {
return true;
}
//...
}
Reply

#5
For files generated *in memory*, you may use `ByteArrayResource`. Just convert your `InputStream` object using [`IOUtils`][1] from the [*Apache Commons IO*][2] library.

It is quite simple:

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


[1]:

[To see links please register here]

[2]:

[To see links please register here]

Reply

#6
Have a look at the spring reference chapter [24.3 Using the JavaMail MimeMessageHelper][1]

The example is from there, I think it do want you want to do:

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 resource = new FileSystemResource(new File("c:/Sample.jpg"));

helper.addAttachment("CoolImage.jpg", resource );

sender.send(message);

if you want to use a Stream, then you can use

ByteArrayResource resource = new ByteArrayResource(IOUtils.toByteArray(inputStream)));

instead of FileSystemResource


[1]:

[To see links please register here]

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through