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

Discussion in 'Java' started by Edwinah63, Sep 19, 2007.

  1. Edwinah63

    Edwinah63 Guest

    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
     
    Edwinah63, Sep 19, 2007
    #1
    1. Advertisements

  2. Edwinah63

    derek Guest

    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?
     
    derek, Sep 19, 2007
    #2
    1. Advertisements

  3. Edwinah63 <> wrote in
    news::

    > 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

    A. Top posters.
    Q. What is the most annoying thing on Usenet?
    ---------------------------------------------------------------------
     
    Greg R. Broderick, Sep 19, 2007
    #3
  4. Re: Extract parts from a Multipart/Related email using Java MailAPI

    Edwinah63 wrote:
    > 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).


    --
    martin@ | Martin Gregorie
    gregorie. | Essex, UK
    org |
     
    Martin Gregorie, Sep 19, 2007
    #4
  5. Edwinah63

    Roedy Green Guest

    On 19 Sep 2007 09:17:41 -0700, Edwinah63 <>
    wrote, quoted or indirectly quoted someone who 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.


    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


    --
    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
     
    Roedy Green, Sep 20, 2007
    #5
  6. Edwinah63

    Edwinah63 Guest

    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


    }
     
    Edwinah63, Sep 20, 2007
    #6
  7. Edwinah63

    Edwinah63 Guest

    Hi Martin,

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

    Edwina
     
    Edwinah63, Sep 20, 2007
    #7
  8. Edwinah63 <> wrote in
    news::

    > 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

    A. Top posters.
    Q. What is the most annoying thing on Usenet?
    ---------------------------------------------------------------------
     
    Greg R. Broderick, Sep 20, 2007
    #8
  9. Edwinah63

    Roedy Green Guest

    On Thu, 20 Sep 2007 12:05:16 -0500, "Greg R. Broderick"
    <> 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
    --
    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
     
    Roedy Green, Sep 20, 2007
    #9
  10. Edwinah63

    Edwinah63 Guest

    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
     
    Edwinah63, Sep 21, 2007
    #10
    1. Advertisements

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jag
    Replies:
    1
    Views:
    515
    Greg R. Broderick
    Dec 29, 2006
  2. Replies:
    3
    Views:
    681
    Keith Thompson
    Mar 31, 2007
  3. Patrick
    Replies:
    0
    Views:
    365
    Patrick
    Feb 17, 2004
  4. Replies:
    1
    Views:
    549
Loading...

Share This Page