Мне кажется, что нижеприведенный фрагмент должен работать, но "mp.getBodyPart(1).getContent(). toString()" возвращает
com.sun.mail.util.BASE64DecoderStream@44b07df8
вместо содержимого вложения.
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";
}
...
Это просто означает, что класс BASE64DecoderStream не предоставляет настраиваемое определение toString. По умолчанию определение toString должно отображать имя класса + '@' + код хэша, что вы видите.
Чтобы получить "контент" Stream, вам нужно использовать метод read().
Это анализирует вложения BASE64DecoderStream точно так, как нужно.
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";
}
...