Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / September 2007

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Edwinah63 - 19 Sep 2007 17:17 GMT
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
derek - 19 Sep 2007 18:05 GMT
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?
Greg R. Broderick - 19 Sep 2007 18:57 GMT
> 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!

Signature

---------------------------------------------------------------------
Greg R. Broderick                  usenet200709@blackholio.dyndns.org

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

Martin Gregorie - 19 Sep 2007 19:35 GMT
> 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).

Signature

martin@   | Martin Gregorie
gregorie. | Essex, UK
org       |

Edwinah63 - 20 Sep 2007 07:13 GMT
Hi Martin,

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

Edwina
Roedy Green - 20 Sep 2007 00:18 GMT
>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

Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

arabzwaj50@gmail.com - 20 Sep 2007 03:41 GMT
http://arabwazaef.com/arab/
                                                                 

                       
http://www.arabzwaj.com/welcome/viewtopic.php?t=2417&sid=eedb9c67db690f3d5fa9cd2
7e6f93e7d

                 

http://www.arabzwaj.com/welcome/viewtopic.php?t=2416&sid=eedb9c67db690f3d5fa9cd2
7e6f93e7d


                  .....                

http://www.arabzwaj.com/welcome/viewtopic.php?t=2423&sid=eedb9c67db690f3d5fa9cd2
7e6f93e7d

                                                     
http://www.arabzwaj.com/welcome/viewtopic.php?t=2422&sid=eedb9c67db690f3d5fa9cd2
7e6f93e7d

               
http://www.arabzwaj.com/welcome/viewtopic.php?t=2418&sid=eedb9c67db690f3d5fa9cd2
7e6f93e7d


                       !!!            
http://www.arabzwaj.com/welcome/viewtopic.php?t=2367&sid=6c6614c5f9dd9b9eaa8c035
5e13e8f9c

                                                 

http://www.arabzwaj.com/welcome/viewtopic.php?t=2332
                                                   ...!

http://www.arabzwaj.com/welcome/viewtopic.php?p=11408#11408

                   
http://www.arabzwaj.com/welcome/viewtopic.php?p=11395#11395
                                   
http://www.arabzwaj.com/welcome/viewtopic.php?t=2329

                                                             
www.arabzwaj.com

                               

                                 
http://www.arabzwaj.com/welcome/viewtopic.php?t=2333
                                                   
http://www.arabzwaj.com/welcome/viewtopic.php?t=2332
                         
http://www.arabzwaj.com/welcome/viewtopic.php?t=2328
                               
http://www.arabzwaj.com/welcome/viewtopic.php?p=11403#11403
                                               
http://www.arabzwaj.com/welcome/viewtopic.php?p=11404#11404

             
http://www.arabzwaj.com/welcome/viewtopic.php?t=2336
                               
http://www.arabzwaj.com/welcome/viewtopic.php?p=11406#11406

                                                   ...!

http://www.arabzwaj.com/welcome/viewtopic.php?p=11408#11408
                                            !
http://www.arabzwaj.com/welcome/viewtopic.php?t=2340
                       ..............   !!!!!
http://www.arabzwaj.com/welcome/viewtopic.php?t=2341

                     
http://www.arabzwaj.com/arabzwaj/welcome/viewtopic.php?t=2303
                               
http://www.arabzwaj.com/arabzwaj/welcome/viewtopic.php?t=2299&start=0&postdays=0
&postorder=asc&highlight
=
   -                                  
http://www.arabzwaj.com/welcome/viewtopic.php?t=2278
(    )                                        
http://www.arabzwaj.com/welcome/viewtopic.php?t=2279
             
http://www.arabzwaj.com/arabzwaj/welcome/viewtopic.php?t=2302&start=0&postdays=0
&postorder=asc&highlight
=
                           
http://www.arabzwaj.com/arabzwaj/welcome/viewtopic.php?t=2301

                             
http://www.arabzwaj.com/welcome/viewtopic.php?t=2270
                                                     
http://www.arabzwaj.com/welcome/viewtopic.php?t=2272
                                                 
http://www.arabzwaj.com/arabzwaj/welcome/viewtopic.php?t=2297
           (           )

http://www.arabzwaj.com/welcome/viewtopic.php?t=2271
              (          )
http://www.arabzwaj.com/welcome/viewtopic.php?t=2273
                                         
http://www.arabzwaj.com/welcome/viewtopic.php?t=2264
>>FIFA 2007
http://www.arabzwaj.com/welcome/viewtopic.php?t=2266

                            www.arabzwaj.com

for marrige and friend ship                                      
     www.arabzwaj.com

www.arabzwaj.com                                        

www.arabzwaj.com

                           

the best web site formarrige and frindship www.arabzwaj.com         
                     

                                                                   
                                                       :-
www.arabzwaj.com/welcome

enter here and watch sexy arab girls

                     
www.arabwazaef.com/arab

                 
http://www.arabzwaj.com/welcome/viewtopic.php?t=1892
                   
http://www.arabzwaj.com/welcome/viewtopic.php?t=900
****                         ****
http://www.arabzwaj.com/welcome/viewtopic.php?t=1894
                                 (                      )
http://www.arabzwaj.com/welcome/viewtopic.php?t=1880
                           
http://www.arabzwaj.com/welcome/viewtopic.php?t=1879

a     +           +          
http://www.arabzwaj.com/welcome/viewtopic.php?t=1956

                       
http://www.arabzwaj.com/welcome/viewtopic.php?p=10201#10201
              ....                              
http://www.arabzwaj.com/welcome/viewtopic.php?p=10210#10210
             000000                     0
http://www.arabzwaj.com/welcome/viewtopic.php?t=686
                    ..!
http://www.arabzwaj.com/welcome/viewtopic.php?p=10203#10203
                   
http://www.arabzwaj.com/welcome/viewtopic.php?p=10207#10207
                                             
http://www.arabzwaj.com/welcome/viewtopic.php?p=10211#10211

        ....                  !!!
http://www.arabzwaj.com/welcome/viewtopic.php?p=10200#10200
                 
http://www.arabzwaj.com/welcome/viewtopic.php?p=10212#10212
                              .....     !!
http://www.arabzwaj.com/welcome/viewtopic.php?p=10208#10208

                                                  :-
http://www.arabzwaj.com/welcome/viewtopic.php?p=10095#10095
         5000      
http://www.arabzwaj.com/welcome/viewtopic.php?t=1952&sid=15f0596ebc9c205da75f65e
b723025a2

                                       
http://www.arabzwaj.com/welcome/viewtopic.php?t=1587&start=0&postdays=0&postorde
r=asc&highlight
=
         
http://www.arabzwaj.com/welcome/viewtopic.php?p=10213#10213

                                         
http://www.arabzwaj.com/welcome/viewtopic.php?p=10209#10209
                             
http://www.arabzwaj.com/welcome/viewtopic.php?p=10205#10205
            .......              
http://www.arabzwaj.com/welcome/viewtopic.php?p=10206#10206

                                            :-
http://www.arabzwaj.com/welcome/viewtopic.php?p=9855#9855

             000000                     0
http://www.arabzwaj.com/welcome/viewtopic.php?t=686
                                                   
http://www.arabzwaj.com/welcome/viewtopic.php?t=1873
                 
http://www.arabzwaj.com/welcome/viewtopic.php?t=124

                                            :-
http://www.arabzwaj.com/welcome/viewtopic.php?p=9855#9855

                          (       )                      
http://www.arabzwaj.com/welcome/viewtopic.php?p=9736#9736
                          Windows Live Messenger 8.0
http://www.arabzwaj.com/welcome/viewtopic.php?t=1834
                       (       )
http://www.arabzwaj.com/welcome/viewtopic.php?t=1831
                               
http://www.arabzwaj.com/welcome/viewtopic.php?t=1812

                                                     
http://www.arabzwaj.com/welcome/posting.php?mode=editpost&p=9729

                  ...                    ..
http://www.arabzwaj.com/welcome/viewtopic.php?t=1796

                                       

http://www.arabzwaj.com/welcome/viewtopic.php?t=1789
                                                         
http://www.arabzwaj.com/welcome/viewtopic.php?t=1568&highlight=%E3%E1%DD
3                                    
http://www.arabzwaj.com/welcome/viewtopic.php?t=1830

                                             
http://www.arabzwaj.com/welcome/viewtopic.php?t=1790
                                     
http://www.arabzwaj.com/welcome/viewtopic.php?t=102

                          00,                              
http://www.arabzwaj.com/welcome/viewtopic.php?p=9739#9739
                   (      )
http://www.arabzwaj.com/welcome/viewtopic.php?p=9740#9740
                                       2038
http://www.arabzwaj.com/welcome/viewtopic.php?t=1837
               
http://www.arabzwaj.com/welcome/viewtopic.php?t=1824
                   
http://www.arabzwaj.com/welcome/viewtopic.php?t=1821
                                       
http://www.arabzwaj.com/welcome/viewtopic.php?t=1768
                             !!
http://www.arabzwaj.com/welcome/viewtopic.php?t=1751
                               
http://www.arabzwaj.com/welcome/viewtopic.php?t=1838
                                                         
http://www.arabzwaj.com/welcome/viewforum.php?f=11
                                                   
http://www.arabzwaj.com/welcome/viewtopic.php?t=234
                                         
                                               
                     
                                                                   
                                                               
www.arabzwaj.com
                               
        :-arabzwaj@yahoo.com
                                                               
                     
hide ip
              +                                  :
           :-
http://d.turboupload.com/d/839284/157615851606157516051580hide.rar.html
            :-
http://www.megaupload.com/?d=BBCGC4J0

the best web site formarrige and frindship www.arabzwaj.com
Edwinah63 - 20 Sep 2007 04:45 GMT
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

}
Greg R. Broderick - 20 Sep 2007 18:05 GMT
> Hi everyone,
>
[quoted text clipped - 5 lines]
> 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

Signature

---------------------------------------------------------------------
Greg R. Broderick                  usenet200705@blackholio.dyndns.org

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

Roedy Green - 20 Sep 2007 18:19 GMT
On Thu, 20 Sep 2007 12:05:16 -0500, "Greg R. Broderick"
<usenet200709@blackholio.dyndns.org> wrote, quoted or indirectly
quoted someone who said :

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

e.g. rm.getHeader( "Message-ID" );
rm.getHeader( "X-Mailer",";" )
Address[] replyTos = rm.getReplyTo();

Again for sample code see Bulk.java in
http://mindprod.com/products1.html#BULK
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Edwinah63 - 21 Sep 2007 00:00 GMT
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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.