public final String getPart(Part p) throws Exception {
if(p.isMimeType("text/html") || p.isMimeType("text/plain")){
if(!body.equals((String)p.getContent())){
logger.info("---- text/plain Message Part ----");
body = body + (String)p.getContent();}
}else if(p.isMimeType("multipart/*")){
logger.info("------- MultiPart Message -------");
Multipart mp = (Multipart)p.getContent();
level++;
int count = mp.getCount();
for (int i = 0; i < count; i++)
getPart(mp.getBodyPart(i));
level--;
}else if(p.isMimeType("message/rfc822")){
logger.info("--------- Nested Message --------");
level++;
getPart((Part)p.getContent());
level--;
}else{
Object o = p.getContent();
if (o instanceof String) {
logger.info("--------- Simple String ---------");
body += (String)o;
} else if (o instanceof InputStream) {
logger.info("---------- Inputstream ----------");
InputStream is = (InputStream)o;
int c;
while ((c = is.read()) != -1)
body += c;
} else {
logger.info("---------- Uknown Type ----------");
body += o.toString();
}
}
String filename = p.getFileName();
String disp = p.getDisposition();
if (level != 0 && disp != null ) {
if(disp.equalsIgnoreCase(Part.ATTACHMENT)){
if (filename == null)
filename = "";
try {
File f = new File(filename);
if (!f.exists()){
((MimeBodyPart)p).saveFile(f);
}
body += "Επισυναπτόμενο: <a href=\"" + f.getAbsolutePath() + "\">" + filename + "</a>";
} catch (IOException ex) {
logger.error("Failed to save attachment: " + ex);
}
}
}
return body;
}