Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Email Java object in memory

 
Chris Wang
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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();
try
{
// Create the message part
BodyPart messageBodyPart = new MimeBodyPart();
// Fill the message
messageBodyPart.setText("");
multipart.addBodyPart(messageBodyPart);
// Part two is attachment
messageBodyPart = new MimeBodyPart();
//leave_message_InputStream is of java.io.InputStream
DataHandler dh = new DataHandler(leave_message_InputStream, "audio/x-wav");
messageBodyPart.setDataHandler(dh);
messageBodyPart.setFileName(recordingFileName_show);
multipart.addBodyPart(messageBodyPart);
}
catch (MessagingException mex)
{
mex.printStackTrace();
}
//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!

Chris
 
Paul Clapham
Sheriff
Posts: 21298
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since you have JavaMail 1.4, you could use a javax.mail.util.ByteArrayDataSource based on that input stream.
 
Chris Wang
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
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?

Thanks,

Chris
 
Chris Wang
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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.

Chris
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic