Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java Mail receive Problem

 
Kelly So
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear George,
Thanks so much for your help!
It works!
Kelly
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic