title: Get MimeMessage Content as String
body: <p>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.</p>
  
  <p>So here's the code where i'm getting a SmtpMessage:</p>
  
  <pre><code>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("\\&lt;+([a-zA-Z0-9@.-]*)\\&gt;+");
          matcher = fromMail.matcher(from);
          if(matcher.find()){
              from = matcher.group(1);
              LOG.info(from);
          }else{
              throw new RuntimeException("Ilegal From Address: " + from);
          }
  </code></pre>
  
  <p>Now i'm getting the content itself:</p>
  
  <pre><code>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();
  </code></pre>
  
  <p>Now I have a String that looks like this: 
  javax.mail.internet.MimeMultipart@4edfc9bb</p>
  
  <p>But when I write it out like this:</p>
  
  <pre><code>((Multipart) message).writeTo(System.out);
  </code></pre>
  
  <p>I get this where I can see the content i want:</p>
  
  <pre><code>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 ...
  </code></pre>
  
  <p>But it can't be empty right?
  I'm trying to get it the content like that:</p>
  
  <pre><code>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 &lt; 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();
                  }
              }
          }
  </code></pre>
  
  <p>I really need to get this done and I'm searching on the internet for ages..</p>