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

Discussion in 'Java' started by briforge@gmail.com, Mar 1, 2006.

  1. Guest

    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?
     
    , Mar 1, 2006
    #1
    1. Advertising

  2. James McGill Guest

    Re: JavaMail, FileDataSource : how to send attachment w/out aphysical file

    On Wed, 2006-03-01 at 12:59 -0800, 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);
     
    James McGill, Mar 1, 2006
    #2
    1. Advertising

  3. Roedy Green Guest

    On 1 Mar 2006 12:59:09 -0800, 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.
     
    Roedy Green, Mar 1, 2006
    #3
  4. Re: JavaMail, FileDataSource : how to send attachment w/out a physicalfile

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

    }
     
    Alexander Avtanski, Mar 1, 2006
    #4
  5. James McGill Guest

    Re: JavaMail, FileDataSource : how to send attachment w/out aphysical file

    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.
     
    James McGill, Mar 1, 2006
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ad
    Replies:
    0
    Views:
    493
  2. Sergio Juan
    Replies:
    0
    Views:
    523
    Sergio Juan
    Sep 12, 2003
  3. barry
    Replies:
    0
    Views:
    13,292
    barry
    Dec 12, 2003
  4. shilpa
    Replies:
    5
    Views:
    6,148
    akila_natarajan
    May 12, 2011
  5. Adam Lipscombe

    How to set attachment name with JavaMail

    Adam Lipscombe, May 21, 2007, in forum: Java
    Replies:
    1
    Views:
    2,077
    voorth
    May 21, 2007
Loading...

Share This Page