Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > JavaMail, FileDataSource : how to send attachment w/out a physical file

Reply
Thread Tools

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

 
 
briforge@gmail.com
Guest
Posts: n/a
 
      03-01-2006
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?

 
Reply With Quote
 
 
 
 
James McGill
Guest
Posts: n/a
 
      03-01-2006
On Wed, 2006-03-01 at 12:59 -0800, http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

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




 
Reply With Quote
 
 
 
 
Roedy Green
Guest
Posts: n/a
 
      03-01-2006
On 1 Mar 2006 12:59:09 -0800, (E-Mail Removed) wrote, quoted or
indirectly quoted someone who said :

>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.
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
 
Reply With Quote
 
Alexander Avtanski
Guest
Posts: n/a
 
      03-01-2006
(E-Mail Removed) 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;
}

}
 
Reply With Quote
 
James McGill
Guest
Posts: n/a
 
      03-01-2006
On Wed, 2006-03-01 at 21:28 +0000, Roedy Green wrote:
>
> 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.



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Scan - to file vs blank email and attachment vs Send to - Bobb - Computer Information 15 08-22-2012 12:02 PM
Cannot send an MP3 file as email attachment... juliekcf@gmail.com Java 3 12-11-2006 03:55 PM
How to send .zip the file as a soap attachment? ad ASP .Net 0 11-05-2005 10:25 PM
Save the Bandwidth -smarter way to send the same file attachment to N users? KK Perl Misc 3 02-15-2004 08:17 AM
javax.mail.internet -> Changing the name of a file send as attachment. Sergio Juan Java 0 09-12-2003 02:41 PM



Advertisments