简体   繁体   中英

Read the first line of the email message and forward with attachment - java.mail

I have a requirement where I need to process the first line in the email message and, possibly, forward it.

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.

Can anyone point me to a good resource with some code examples? Thank you

The code of getting the first line of the email message, forwarding I don't have working:

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 < 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;
}

The simplest way will be to forward the original message as an attachment to the new message. See the JavaMail FAQ .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM