JavaMail, FileDataSource : how to send attachment w/out a physical file

B

briforge

I've seen the samples for JavaMail that illustrate how to send an
attachment using FileDataSource. However, the constructors for
FileDataSource require either a java.io.File, or a String which is
resolved as the pathname to a file.

However, I have a a stream of data which I need to convert to a file,
basically an in-memory file, so that I can pass it to the
FileDataSource constructor.

What I need to do is email a PDF as an attachment. I receive the PDF as
a response using HttpClient. So I could have a string instead of a
stream. Either way, how can I get this into FileDataSource?
 
J

James McGill

What I need to do is email a PDF as an attachment. I receive the PDF as
a response using HttpClient. So I could have a string instead of a
stream. Either way, how can I get this into FileDataSource?

If you already have the data in a String or a Stream, you don't need a
DataSource to build your MimeBodyPart. You can go straight to the
MimeMultipart object and add the type and content directly to it.

The DataSource stuff is just a convenience for doing that with data from
a file. Something like this perhaps:

String pdf_data
MimeMultipart content = new MimeMultipart("alternative");
MimeBodyPart pdf = new MimeBodyPart();
pdf.setContent(pdf_data, "application/pdf");
content.addBodyPart(pdf);
 
R

Roedy Green

What I need to do is email a PDF as an attachment. I receive the PDF as
a response using HttpClient. So I could have a string instead of a
stream. Either way, how can I get this into FileDataSource?

Presumably FileDataSource implements some interface possibly called
DataSource. You don't want a FileDataSource. You want some other
flavour, perhaps something you concoct yourself from scratch the
implments the interface.
 
A

Alexander Avtanski

I've seen the samples for JavaMail that illustrate how to send an
attachment using FileDataSource. However, the constructors for
FileDataSource require either a java.io.File, or a String which is
resolved as the pathname to a file.

However, I have a a stream of data which I need to convert to a file,
basically an in-memory file, so that I can pass it to the
FileDataSource constructor.

What I need to do is email a PDF as an attachment. I receive the PDF as
a response using HttpClient. So I could have a string instead of a
stream. Either way, how can I get this into FileDataSource?


One way is not to use FileDataSource but to implement your own
DataSource. Something like this (here I have byte[] to store the
data, but you may have it as String or any other storage that is
appropriate in your case):


public class MyDataSource implements javax.activation.DataSource {

private byte[] data = new byte[0];
private String contentType = "text/plain";
private String name = null;

public void setData(byte[] data) {
this.data = data;
}

public void setContentType(String contentType) {
this.contentType = contentType;
}

public void setName(String name) {
this.name = name;
}

public InputStream getInputStream() {
return new ByteArrayInputStream(data);
}

public OutputStream getOutputStream() {
return null;
}

public String getContentType() {
return contentType;
}

public String getName() {
return name;
}

}
 
J

James McGill

Presumably FileDataSource implements some interface possibly called
DataSource. You don't want a FileDataSource. You want some other
flavour, perhaps something you concoct yourself from scratch the
implments the interface.

To make a DataSource in order to accomplish this task (adding a MIME
attachement to a Message) is really going the long way around!

If you've already got the data, you just build the MIME parts directly
-- no need to cook up a convoluted path to make a piece of data appear
to be a file so that you can take some specific approach to make it into
a piece of data. That's just silly.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,836
Messages
2,569,748
Members
45,545
Latest member
rapter____0

Latest Threads

Top