Re: Extract parts from a Multipart/Related email using Java Mail API
Hi everyone,
Thanks for everyone's replies.
I should have included a code snippet - I do know how to pull apart
emails according to their mime body part, but my question is how to
further extract the various headers (thanks to Derek above for this
suggestion) a body part that is coded multipart/related - the email
body contains both Text and an embedded Image
code snippet (apologies how it pasted):
BTW what is that post with all the links in it by arabzwaj50?)
//logon to pop3 server
//connect to store
//open inbox folder
Message messages[] = inboxFolder.getMessages();
for (int i=0, n=messages.length; i<n; i++)
{
Object content = messagePart.getContent();
if (content instanceof Multipart)
{
processMultipartContent(content, myNote); //myNote is object to
hold parts/subject etc
}
else
{
//not multipart email
String contentType = messagePart.getContentType();
if (contentType.startsWith("text/plain") ||
contentType.startsWith("text/html"))
{
myNote.setMsgBody((String) messagePart.getContent());
}
}
//save myNote to database etc
//process the multipart - works for everything except text with inline
object eg company email with text and company logo in body of email
private static void processMultipartContent(Object content, Note nt)
{
Part[] pa = null;
int i = 0;
Part p = null;
try
{
//loop through body parts
Multipart mp = (Multipart) content;
pa = new Part[mp.getCount()];
for(int j = 0; j< mp.getCount(); j++ )
{
p = mp.getBodyPart(j);
System.out.println(p.getContentType());
// this code works fine except for emails with inline
attachements and messages
if (p.getContentType().startsWith("text/plain") ||
p.getContentType().startsWith("text/html"))
{
nt.setMsgBody(nt.getMsgBody() + " " + (String)
p.getContent());
}
else //save all other mime content types to directory
{
//this is where the body of multipart/related email is
getting put together into a single part at the moment. This is where I
need help on how to loop thru multipart/related headers(?)
pa[i] = p;
i++;
}
} //end for
}
catch(Exception e){e.printStackTrace();}
finally
{
//add array attachments
nt.attachments = pa;
//clean up
p = null;
pa = null;
}
} //processMultipartContent
}