This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes JSP and the fly likes Java Mail receive Problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Java Mail receive Problem" Watch "Java Mail receive Problem" New topic
Author

Java Mail receive Problem

Kelly So
Greenhorn

Joined: Mar 27, 2001
Posts: 20
Hi all,
I want to receive a email from Outlook to my email server,
I have using HTML text to write the email, and add a attachment to it. After that, I write a java class to get the email content.
However, I can get the attachment but cannot the get the main body text.
This maybe due to the isMimeType = "multipart/alternative"
I can only get code below if I print
System.out.print(p.getContent().toString());
javax.mail.internet.MimeMultipart@1c74f371
How can I get the main body text and attachment?
Many thanks!
George Joseph
Greenhorn

Joined: Nov 05, 2001
Posts: 24
A multipart/alternate mail contains many part which are essential same in content.
While using outlook if we compose mail as html the actully two parts/versions of mail are sent one plain text two html part, so that mail clients which are unable view html message.
Usually the mail client sets the most advanced form (html or rich text) as the last part in the "multipart/alternate", so we need only to view the last (advanced) part only and discard other parts.

George Joseph,
Product Development Group,
Transversal e Networks
[ November 07, 2002: Message edited by: George Joseph ]
Kelly So
Greenhorn

Joined: Mar 27, 2001
Posts: 20
Hi George,
Here is my code :
Multipart multipart = (Multipart)message.getContent();
for(int i = 0;i < multipart.getCount();i++)
{

Part p = multipart.getBodyPart(i);
System.out.println(p.getContentType());
if(p.isMimeType("text/plain") || p.isMimeType("text/html"))
{
content = p.getContent().toString();
System.out.println(content);
}
else if (p.isMimeType("multipart/alternative"))
{

Multipart mp = (Multipart)message.getContent();
int partsCount = mp.getCount();
//IN CASE OF MULTIPART MAIL WE CAN TAKE ONLY THE BEST ALTERNATE PART
//WHICH HAS THE HIGHEST SEQUENCE NUMBER
System.out.println((String)mp.getBodyPart(partsCount -1).getContent());

}
else
{

String filename = p.getFileName();
File file;
if (filename!=null){
file = new File(TargetFolder+filename);
InputStream is = p.getInputStream();
// Open an output stream on the file
FileOutputStream fos = new FileOutputStream(file);
// Write each byte from the input stream to the file
int j;
while ((j = is.read()) != -1)
{
fos.write(j);
}
// Close the file for output
fos.close();
}
}
}

And the result comes :
multipart/alternative;
boundary="----=_NextPart_001_0006_01C28731.47E173A0"
java.lang.ClassCastException
I really don't know why I cannot get my main body text !
Really really thanks for your help!
Kelly
George Joseph
Greenhorn

Joined: Nov 05, 2001
Posts: 24
Hi Kelly,

I forgot to tell you that the else case that is (MimeType!=text && MimeType!=multipart/alternative) can not be assumed to be an attachement.
It can be further be broken down and also some of the Parts needs to be recursiveley parsed.
It is better you write a method to parse the enitre mail content and this method must call itself recursively
The method can be called by passing the messageObject as Parameter.
parseMail(messageObject);

George Joseph,
Product Development Group,
Transversal e Networks
[ November 08, 2002: Message edited by: George Joseph ]
Kelly So
Greenhorn

Joined: Mar 27, 2001
Posts: 20
Dear George,
Thanks so much for your help!
It works!
Kelly
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java Mail receive Problem