We help IT Professionals succeed at work.

JavaMail - How to read a text-formatted mail  and get the msg body

woodmouse347
woodmouse347 asked
on
443 Views
Last Modified: 2013-11-24
I have recd a mail with content type as text/plain. When I open it in yahoo-mail, i am getting the mail formatted properlly(like with hyperinks) but if i try to use pop3 and fetch the mail the formatting is lost. How should I identify this mail and get the formatting properly.

Thanks.
Comment
Watch Question

Commented:
It's maybe the charset problem.

try this

String text = "";
Message message = folder.getMessage(msgnum);
Multipart mp = (Multipart)message.getContent();
int count = mp.getCount();
for(int i = 0; i < count; i++) {
    String contentType = p.getContentType();
    if( contentType.equals("text/plain") ) {
        text = "" + p.getContent();
        text = new String(text.getBytes("ISO8859-1"));
    }
}

-gnoon-
CERTIFIED EXPERT
Top Expert 2016

Commented:
The fact that it's formatted properly (i guess you mean working hyperlinks) in the mail reader is probably due to functionality of that reader. If it's in plain text then it won't have inherent working hyperlinks in the message.
Top Expert 2006

Commented:
CEHJ is right, plain text does not support hyperlinks.  It's up to the client to create those when a certain pattern is found.

Author

Commented:
Well, then if I create a text in MS Word with mails ids and http reference and creat a mail, my mail reader should handle the text. Is that what u are saying?

If Yes,
what are the points I should take care when i do the smart mail reader? (eg, links for www. or http://; mailto tag for mail ids....)
CERTIFIED EXPERT
Top Expert 2016
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Top Expert 2006

Commented:
if you use JRE1.4, you can use regular expressions to replace your pattern with a hyperlink:
it will be something like this:

String message  // variable containing the message in plain text
String hyperPattern = "http://[^ ]*";
String replacePattern = "<a href=\"$0\">$0</a>"
message.replaceAll(hyperPattern, replacePattern);
CERTIFIED EXPERT
Top Expert 2016

Commented:
Aw come on Grand - no combined mailto: replace? ;-)

>>$0

should be $1

Author

Commented:
Thank u every one for helping me.
CERTIFIED EXPERT
Top Expert 2016

Commented:
:-)
Top Expert 2006

Commented:
>> Aw come on Grand - no combined mailto: replace? ;-)
:-)

>> should be $1
no, it should be $0.
$0 represent the entire match.
if you want to use $1, you need to use a capturing group:
String hyperPattern = "(http://[^ ]*)";
CERTIFIED EXPERT
Top Expert 2016

Commented:
>>no, it should be $0.

Thanks. Never seen $0 before
Top Expert 2006

Commented:
you are welcome CEHJ.
:-)
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.