Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
233 views
in Technique[技术] by (71.8m points)

java - 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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...