1

It seems to me the snippet below should work, but "mp.getBodyPart(1).getContent().toString()" returns

com.sun.mail.util.BASE64DecoderStream@44b07df8

instead of the contents of the attachment.

public class GMailParser {
    public String getParsedMessage(Message message) throws Exception {
        try {
            Multipart mp = (Multipart) message.getContent();
            String s = mp.getBodyPart(1).getContent().toString();
            if (s.contains("pattern 1")) {
                return "return 1";
            } else if (s.contains("pattern 2")) {
                return "return 2";
            }
            ...
jacknad
  • 13,483
  • 40
  • 124
  • 194
  • See this as well http://stackoverflow.com/questions/5628395/javamail-parsing-email-content-cant-seem-to-get-it-to-work-message-getcont/26142591#26142591 – NoNaMe Oct 01 '14 at 14:03

2 Answers2

3

It simply means that the BASE64DecoderStream class does not provide a custom toString definition. The default toString definition is to display the class name + '@' + Hash Code, which is what you see.

To get the "content" of the Stream you need to use the read() method.

Vincent Ramdhanie
  • 102,349
  • 23
  • 137
  • 192
1

This parses BASE64DecoderStream attachments exactly as needed.

private String getParsedAttachment(BodyPart bp) throws Exception {
    InputStream is = null;
    ByteArrayOutputStream os = null;
    try {
        is = bp.getInputStream();
        os = new ByteArrayOutputStream(256);
        int c = 0;
        while ((c = is.read()) != -1) {
            os.write(c);
        }
        String s = os.toString(); 
        if (s.contains("pattern 1")) { 
            return "return 1"; 
        } else if (s.contains("pattern 2")) { 
            return "return 2"; 
        } 
        ... 
jacknad
  • 13,483
  • 40
  • 124
  • 194