File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes EJB and other Java EE Technologies and the fly likes Email Java object in memory Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "Email Java object in memory" Watch "Email Java object in memory" New topic

Email Java object in memory

Chris Wang
Ranch Hand

Joined: Jan 13, 2004
Posts: 34

I got an exception:
javax.mail.MessagingException: IOException while sending message;
nested exception is:
javax.activation.UnsupportedDataTypeException: no object DCH for MIME type audio/x-wav
at com.sun.mail.smtp.SMTPTransport.sendMessage
when I tried to email a java object "InputStream" as an attachment.
The code segment is as follows:

Multipart multipart = new MimeMultipart();
// Create the message part
BodyPart messageBodyPart = new MimeBodyPart();
// Fill the message
// Part two is attachment
messageBodyPart = new MimeBodyPart();
//leave_message_InputStream is of
DataHandler dh = new DataHandler(leave_message_InputStream, "audio/x-wav");
catch (MessagingException mex)
//Here I send the email ...

In TOMCAT/common/lib I put the lastest mail.jar(JavaMail 1.4) and activation.jar(JavaBean Activation Framework 1.1).

If I save leave_message_InputStream to a file and then use the file as the Datasource of DataHandler, the updated code listing below worked fine:

final URL url = new URL(url of the file);
DataSource source = new URLDataSource(url);
DataHandler dh = new DataHandler(source);

For performance consideration, I don't need to save the java object to a file and then read it and delete it.

Any help is appreciated!

Paul Clapham

Joined: Oct 14, 2005
Posts: 19848

Since you have JavaMail 1.4, you could use a javax.mail.util.ByteArrayDataSource based on that input stream.
Chris Wang
Ranch Hand

Joined: Jan 13, 2004
Posts: 34
When I changed the code to:
javax.mail.util.ByteArrayDataSource bds =
new javax.mail.util.ByteArrayDataSource(leave_message_InputStream, "audio/x-wav");
DataHandler dh = new DataHandler(bds);
The excption was gone. But bds object is empty, no audio data.
leave_message_InputStream is of com.oreilly.servlet.multipart.PartInputStream and has audio data inside.

Could you please show me the code how to use leave_message_InputStream to build ByteArrayDataSource object?


Chris Wang
Ranch Hand

Joined: Jan 13, 2004
Posts: 34

The problem is fixed. Just want to share the idea here.
The leave_message_inputStream has been read previously and can NOT "rewind", so when I tried to construct the new ByteArrayDataSource usng leave_message_inputStream, bds is empty.

I agree. Here's the link:
subject: Email Java object in memory
It's not a secret anymore!