Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / March 2006

Tip: Looking for answers? Try searching our database.

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

Thread view: 
briforge@gmail.com - 01 Mar 2006 20:59 GMT
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?
James McGill - 01 Mar 2006 21:22 GMT
> 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);
Roedy Green - 01 Mar 2006 21:28 GMT
>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.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

James McGill - 01 Mar 2006 22:34 GMT
> 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.
Alexander Avtanski - 01 Mar 2006 21:33 GMT
> I've seen the samples for JavaMail that illustrate how to send an
> attachment using FileDataSource. However, the constructors for
[quoted text clipped - 8 lines]
> 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;
    }

}


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.