org.apache.commons.mail.util.MimeMessageParser never ends parsing

1k views Asked by At

I have some code to parse incoming mails. I use org.apache.commons.mail.util.MimeMessageParser for this.The problem occurs in the following method:

private String getActionDescription(Message message) throws Exception {
    MimeMessage mimeMessage = (MimeMessage)message;
    MimeMessageParser mmp = new MimeMessageParser(mimeMessage);
    mmp.parse(); // !!! sometimes my code hangs here; inside this parse method
    String description = mmp.getHtmlContent();
    if(Utils.isNullOrEmpty(description)) {
        description = mmp.getPlainContent();
        if(description != null) {
            description = description.replace("\r\n", "<br>");
        }
    }
    return description;
}

Most of the time it works fine, but every few days my code gets stuck inside mmp.parse(). I don't get any exception or timeout. It just stops.

Anyone can shed any light on this?

Kind regards.

P.S.: if you need more info, just ask and I'll add it here.

apache jar version: commons-email-1.5.jar

jdk version: 11

my imports:

import java.io.IOException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.mail.Address;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Part;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;

import org.apache.commons.mail.util.MimeMessageParser;
1

There are 1 answers

0
Tom On BEST ANSWER

adding following properties when getting a mail session solved the problem:

mail.imap.partialfetch false
mail.imap.fetchsize 1048576

Mails with big attachments slowed parsing down extremely. These settings greatly improved network IO to the mail server for big attachments.