Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java Mail - Getting SharedByteArrayInputStream object from getContent() method of Message object

 
Ghulam Rasool Raja
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I wrote a little program which downloads emails, using JavaMail API. I use the method getContent() of message object to get the content of the message,
I handled two conditions while reading the email message content..

1- I can either get an object of type String (if the email message is just a plain text) or
2- I can get an object of type Multipart (if the email has attachments)

code looks something like this


it was working fine, now we are in process of changing the email server, previously it was IBM Domino (running on windows), and we are planning to shift to SendMail (running on unix), But the problem that I am facing now, is that I am only getting an object of type javax.mail.util.SharedByteArrayInputStream from getContent() method of Message object. How do I convert this input stream to the expected format ? Tried using MimeBodyParty object, but was not all the helpful. Any pointers, suggestions will be greatly appreciated.
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use the InputStreamReader class read string data from an InputStream (which SharedByteArrayInputStream extends). You'll need to pay attention to which encoding is used; you can probably find that in one of the mail headers.
 
Ghulam Rasool Raja
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ulf,

and how about the mails which contain both text and attachments ?

 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In that case the object returned should be an instance of Multipart. Form the javadocs of MimeMessage.getContent:
Return the content as a Java object. The type of this object is dependent on the content itself. For example, the native format of a "text/plain" content is usually a String object. The native format for a "multipart" message is always a Multipart subclass. For content types that are unknown to the DataHandler system, an input stream is returned as the content.

You may want to use the getContentType() method to handle the different cases.
 
Ghulam Rasool Raja
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well yes, it should have, but it is not - It is returning an object of type SharedByteArrayInputStream. Furthermore, I observed one more thing, whether I am sending just a plain message, or a message with attachments, I am getting the object of SharedByteArrayInputStream.
Any pointers will be appreciated, thanks for your time.
 
Ghulam Rasool Raja
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried removing activation.jar from my classpath too. Did not work.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic