aspose file tools*
The moose likes Other JSE/JEE APIs and the fly likes Java Mail - Getting SharedByteArrayInputStream object from getContent() method of Message object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Bookmark "Java Mail - Getting SharedByteArrayInputStream object from getContent() method of Message object" Watch "Java Mail - Getting SharedByteArrayInputStream object from getContent() method of Message object" New topic
Author

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

Ghulam Rasool Raja
Greenhorn

Joined: Mar 10, 2007
Posts: 10
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
Marshal

Joined: Mar 22, 2005
Posts: 42610
    
  65
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.


Ping & DNS - my free Android networking tools app
Ghulam Rasool Raja
Greenhorn

Joined: Mar 10, 2007
Posts: 10
Thanks Ulf,

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

Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42610
    
  65
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

Joined: Mar 10, 2007
Posts: 10
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

Joined: Mar 10, 2007
Posts: 10
I tried removing activation.jar from my classpath too. Did not work.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java Mail - Getting SharedByteArrayInputStream object from getContent() method of Message object