Extract parts from a Multipart/Related email using Java Mail API

E

Edwinah63

Hi everyone,

I would some advice/code on how to extract the various parts from a
multipart/related email in order to save the correct parts to the
correct database tables.

I need to extract the text portion of a multipart/related email and
save it to one table, and the attached image/BLOB part of it to
another related table.

For example:

I have no problem extracting the text from any kind email with
*external attachments only*. I have no trouble extracting any kind of
external attachment....however....

if I process an email with text and in-line image file which is
multipart/related

I get this:

------_=_NextPart_002_01C7FA7E.166DF35A Content-Type: text/html;
charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable
HERE IS THE=20 TEXT I WANT



------_=_NextPart_002_01C7FA7E.166DF35A Content-Type: image/bmp;
name="Outlook.bmp" Content-Transfer-Encoding: base64 Content-ID:
<890092905@19092007-127d> Content-Description: Outlook.bmp Content-
Location: Outlook.bmp
Qk0OiQAAAAAAADYAAAAoAAAAyQAAADoAAAABABgAAAAAANiIAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAl2YAilwAilwAilwAilwAilwAilwAilwAilwAilwA
ilwAilwAilwAilwAilwAilwAilwAilwAilwAilwAilwAilwAilwAilwAilwAilwAilwAilwAilwA
ilwAilwAilwAilwAilwAilwAilwAilwAilwAilwAilwAilwAilwAilwAilwAilwAilwAilwAilwA
ilwAilwAilwAilwAilwAilwAilwAilwAilwAilwAilwAilwAilwAilwAilwAilwAilwAilwAilwA

(part of much longer file)


in a single BLOB not the text "HERE IS THE TEXT I WANT" as a separate
item and the image (JavaWorld logo) as another.

Any and all help appreciated.

Edwina
 
D

derek

Well i believe you would start by looking in the headers to find
out what the sections are breaking on, then just split it by the
section break. Then each section will have a header, so, look
at each sections header to see what mime type it is, etc.

Was there a specific question on how to do this?
Or are you unsure of the format of an smtp message?
Or was the question something else?
 
G

Greg R. Broderick

Hi everyone,

I would some advice/code on how to extract the various parts from a
multipart/related email in order to save the correct parts to the
correct database tables.

1. use javax.mail.Message.getContent() to get the message's content. This
should return the entire message's content, in an object of type
javax.mail.Multipart.

2. use the methods on java.mail.Multipart to retrieve a particular part of
the message. This should be encapsulated in an object of type
javax.mail.BodyPart.

3. use the methods on javax.mail.BodyPart to retrieve the content of the
particular part of the message that you're interested in.

Note that MIME multipart message parts may contain other MIME multipart
message parts.

For a better technical understanding of the MIME multipart message mechanism,
see RFCs 2045 - 2049.

Cheers!


--
---------------------------------------------------------------------
Greg R. Broderick (e-mail address removed)

A. Top posters.
Q. What is the most annoying thing on Usenet?
---------------------------------------------------------------------
 
M

Martin Gregorie

Edwinah63 said:
Hi everyone,

I would some advice/code on how to extract the various parts from a
multipart/related email in order to save the correct parts to the
correct database tables.
Look at the example code in the JavaMail API Design Specification (first
example, Appendix B).
 
R

Roedy Green

Hi everyone,

I would some advice/code on how to extract the various parts from a
multipart/related email in order to save the correct parts to the
correct database tables.

The key is MimeMultipart which describes the whole message, and
BodyPart which describes one partition of the message.

I suspect you have nested structure, with a MimeMultipart as one of
the BodyParts of the entire message. You can then take it apart into
BodyParts.

You can have a look at the code Bulk.java in
http://mindprod.com/products1.html#BULK

for how I take a MimeMultipart message apart into BodyParts (but not
nested).

It might help to study the structure of the message with a protocol
sniffer to see if it has a simple nesting.

http://mindprod.com/jgloss/sniffer.html
 
E

Edwinah63

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 = p;
i++;
}

} //end for

}
catch(Exception e){e.printStackTrace();}
finally
{
//add array attachments
nt.attachments = pa;

//clean up
p = null;
pa = null;
}
} //processMultipartContent


}
 
E

Edwinah63

Hi Martin,

Thanks for the link, this is a tidier/smarter version of my code
below.

Edwina
 
G

Greg R. Broderick

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

Use the various getHeaders() methods (e.g. getAllHeaders()) of the
javax.mail.BodyPart class. See also javax.mail.Header in the javadoc.

Cheers!
GRB

--
---------------------------------------------------------------------
Greg R. Broderick (e-mail address removed)

A. Top posters.
Q. What is the most annoying thing on Usenet?
---------------------------------------------------------------------
 
E

Edwinah63

Hi everyone,

Many thanks again to everyone for their replies. Now I know what to
research (getHeader() method) I hopefully with be able to get on with
my app.

:)

Edwina
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top