Get MimeMessage Content as String

Sundaze picture Sundaze · Nov 18, 2015 · Viewed 16.7k times · Source

I have been searching and trying this so long i need your help. I have a server that gets HTML email. After that, i want to get the parts of it (header, subject and bodypart). Getting the header and subject is no problem, but the content is.

So here's the code where i'm getting a SmtpMessage:

private SmsItem getEmailData(SmtpMessage msg) throws MessagingException, IOException {
        String to = msg.getHeaderValue("To");
        String from = msg.getHeaderValue("From");
        String content = null;
        LOG.info("Mail from: " + from + " to: " + to);

        String toNr;
        Pattern mobileNr = Pattern.compile("(\\+?[0-9]+)@*");
        Matcher matcher = mobileNr.matcher(to);
        if (matcher.find()) {
            toNr = Utils.processAddress(matcher.group(1));
        } else {
            throw new RuntimeException("Illegal address to send SMS from mail: " + to);
        }

        Pattern fromMail = Pattern.compile("\\<+([[email protected]]*)\\>+");
        matcher = fromMail.matcher(from);
        if(matcher.find()){
            from = matcher.group(1);
            LOG.info(from);
        }else{
            throw new RuntimeException("Ilegal From Address: " + from);
        }

Now i'm getting the content itself:

Session s = Session.getDefaultInstance(new Properties());
        String ip = msg.toString();
        ip = MimeUtility.decodeText(ip);
        InputStream is = new ByteArrayInputStream(ip.getBytes("ISO-8859-1"));
        MimeMessage mi = new MimeMessage(s, is);

        Object message = mi.getContent();
        String test = mi.getContent().toString();
        test.toString();

Now I have a String that looks like this: javax.mail.internet.MimeMultipart@4edfc9bb

But when I write it out like this:

((Multipart) message).writeTo(System.out);

I get this where I can see the content i want:

This is a multi-part message in MIME format.--------------010600010509000401060604Content-Type: text/plain; charset=utf-8; format=flowedContent-Transfer-Encoding: 7bit
*asdasd ad asd asddfgdfgsdfsd:-)*
  asdsad
javax.mail.MessagingException: Empty multipart: multipart/alternative;
    at javax.mail.internet.MimeMultipart.writeTo(MimeMultipart.java:548)
    at ...

But it can't be empty right? I'm trying to get it the content like that:

if (message instanceof String)
        {
            message = (String)message;
            LOG.info("String");
        }
        else if (message instanceof Multipart)
        {   
            Multipart mp = (Multipart)message;
            LOG.info("Count: " + mp.getCount() + mp.getParent() + test.toString());
            for (int i = 0; i < mp.getCount(); i++){
                LOG.info("Multipart");
                LOG.info(i + ". " + mp.getBodyPart(i).toString());
                BodyPart bp = mp.getBodyPart(i);
                String dp = bp.getDisposition();

                if(dp != null){
                    LOG.error("Something's wrong");
                }else if(dp.equalsIgnoreCase("ATTACHMENT")){
                    LOG.error("Attachment dürfen nicht mitgegeben werden!");
                }else{
                    content = bp.getContent().toString();
                }
            }
        }

I really need to get this done and I'm searching on the internet for ages..

Answer

Daniel picture Daniel · Nov 18, 2015

Using commons-email you can get the content as follows

MimeMessageParser parser = new MimeMessageParser(yourMimeMessage);
parser.parse();

String htmlContent = parser.getHtmlContent();
// or
String plainContent = parser.getPlainContent();