Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaMail - Parsing email content, can't seem to get it to work! (Message.getContent())

For a few weeks I have been developing a email client for android, I have been ignoring parsing email content for a while as I have never been able to get it to work. Thus, the time has come to ask for help!

I have been looking around and I have come across a few methods I have tried but never had much success with! Currently my closest attempt would have to be:

private String parseContent(Message m) throws Exception
{       
    //Multipart mp = (Multipart)c;
    //int j = mp.getCount();

    /*for (int i = 0; i < mp.getCount(); i++)
    {
        Part part = mp.getBodyPart(i);
        System.out.println(((MimeMessage)m).getContent());
        content = content + part.toString();
        //System.out.println((String)part.getContent());
    }*/

    Object content = m.getContent();
    String contentReturn = null;

    if (content instanceof String) 
    {
        contentReturn = (String) content;
    } 
    else if (content instanceof Multipart) 
    {
        Multipart multipart = (Multipart) content;
        BodyPart part = multipart.getBodyPart(0);
        part.toString();
        contentReturn = part.getContent().toString();
    }   
    return contentReturn;
}

But it does not work and I get gibberish such as "javax.mail.internet.MimeMultipart@44f12450".

Can anyone see where I am going wrong?

Thanks, Rhys

like image 575
Rhys12341111 Avatar asked Apr 11 '11 22:04

Rhys12341111


3 Answers

I had the same issues while parsing Message of javax mail. In my workaround i found a weird thing. Listing mail from POP3 was not giving me Mail body content. So used IMAP which worked for me. Now i'm able to parse Text/plain as well as Text/Html and read the body. To parse the same i used following method.

public String printMessage(Message message) {

    String myMail = "";

    try {
        // Get the header information
        String from = ((InternetAddress) message.getFrom()[0])
                .getPersonal();



        if (from == null)
            from = ((InternetAddress) message.getFrom()[0]).getAddress();
        System.out.println("FROM: " + from);
        String subject = message.getSubject();
        System.out.println("SUBJECT: " + subject);
        // -- Get the message part (i.e. the message itself) --
        Part messagePart = message;
        Object content = messagePart.getContent();
        // -- or its first body part if it is a multipart message --
        if (content instanceof Multipart) {
            messagePart = ((Multipart) content).getBodyPart(0);
            System.out.println("[ Multipart Message ]");
        }
        // -- Get the content type --
        String contentType = messagePart.getContentType();
        // -- If the content is plain text, we can print it --
        System.out.println("CONTENT:" + contentType);
        if (contentType.startsWith("TEXT/PLAIN")
                || contentType.startsWith("TEXT/HTML")) {
            InputStream is = messagePart.getInputStream();
            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(is));
            String thisLine = reader.readLine();
            while (thisLine != null) {
                System.out.println(thisLine);
                myMail = myMail + thisLine;
                thisLine = reader.readLine();
            }


        }
        System.out.println("-----------------------------");
    } catch (Exception ex) {
        ex.printStackTrace();
    }

    return myMail;

}

Hope it helps someone.

like image 122
Shahbaz sultan Avatar answered Oct 19 '22 11:10

Shahbaz sultan


None of the above suggestions is valid. You don't need to do anything complex here. Mimemessage has got message.writeTo(outputStream);

All you need to print the message is:

message.writeTo(System.out);

The above code will print the actual mime message to the console (or you can use any logger).

Save the content to .eml and you can open it in outlook. Simple as that!

like image 11
Aneesh Vijendran Avatar answered Oct 19 '22 13:10

Aneesh Vijendran


    Multipart multipart = (Multipart) content;
    BodyPart part = multipart.getBodyPart(0);
    part.toString();
    contentReturn = part.getContent().toString();

When you have BodyPart part, you should keep testing

if(part.getContent() instanceof String){ ... }

like image 4
DiTime4Tea Avatar answered Oct 19 '22 12:10

DiTime4Tea