title: Read the first line of the email message and forward with attachment - java.mail
body: <p>I have a requirement where I need to process the first line in the email message and, possibly, forward it. </p>
  
  <p>But the problem happens when this message has attachments. And I need to forward them as well. I just can't find a good example of processing email messages with java.mail in a safe way that would cater for multiple message structures. Also, the forwarding example is a problem.</p>
  
  <p>Can anyone point me to a good resource with some code examples?
  Thank you</p>
  
  <p>The code of getting the first line of the email message, forwarding I don't have working:</p>
  
  <pre><code>private String getMessgaeFirstLine(Message msg) throws IOException, MessagingException{
      String result = null;
      Object objRef = msg.getContent();   
      Multipart mp = (Multipart) objRef;
      int count = mp.getCount();
      for (int i = 0; i &lt; count; i++)
      {
              BodyPart bp = mp.getBodyPart( i );
              if (bp instanceof MimeBodyPart )
                  {
                  MimeBodyPart mbp = (MimeBodyPart) bp;
  
                  if ( mbp.isMimeType( "text/plain" )) {
                  result = (String) mbp.getContent();
                  result = result.replaceAll("(\\r|\\n)", "");
                  break;
                  } 
              }
          }
      return result;
  }
  </code></pre>