How do I get the text of a mail with attachments?
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Fenny_Yu
Posted On:   Thursday, March 29, 2001 12:16 AM

I can get the text of a mail without attachment.And I can get the attachments of a mail with attachments.But I can't get the text of a mail with attachments.How do I get it?

Re: How do I get the text of a mail with attachments?

Posted By:   sharona_f  
Posted On:   Thursday, March 29, 2001 02:50 AM

no shortcuts - you need to loop over the attachments and get the text.

attached is a code that does that .

Good Luck

private String getEmailContentStr(Part p)
{
String mailContent = "";

if (p.isMimeType("text/plain"))
{
if (p.getContent() == null)
{
return mailContent;
}
try
{
mailContent = (String)p.getContent();
}
catch (UnsupportedEncodingException uex)
{
InputStream is = p.getInputStream();
/*
* Read the input stream into a byte array.
* Choose a charset in some heuristic manner, use
* that charset in the java.lang.String constructor
* to convert the byte array into a String.
*/
mailContent = is.toString();
}
}
else if (p.isMimeType("multipart/*"))
{
Multipart mp = (Multipart)p.getContent();
int count = mp.getCount();
printLog("Count :" + count);
for (int i = 0; i < count; i++)
mailContent = mailContent + (getEmailContentStr(mp.getBodyPart(i)));
}
else if (p.isMimeType("message/rfc822"))
{
getEmailContentStr((Part)p.getContent());
}
else
{
printLog("This is an unknown type");
}
return mailContent;
}
catch(Exception ex)
{
printLog("Getting content mail FAILED");
ex.printStackTrace();
return mailContent;
}

} //end getEmailContentStr
About | Sitemap | Contact