wrote:
> 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;
}
}