File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Other JSE/JEE APIs and the fly likes Reading mail content 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 "Reading mail content" Watch "Reading mail content" New topic
Author

Reading mail content

harshada patil
Ranch Hand

Joined: Mar 12, 2011
Posts: 96


I wrote above code for reading mail content, but i'm failed to read the mail content as bodyPart.getContent().toString() gives object name and logger.info(bodyPart.getContent()) gives actual html content that i want. How do i get that content as string object ?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18127
    
    8

If you can cast the getContents() object to an InputStream, then read from that InputStream.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19543
    
  16

I would actually prefer to use Part's getInputStream() method. getContent() could return a String if the attachment is a text file (I've seen this happen a lot) or a Message if it's an embedded (forwarded) message (don't know if that would show up as an attachment though). Both Message and BodyPart implement Part so the method is available for you.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
harshada patil
Ranch Hand

Joined: Mar 12, 2011
Posts: 96

No I'm getting HTML content in body, but when i'm using that contents to display as a web page, it displays without images.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18127
    
    8

I'm not at all surprised. The HTML code you're talking about would have to have proper links to the images, which a browser can resolve. No doubt yours doesn't.

Of course if you had posted the HTML which you had the question about, we wouldn't have to guess.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19543
    
  16

It's also possible the HTML body includes embedded images. They are <img> tags with a source that starts with "cid:", and the value after that matches the Content-ID of one of the attachments. What I've done for one project is use a Pattern / Matcher pair to find all such sources, find the matching attachments, extract these attachments to a web server's file system and then replace the source to the location of the attachment on the web server. I can't give you any more details though as my employer won't allow me to share that code.
harshada patil
Ranch Hand

Joined: Mar 12, 2011
Posts: 96
Thanks Rob. That is what exactly i want to know. Please tell me more about " extracting attachments to a web server's file system " if possible.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19543
    
  16

Well, just copy the attachment contents to a file that is accessible through a web server.
harshada patil
Ranch Hand

Joined: Mar 12, 2011
Posts: 96
I want to read whole email content (message + attachments) and as there are various Content types, What is the good way to read email read email ?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18127
    
    8

In your JavaMail download you will find a "demo" folder. It contains all kinds of basic examples including one for reading the parts of an e-mail message. So start with that example and modify it to suit your requirements.
harshada patil
Ranch Hand

Joined: Mar 12, 2011
Posts: 96
If web server is not accessible, is there any other way to send images to application for displaying them ?
harshada patil
Ranch Hand

Joined: Mar 12, 2011
Posts: 96
This is a part of code i am using for reading email, if email contains multiple images then in some cases code is reading images in different sequence from one that appears in email, Is it possible to read images in the correct sequence ?



 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Reading mail content
 
Similar Threads
JavaMail:Why can't a recipient using Lotus Notes see the attachment of my mail
email attachment
JavaMail set content to utf-8
Java Mail - Getting SharedByteArrayInputStream object from getContent() method of Message object
Getting Multiple Attachments from MIMEMessage